






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!







Mutual Exclusion Progress Bounded Waiting Performance
What did all the acquire() implementations have in common? while (1) { while ( TestAndSet( lock ) ); critical section lock = false; remainder section } while (1) { flag[i] = true; turn = j; while ( turn == j && flag[j]); critical section flag[i] = false; remainder section }
// type semaphore has two fields: a value and a list // of waiting processes (L) void wait ( semaphore S ) { S.value--; if (S.value < 0) { add this process to S.L; sleep(); // go to sleep; } } void signal( semaphore S ) { S.value++; if (S.value <= 0) { remove a process P from S.L wakeup(P); } } Who is responsible for waking up processes?
Hint: This code looks like our TestAndSet code—what had to be true about the TestAndSet function?
void wait ( semaphore S ) { S.value--; if (S.value < 0) { add this process to S.L; sleep(); // go to sleep; } } void signal( semaphore S ) { S.value++; if (S.value <= 0) { remove a process P from S.L wakeup(P); } }
how many are reading who wants to write who wants to read
// keep track of how many // reading int read_count = 0; // keep track of writing or // reading and who wants to // read/write Semaphore w_or_r = 1; Semaphore mutex = 1; writer { wait(w_or_r); write signal(w_or_r); } reader { wait(mutex); read_count++; if (read_count == 1) { wait(r_or_w); } signal(mutex); Read wait( mutex ); read_count--; if (read_count == 0 ) { signal(w_or_r); } signal(mutex); }