project 2 kthreads, concurrency, shuttle. highlevel overview user space –program wants to control...

30
Project 2 kthreads, concurrency, shuttle

Upload: domenic-obrien

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Project 2

kthreads, concurrency, shuttle

Page 2: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Highlevel overview

• User space– program wants to

control the shuttle simulation by sending requests.

– start_shuttle()– issue_request()– stop_shuttle()

•Kernel spaceprogram wants to simulate the shuttle according those requests.

Need to use a thread to simulate the shuttle running.

Page 3: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Kthreads

• Thread: basic executable unit.

• Run the shuttle in its own kthread, not the thread that calls the module_init function.

Page 4: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

kthreads

• Kernel Threads

• Kthread_run(threadfn, data, name, ...)– Creates a new thread of execution that starts

with the function pointed to by threadfn

– threadfn: fuction pointer– data: data passed to threadfn– name: name of the thread

Page 5: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

while shuttle_running

if users want get off at current terminal

Unload (release space)

if users want get on at current terminal

and have space (..other performance logic)

Load (consume space)

Move to next desired terminal (*)

endwhile

Page 6: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Module

• (1) Start a new kernel thread, performing a infinite loop.

• (2) Simulating the shuttle (load, unload, move to next)

• (3) Finally you may use kthread_stop to stop that thread.

Page 7: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Structure

static int yourshuttle_init(void) {//create an proc entry//set function pointers

}

static void yourshuttle_exit(void) {//reset function pointers to null//remove module data file

module_init(yourshuttle_init);module_exit(yourshuttle_exit);

Page 8: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Structure

int yourstartshuttle(void) {//initilization//kthread_run

}

int yourissuerequest(pass_type, initial_terminal, dest_terminal) {//add pass

}

int yourstopshuttle(void) {//release resources

Page 9: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Add one

1 2 3 4 5

shuttle

Page 10: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Good

1 2 3 4 5

shuttle

Page 11: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Remove one

1 2 3 4 5

shuttle

Page 12: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Good

1 2 3 4 5

shuttle

Page 13: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Simultaneously

1 2 3 4 5

shuttle

Page 14: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

1 2 3 4 5

shuttle

Page 15: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Bad

1 2 3 4 5

shuttle

Page 16: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

That is correct

1 2 3 4 5

shuttle

Page 17: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Why

• Two processes(threads) are accessing the same data.

• The order of doing things matters.

• Potential problem, unpredictable.

Page 18: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Concurrency problem?

• User space: – start_taxi, issue_request, stop_taxi

• Kernel space:– simulate the shuttle and the queues

• Bad thing happens if they are accessing the same queue.

Page 19: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Protection

• There can not be multiple threads accessing the same data, simultaneously.

• User space– add one passenger to the queue

• Kernel space– remove one passenger from the queue

Page 20: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

"Lock"

• The solution is to use lock.

• When a thread wants to access the queue, it first check if it is locked:– if yes, then it waits until it becomes unlocked– if no, then it is fine to move on.

Page 21: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

• Add one lock before accessing the data;

• Remove the lock after accessing the data;

• What is a lock– mutex– semaphore– atom– ......

Page 22: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Mutex

• Semaphone is old... do not use semaphone.

• Use mutex.

• Put a pair of mutex lock before and after the critical section.

Page 23: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

mutex

struct mutex lock;

mutex_init(&lock);

mutex_lock(&lock);{ ......

......

......

......}mutex_unlock(&lock);

Page 24: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Page 25: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Requests lock Receives it

Page 26: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Doing stuff

Page 27: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Requests lockAlready lockedWaits...

Page 28: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Releases lockNotifies threads

Page 29: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

MutexKernel Module

(Prepares Lock)

struct mutex m;mutex_init(&m);

User-space Code(Adds Alice)

Kernel Module(Removes Bob)

mutex_lock(&m)list_item->data = Alicelist_item->next = NULLlist_item->prev = bob_itemQueue->last = list_item mutex_unlock(&m);

mutex_lock(&m)newlast = list_item->last->prevnewlast->next = NULLdelete Queue->lastQueue->last = newlastmutex_unlock(&m)

Wakes upRequests lockGets lock

Page 30: Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

• Q&A