Docsity

Classic IPC Problems: Producer-Consumer, Philosophers, Barber, and Readers - Prof. Jonatha, Study notes of Operating Systems

Partial preview of the text

Download Classic IPC Problems: Producer-Consumer, Philosophers, Barber, and Readers - Prof. Jonatha and more Study notes Operating Systems in PDF only on Docsity!

CS 333

Introduction to Operating SystemsClass 5 – Classical IPC Problems

Jonathan WalpoleComputer Science Portland State University

Classical IPC problems^ ^ Producer Consumer (bounded buffer)^ ^ Dining philosophers^ ^ Sleeping barber^ ^ Readers and writers

Is this a valid solution? thread producer {while(1){

// Produce char c while (count==n) {no_op} buf[InP] = cInP = InP + 1 mod ncount++} }

thread consumer {

while(1){while (count==0) {

no_op} c = buf[OutP]OutP = OutP + 1 mod ncount-- // Consume char } }

n-1 …

Global variables:

char buf[n]int InP = 0

// place to add

int OutP = 0

// place to get

int count

0 thread consumer { 1

while(1) { 2

while (count==0) { 3

sleep(empty) 4

c = buf[OutP] 6

OutP = OutP + 1 mod n 7

count--; 8

if (count == n-1) 9

wakeup(full) 10

// Consume char 11

How about this? 0 thread producer { 1

while(1) { 2

// Produce char c 3

if (count==n) { 4

sleep(full) 5

buf[InP] = c; 7

InP = InP + 1 mod n 8

count++ 9

if (count == 1) 10

wakeup(empty) 11

n-1 …

Global variables:

char buf[n]int InP = 0

// place to add

int OutP = 0

// place to get

int count

Producer consumer problem^ ^

What is the shared state in the last solution?  Does it apply mutual exclusion? If so, how?

8 Buffers

InP

OutP Consumer

Producer

Producer and consumerare separate threads

Dining philosophers problem^ ^

Five philosophers sit at a table  One fork between each philosopher  Why do they need to synchronize?  How should they do it?

while(TRUE) {Think();^ Grab first fork;Grab second fork;^ Eat();^ Put down first fork;Put down second fork; }

Each philosopher ismodeled with a thread

Working towards a solution …

#define N 5Philosopher() {while(TRUE) {

Think();take_fork(i);take_fork((i+1)% N);Eat();put_fork(i);put_fork((i+1)% N);} }

take_forks(i)^ put_forks(i)

Working towards a solution …

#define N 5Philosopher() {while(TRUE) {

Think(); take_forks(i); Eat(); put_forks(i); } }

Putting down forks

// only called with mutex set!test(int i) {if (state[i] == HUNGRY &&

state[LEFT] != EATING &&state[RIGHT] != EATING){state[i] = EATING; up(sem[i]); } }

int state[N] semaphore mutex = 1semaphore sem[i] put_forks(int i) {^ down(mutex);^ state [i] = THINKING;test(LEFT);test(RIGHT);^ up(mutex); }

Dining philosophers^ ^ Is the previous solution correct?^ ^ What does it mean for it to be correct?^ ^ Is there an easier way?

The sleeping barber problem^ ^ Barber:

^ While there are people waiting for a hair cut,put one in the barber chair, and cut their hair ^ When done, move to the next customer ^ Else go to sleep, until someone comes in

^ Customer:

^ If barber is asleep wake him up for a haircut ^ If someone is getting a haircut wait for thebarber to become free by sitting in a chair ^ If all chairs are all full, leave the barbershop

Designing a solution^ ^ How will we model the barber and customers?^ ^ What state variables do we need?

^ .. and which ones are shared? ^ …. and how will we protect them?

^ How will the barber sleep? ^ How will the barber wake up? ^ How will customers wait? ^ What problems do we need to look out for?

The readers and writers problem^ ^ Multiple readers and writers want to access adatabase (each one is a thread)^ ^ Multiple readers can proceed concurrently^ ^ Writers must synchronize with readers andother writers

^ only one writer at a time! ^ when someone is writing, there must be no readers!

Goals:

^ Maximize concurrency. ^ Prevent starvation.

Designing a solution^ ^ How will we model the barber and customers?^ ^ What state variables do we need?

^ .. and which ones are shared? ^ …. and how will we protect them?

^ How will the barber sleep? ^ How will the barber wake up? ^ How will customers wait? ^ What problems do we need to look out for?