Download presentation
Presentation is loading. Please wait.
Published byOscar Leon Nash Modified over 9 years ago
1
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages 477-481 ICS 145B L. Bic
2
ICS 145B -- L. Bic2 Assignment Using Pthreads (or some other thread package): –set up a race condition –solve it using (1) mutex (2) software solution compare performance –develop P/V using (1) mutex (2) software sol. compare performance –implement bounded buffer (groups only)
3
ICS 145B -- L. Bic3 Creating Pthreads on Unix, compile as: gcc -lpthread prog.c program structure: #include /* global declarations */ void *foo(void *arg){...} /* func for thread */ int main(){...}
4
ICS 145B -- L. Bic4 Creating Pthreads to create a new thread pthread_create(&t, &a, (void*)f, p); t: variable of type pthread_t (name of new thread) a: variable of type pthread_attr_t (various attributes, e.g. RR scheduling) f: function to run by new thread p: parameter for f
5
ICS 145B -- L. Bic5 Creating Pthreads Example: thread without attributes –thread function foo: single argument of type void*, returns void* void *foo(void *arg) {...} int main(){ pthread_t th1; pthread_create( &th1, NULL, (void*)foo, NULL );... }
6
ICS 145B -- L. Bic6 Creating Pthreads using scheduling attribute (RR) –define attribute variable pthread_attr_t atr; –create attribute structure pthread_attr_init(&atr); –set attribute pthread_attr_setschedpolicy( &atr, SCHED_RR);
7
ICS 145B -- L. Bic7 Creating Pthreads int main(){ pthread_t th1; pthread_attr_t att; pthread_attr_init(&att); pthread_attr_setschedpolicy( &att, SCHED_RR);... pthread_create( &th1, &att, (void*)foo, NULL );... }
8
ICS 145B -- L. Bic8 Creating Pthreads main needs to wait for child threads: int main(){ pthread_t th1;... pthread_create( &th1, &att, (void*)foo, NULL );... pthread_join(th1, NULL); }
9
ICS 145B -- L. Bic9 Setting up a race condition create 2 concurrent threads; both do: counter = accnt1 = accnt2 = 0; do { tmp1 = accnt1; tmp2 = accnt2; r = rand(); accnt1 = tmp1 + r; accnt2 = tmp2 - r; counter++; } while ( accnt1 + accnt2 == 0 ); print(counter);
10
ICS 145B -- L. Bic10 Solving the Critical Section Solution 1: use mutex locks of Pthreads –mutex is a variable of type pthread_mutex_t –must be initialized using init macro: pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; –locking mutex; thread blocks if mutex already locked pthread_mutex_lock(&mu); –unlocking mutex; if processes waiting, unblock one pthread_mutex_unlock(&mu); –use lock/unlock to guard CS
11
ICS 145B -- L. Bic11 Solving the Critical Section Solution 2: Peterson’s software solution th1: while (1) { c1 = 1; will_wait = 1; while (c2 && (will_wait==1)) ; CS1; c1 = 0; } th2: while (1) { c2 = 1; will_wait = 2; while (c1 && (will_wait==2)) ; CS2; c2 = 0; } –CS is the code between first read and second write –Caution: accnt1+accnt2==0 on pg 9 is also a CS!!
12
ICS 145B -- L. Bic12 Performance comparison implement the two solutions repeat the do loop n times, measure execution time of both solutions repeat several times to get averages how to measure time: –at command line: time program_to_be_timed –inside program: #include gettimeofday()
13
ICS 145B -- L. Bic13 Implementing P/V Solution 1: P/V use binary semaphores; –use mutex locks of Pthreads as binary semaphores Pb(sb): if sb==1, set sb=0 and proceed; else wait until sb==1 and retry This is equivalent to locking a mutex and waiting if already locked Vb(sb): sb=1 This is equivalent to unlocking a mutex
14
ICS 145B -- L. Bic14 Implementing P/V Implementing P/V using Pb/Vb P(s) { Pb(sb); s = s-1; if (s < 0) {Vb(sb); Pb(delay);} Vb(sb); } V(s) { Pb(sb); s = s+1; if (s <= 0) Vb(delay); else Vb(sb); Pb is like pthread_mutex_lock Vb is line pthread_mutex_unlock use two mutex variables to implement P/V
15
ICS 145B -- L. Bic15 Implementing P/V Solution 2: use software solution for CS –each semaphore s is an integer –access to s must be a CS –P: if (s>0) decrement s and proceed if (s==0) yield CPU and try again Note: yields must not be issued inside CS (deadlock)! –V: increment s
16
ICS 145B -- L. Bic16 Performance comparison implement the two solutions for each solution, try different tests, e.g.: –single thread repeatedly executes P, V, P, V, … no blocking, just execution times –n threads block on a single P; another thread then executes n V operations tests blocking and unblocking –2 threads: th1 executes P, th2 executes V (repeatedly, with random delays) try to come up with interesting tests
17
ICS 145B -- L. Bic17 Bounded buffer problem (groups only) semaphore e = n, f = 0, b = 1; Producer: while (1) { read element from file 1; P(e);P(b); Add element to buffer; V(b);V(f); cause a random delay} Consumer: while (1) { P(f);P(b); Take element from buffer; V(b);V(e); write element to file 2; cause a random delay} implement buffer as an array producer and consumer each maintain current index plot number of elements in buffer over time test performance using the two versions of P/V
18
ICS 145B -- L. Bic18 Summary of tasks demonstrate race condition solve race condition using: (1) mutex; (2) software; compare performance implement P/V using: (1) mutex; (2) software; compare performance (groups) implement bounded buffer using (1) and (2); make sure #elements fluctuates over time; compare performance document your design and results
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.