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

An in-depth exploration of process synchronization techniques, focusing on semaphores, monitors, and condition variables. Topics include weak and strong semaphores, the bounded-buffer problem, readers-writers problem, dining philosophers problem, and monitor initialization code. The document also covers problems with semaphores and the use of condition variables in solving the dining philosophers problem.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

baidehi
baidehi 🇮🇳

4.4

(14)

101 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 12
Chapter 6: Process Synchronization (cont)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

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; }

}