

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Fall 2004;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!
Process 1 Process 2 S1 S2^ S Thread 1 Thread 2 Thread 3 Thread 1 Thread 2
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); } }
2 Bounded Buffer There is a set of resource buffers shared by producer and consumer threads Producer inserts resources into buffer set if not full Consumer removes resources if buffer set not empty They execute at different rates
What semaphores will we need? What do we need to keep track of? make sure not writing at same time make sure there’s something to read make sure there’s room to write Leads to one mutex and 2 counting Bounded Buffer Implementation Pair up and work on this problem Three semaphores:
You need two threads:
do { produce an item wait(empty); wait(mutex); add nextp to buffer signal( mutex ); signal( full ); } Semaphore mutex = 1; Semaphore full = 0; Semaphore empty = N; do { wait(full); wait(mutex); remove an item signal(mutex); wait(empty); consume the item } What if there were more than one producer or consumer What if we switch around mutex and full? (Deadlock) Semaphores Summary Semaphores can be used to solve any of the traditional synchronization problems They have some drawbacks:
Sometimes hard to use and prone to bugs.