







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








School of Electrical Engineering and Computer Science University of Central Florida 1
Huiyang Zhou
Slides 1-18 are from Professor Umakishore Ramachandran @ GaTech
compute
compute
I/O
I/O result Needed
(a) Sequential process
compute thread
I/O result Needed
(b) Multithreaded process
I/O request
I/O complete
I/O thread
3
Digitizer Tracker Alarm
Main thread
thread_create(foo, args)
(a) Before thread creation
main thread
thread_create(foo, args)
(b) After thread creation
foo thread
7
tracker() { image_type track_image; int head = 0; loop { if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } } }
Problem?
digitizer tracker
bufavail = bufavail – 1; bufavail bufavail = bufavail + 1;
Shared data structure
……
head tail (First valid filled frame in frame_buf)
(First empty spot in frame_buf)
0 frame_buf 99
9
digitizer() { image_type dig_image; int tail = 0; loop { thread_mutex_lock(buflock); if (bufavail > 0) { grab(dig_image); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; bufavail = bufavail - 1; } thread_mutex_unlock(buflock); } }
tracker() ( image_type track_image; int head = 0;
loop { thread_mutex_lock(buflock); if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } thread_mutex_unlock(buflock); } } Problem?
13
T1 T
cond_wait (c, m)
blocked cond_signal (c) resumed
T1 T
cond_wait (c, m)
cond_signal (c)
(a) Wait before signal (^) (b) Wait after signal (T1 blocked forever)
15
digitizer() { image_type dig_image; int tail = 0; loop { grab(dig_image); thread_mutex_lock(buflock); if (bufavail == 0) thread_cond_wait(buf_not_full, buflock); thread_mutex_unlock(buflock); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; thread_mutex_lock(buflock); bufavail = bufavail - 1; thread_cond_signal(buf_not_empty); thread_mutex_unlock(buflock); } }
tracker() { image_type track_image; int head = 0; loop { thread_mutex_lock(buflock); if (bufavail == MAX) thread_cond_wait(buf_not_empty, buflock); thread_mutex_unlock(buflock); track_image = frame_buf[head mod MAX]; head = head + 1; thread_mutex_lock(buflock); bufavail = bufavail + 1; thread_cond_signal(buf_not_full); thread_mutex_unlock(buflock); analyze(track_image); } }
This solution is correct so long as there is exactly one producer and one consumer
acquire_shared_resource() { thread_mutex_lock(cs_mutex); if (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; thread_cond_signal(res_not_busy); thread_mutex_unlock(cs_mutex); }
T3 is here
T2 is here
T1 is here
19
Defensive programmingDefensive programming –– retest predicateretest predicate
acquire_shared_resource() { thread_mutex_lock(cs_mutex); T3 is here while (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); T2 is here res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; T1 is here thread_cond_signal(res_not_buys); thread_mutex_unlock(cs_mutex); }
Defensive programmingDefensive programming –– retest predicateretest predicate
acquire_shared_resource() { thread_mutex_lock(cs_mutex); while (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); T2 is still here (get the lock, fail the condition, wait again) res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; thread_cond_signal(res_not_buys); thread_mutex_unlock(cs_mutex); } T1 is here
T3 is here (release the lock)
21
digitizer() { image_type dig_image; int tail = 0; loop { thread_mutex_lock(buflock); if (bufavail > 0) { grab(dig_image); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; bufavail = bufavail - 1; } thread_mutex_unlock(buflock); } }
tracker() ( image_type track_image; int head = 0;
loop { thread_mutex_lock(buflock); if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } thread_mutex_unlock(buflock); } } Problem?
Challenges of TM Challenges of TM