36 Silberschatz Galvin and Gagne 2013 Operating System Concepts 9 th Edition

36 silberschatz galvin and gagne 2013 operating

This preview shows page 36 - 43 out of 72 pages.

5.36 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Bounded Buffer Problem (Cont.) The structure of the producer process do { ... /* produce an item in next_produced */ ... wait(empty); wait(mutex); ... /* add next produced to the buffer */ ... signal(mutex); signal(full); } while (true);
Image of page 36
5.37 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Bounded Buffer Problem (Cont.) The structure of the consumer process Do { wait(full); wait(mutex); ... /* remove an item from buffer to next_consumed */ ... signal(mutex); signal(empty); ... /* consume the item in next consumed */ ... } while (true);
Image of page 37
5.38 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Discussions Do we need Semaphore full and empty (or only one of them) if there is only one buffer? Why Semaphore mutex? Shouldn’t the other two semaphores guarantee that the n buffers will be written and read correctly? Semaphore mutex can be removed when there is only one producer and one consumer. With multiple producers sharing the same memory space for the item buffer, or multiple consumers sharing the same memory space, removing mutex could result in two or more processes reading or writing into the same slot at the same time
Image of page 38
5.39 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Readers-Writers Problem A data set is shared among a number of concurrent processes Readers – only read the data set; they do not perform any updates Writers – can both read and write How is this different from the producer-consumer problem? Problem – allow multiple readers to read at the same time Only one single writer can access the shared data at the same time Several variations of how readers and writers are considered – all involve some form of priorities Shared Data Data set Semaphore rw_mutex initialized to 1 Semaphore mutex initialized to 1 Integer read_count initialized to 0
Image of page 39