Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control...
-
Upload
domenic-obrien -
Category
Documents
-
view
212 -
download
0
Transcript of Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control...
Project 2
kthreads, concurrency, 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.
Kthreads
• Thread: basic executable unit.
• Run the shuttle in its own kthread, not the thread that calls the module_init function.
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
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
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.
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);
Structure
int yourstartshuttle(void) {//initilization//kthread_run
}
int yourissuerequest(pass_type, initial_terminal, dest_terminal) {//add pass
}
int yourstopshuttle(void) {//release resources
Add one
1 2 3 4 5
shuttle
Good
1 2 3 4 5
shuttle
Remove one
1 2 3 4 5
shuttle
Good
1 2 3 4 5
shuttle
Simultaneously
1 2 3 4 5
shuttle
1 2 3 4 5
shuttle
Bad
1 2 3 4 5
shuttle
That is correct
1 2 3 4 5
shuttle
Why
• Two processes(threads) are accessing the same data.
• The order of doing things matters.
• Potential problem, unpredictable.
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.
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
"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.
• Add one lock before accessing the data;
• Remove the lock after accessing the data;
• What is a lock– mutex– semaphore– atom– ......
Mutex
• Semaphone is old... do not use semaphone.
• Use mutex.
• Put a pair of mutex lock before and after the critical section.
mutex
struct mutex lock;
mutex_init(&lock);
mutex_lock(&lock);{ ......
......
......
......}mutex_unlock(&lock);
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)
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
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
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...
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
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
• Q&A