















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
Classical inter-process communication (ipc) problems in computer science, including the producer-consumer problem, dining philosophers problem, sleeping barber problem, and readers-writers problem. These problems illustrate the challenges of synchronizing concurrent processes and threads, and the need for mutual exclusion and semaphore-based solutions.
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Jonathan WalpoleComputer Science Portland State University
// 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
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 and consumerare separate threads
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; }
#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)
#define N 5Philosopher() {while(TRUE) {
Think(); take_forks(i); Eat(); put_forks(i); } }
// 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); }
^ Customer: