Docsity

Operating Systems Lecture 5: Semaphores, Exams of Operating Systems

Partial preview of the text

Download Operating Systems Lecture 5: Semaphores and more Exams Operating Systems in PDF only on Docsity!

COMP 310:

Operating Systems

Lecture 5: Semaphores

September 15, 2004

Christine Alvarado

Announcement

 Summer Research Party

 Next Tuesday

 Serra 210, 12noon-2pm

 Why you should come:

 Learn about cool research

 Because you can’t get enough of Serra Hall

 FREE FOOD!

Today’s Topics

 OS Support for Synchronization

 Semaphores

Last time

 Requirements for Critical Section

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 }

Spinning (Busy Waiting)

 Spinning is when a process is doing useless

instructions waiting for some condition to be

met

 Also called busy waiting

 Why is busy waiting a bad thing?

 What would be better?

Semaphores

 Operating system support for providing mutual

exclusion to critical sections

 Threads are “put to sleep” if the semaphore is

blocked (e.g. someone else is in CS)

 Thread are “woken up” when the semaphore

becomes open (e.g. the other thread left the CS)

 They are NOT SPINNING

 Does this mean they are on the ready queue?

Semaphores

 Semaphores support two operations:

 wait(semaphore): go to sleep until semaphore is

open

 signal(semaphore): wake up another thread that

is sleeping on this semaphore

 wait() also named P(), after

Dutch word for “test”

 signal also named V(), after

Dutch word for “increment”

What do semaphores need to

keep track of?

 Whether they can let someone in?

 Who is waiting (so they can wake them up)

Implementing Counting

semaphores

// 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?

What needs to be true about

the semaphore code?

// S is a semaphore
while (1) {
S.wait();
critical section
S.signal();
remainder section

Hint: This code looks like our TestAndSet code—what had to be true about the TestAndSet function?

What if this code wasn’t

atomic?

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); } }

Readers/Writers

 An object is shared across many threads.
 Some threads want to read it, others want to write to it
 Any number of readers can read at the same time
 When a writer writes, there can be no other readers or
writers accessing the object
 What do we need to keep track of?

how many are reading who wants to write who wants to read

Readers/Writers: Algorithm

Readers/Writers

// 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); }