Docsity

Process Synchronization: Semaphores, Monitors, and Condition Variables, Slides of Operating Systems

Partial preview of the text

Download Process Synchronization: Semaphores, Monitors, and Condition Variables and more Slides Operating Systems in PDF only on Docsity!

Lecture 12

Chapter 6: Process Synchronization (cont)

Chapter 6: Process Synchronization

• Background

• The Critical-Section Problem

• Peterson’s Solution

• Synchronization Hardware

• Semaphores

• Classic Problems of Synchronization

• Monitors

• Synchronization Examples

• Atomic Transactions

Starvation – indefinite blocking.

A process may never be removed from the semaphore queue in

which it is suspended

 Order of arrival retainment:

 Weak semaphores:

 The thread that will access the critical region next is selected randomly

Starvation is possible

 Strong semaphores:

 The thread that will access the critical region next is selected based on its arrival time, e.g. FIFO

Starvation is not possible

Starvation

Classical Problems of Synchronization

  • Bounded-Buffer Problem
  • Readers and Writers Problem
  • Dining-Philosophers Problem

Bounded Buffer Problem (Cont.)

  • The structure of the producer process

do {

// produce an item in nextp

wait (empty); wait (mutex);

// add the item to the buffer

signal (mutex); signal (full); } while (TRUE);

Bounded Buffer Problem (Cont.)

  • The structure of the consumer process

do { wait (full); wait (mutex);

// remove an item from buffer to nextc

signal (mutex); signal (empty);

// consume the item in nextc

} while (TRUE);

Readers-Writers Problem (Cont.)

• The structure of a writer process

do {

wait (wrt) ;

// writing is performed

signal (wrt) ;

} while (TRUE);

Readers-Writers Problem (Cont.)

  • The structure of a reader process

do { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex)

// reading is performed

wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; } while (TRUE);

Dining-Philosophers Problem

(Cont.)

• The structure of Philosopher i :

do {

wait ( chopstick[i] );

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

// eat

signal ( chopstick[i] );

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

// think

Problems with Semaphores

• Correct use of semaphore operations:

– signal (mutex) …. wait (mutex)

– wait (mutex) … wait (mutex)

– Omitting of wait (mutex) or signal

(mutex) (or both)

Condition Variables

• condition x, y;

• Two operations on a condition variable:

– x.wait () – a process that invokes the operation

is suspended.

– x.signal () – resumes one of processes (if any)

that invoked x.wait ()

Solution to Dining Philosophers

  • Each philosopher I invokes the operations pickup() and putdown() in the following

sequence:

DiningPhilosophters.pickup (i); EAT DiningPhilosophers.putdown (i);

monitor DP

{

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

initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; }

A Monitor to Allocate Single

monitor ResourceAllocator Resource

{

boolean busy; condition x;

void acquire(int time) { if (busy) x.wait(time); busy = TRUE; }

void release() { busy = FALSE; x.signal(); }

initialization code() { busy = FALSE; }

}