Top Banner
CISC 3320 C22a Process Synchronization: Classical Problems Hui Chen Department of Computer & Information Science CUNY Brooklyn College 4/10/2019 1 CUNY | Brooklyn College
27

CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Aug 11, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

CISC 3320

C22a Process Synchronization: Classical

ProblemsHui Chen

Department of Computer & Information Science

CUNY Brooklyn College

4/10/2019 1CUNY | Brooklyn College

Page 2: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Acknowledgement

• These slides are a revision of the slides provided by the authors of the textbook via the publisher of the textbook

4/10/2019 CUNY | Brooklyn College 2

Page 3: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Classical Problems of Synchronization• Classical problems used to test newly-

proposed synchronization schemes

• Bounded-Buffer Problem

• Readers and Writers Problem

• Dining-Philosophers Problem

4/10/2019 CUNY | Brooklyn College 3

Page 4: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Bounded-Buffer Problem

• n buffers, each can hold one item

• A producer produces an item and inserts to the buffer

• A consumer removes an item from the buffer and consumes it.

4/10/2019 CUNY | Brooklyn College 4

Page 5: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Solution to Bounded-Buffer Problem• Example solution using semaphores

• A binary semaphore and two countingsemaphores

• Semaphore mutex initialized to the value 1

• Semaphore full initialized to the value 0

• Semaphore empty initialized to the value n

4/10/2019 CUNY | Brooklyn College 5

Page 6: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Producer Processwhile (true) {

...

/* produce an item in next_produced */

...

wait(empty);

wait(mutex);

...

/* add next produced to the buffer */

...

signal(mutex);

signal(full);

}

4/10/2019 CUNY | Brooklyn College 6

Page 7: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Consumer Processwhile (true) {

wait(full);

wait(mutex);

...

/* remove an item from buffer to next_consumed */

...

signal(mutex);

signal(empty);

...

/* consume the item in next consumed */

...

}

4/10/2019 CUNY | Brooklyn College 7

Page 8: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Questions?

• Producer-consumer problem

• Example solution using semaphores

4/10/2019 CUNY | Brooklyn College 8

Page 9: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Readers-Writers Problem

• A data set is shared among a number of concurrent processes

• Readers – only read the data set; they do notperform any updates

• Writers – can both read and write

• Problem

• Allow multiple readers to read at the same time

• Allow only one single writer to access the shared data at the same time, and

• Variations

4/10/2019 CUNY | Brooklyn College 9

Page 10: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Variations of Readers-Writers Problem• The first readers–writers problem

• The second readers–writers problem, and

• Other variations

4/10/2019 CUNY | Brooklyn College 10

Page 11: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

The First Readers–Writers Problem• Requires that no reader be kept waiting

unless a writer has already obtained permission to use the shared object.

• In other words, no reader should wait for other readers to finish simply because a writer is waiting.

4/10/2019 CUNY | Brooklyn College 11

Page 12: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

The Second Readers–Writers Problem• Requires that, once a writer is ready, that

writer perform its write as soon as possible.

• In other words, if a writer is waiting to access the object, no new readers may start reading.

4/10/2019 CUNY | Brooklyn College 12

Page 13: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Solution to the First Readers–Writers Problem• Shared Data

• Data set

• Semaphore rw_mutex initialized to 1

• Semaphore mutex initialized to 1

• Integer read_count initialized to 0

4/10/2019 CUNY | Brooklyn College 13

Page 14: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Writer P rocess

while (true) {

wait(rw_mutex);

...

/* writing is performed */

...

signal(rw_mutex);

}

4/10/2019 CUNY | Brooklyn College 14

Page 15: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Reader Processwhile (true){

wait(mutex);

read_count++;

if (read_count == 1) wait(rw_mutex);

signal(mutex);

...

/* reading is performed */

...

wait(mutex);

read count--;

if (read_count == 0) signal(rw_mutex);

signal(mutex);

}

4/10/2019 CUNY | Brooklyn College 15

Page 16: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Readers-Writers Problem Variations• First variation – no reader kept waiting

unless writer has permission to use shared object

• Second variation – once writer is ready, it performs the write ASAP

• Both may have starvation leading to even more variations

• Problem is solved on some systems by kernel providing reader-writer locks

4/10/2019 CUNY | Brooklyn College 16

Page 17: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Questions?

• The Readers-Writers problem

• Variations of The Readers-Writers problem

• Solution to the First Readers-Writersproblem.

4/10/2019 CUNY | Brooklyn College 17

Page 18: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Dining-Philosophers Problem

• Philosophers spend their lives alternating thinking and eating

• Don’t interact with their neighbors

• Occasionally try to pick up 2 chopsticks, one at a time, to eat from bowl

• Need both to eat, then release both when done

• Starvation and deadlock

4/10/2019 CUNY | Brooklyn College 18

Page 19: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

4/10/2019 CUNY | Brooklyn College 19

Page 20: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Solution to Dining-Philosophers Problem• Assume there are 5 philosophers

• Shared data

• Bowl of rice (data set)

• Semaphore chopstick [5] initialized to 1

4/10/2019 CUNY | Brooklyn College 20

Page 21: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

The Structure of Philosopher i

1. while (true){

2. wait (chopstick[i] );

3. wait (chopStick[ (i + 1) % 5] );

4. /* eat for awhile */

5. signal (chopstick[i] );

6. signal (chopstick[ (i + 1) % 5] );

7. /* think for awhile */

8. }

• What is the problem with this algorithm?4/10/2019 CUNY | Brooklyn College 21

Page 22: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Deadlock May Happen

• What if Process i and (i+1) both completes Line 2?

4/10/2019 CUNY | Brooklyn College 22

Page 23: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Monitor Solution to Dining Philosophers• A deadlock free solution

4/10/2019 CUNY | Brooklyn College 23

Page 24: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Monitor Solutionmonitor DiningPhilosophers

{

enum { THINKING; HUNGRY, EATING) state [5] ;

condition self [5];

void pickup (int i) {

state[i] = HUNGRY;

test(i);

if (state[i] != EATING) self[i].wait;

}

void putdown (int i) {

state[i] = THINKING;

// test left and right neighbors

test((i + 4) % 5);

test((i + 1) % 5);

}

4/10/2019 CUNY | Brooklyn College 24

Page 25: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Monitor Solutionvoid test (int i) {

if ((state[(i + 4) % 5] != EATING) &&

(state[i] == HUNGRY) &&

(state[(i + 1) % 5] != EATING) ) {

state[i] = EATING ;

self[i].signal () ;

}

}

initialization_code() {

for (int i = 0; i < 5; i++)

state[i] = THINKING;

}

}

4/10/2019 CUNY | Brooklyn College 25

Page 26: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Starvation• Each philosopher i invokes the operations pickup() and putdown() in the following sequence:

DiningPhilosophers.pickup(i);

/** EAT **/

DiningPhilosophers.putdown(i);

• No deadlock, but starvation is possible• Dealing with starvation?

4/10/2019 CUNY | Brooklyn College 26

Page 27: CISC 3320 C22a Process Synchronization: Classical Problems · Classical Problems of Synchronization •Classical problems used to test newly-proposed synchronization schemes •Bounded-Buffer

Questions?

• Bounded-Buffer Problem

• Readers and Writers Problem

• Dining-Philosophers Problem

• Semaphore solution

• Monitor solution

• Deadlock and starvation

4/10/2019 CUNY | Brooklyn College 27