Docsity

OS: High-Level Sync Mechanisms & Dining Philosophers Problem - Prof. Shivakant Mishra, Study notes of Operating Systems

Toggle sidebar
bg1
CSCI 3753
Operating Systems
Higher level synchronization
mechanisms
Lecture Notes By
Shivakant Mishra
Computer Science, CU-Boulder
Last Update: 02/21/06
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
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download OS: High-Level Sync Mechanisms & Dining Philosophers Problem - Prof. Shivakant Mishra and more Study notes Operating Systems in PDF only on Docsity!

CSCI 3753

Operating Systems

Higher level synchronization

mechanisms Lecture Notes By Shivakant Mishra

Computer Science, CU-Boulder

Last Update: 02/21/

Higher level synchronization

mechanisms

  • TS(), sleep() and wakeup(), and semaphores

are low-level synchronization primitives^ – Non-intuitive: difficult to develop solutions^ – Difficult to debug^ – Easy to make mistakes

Dining Philosophers Problem

Quiz: Write a synchronization schema for the problem

First Try

philosopher(int i) {

while(TRUE) {

// Think // Eat P(fork[i]);

P(fork[(i+1) mod 5]);

eat(); V(fork[(i+1) mod 5]); V(fork[i]); } } semaphore fork[5] = (1,1,1,1,1);

  • On a fateful day, all philosophers decide to

eat at the same time.

  • All philosophers pick up their right fork. • All philosophers now wait forever for their

left fork to become available, and die ofstarvation^ – Deadlock.

philosopher(int i) {

while(TRUE) {

// Think // Eat P(mutex);

P(fork[i]); P(fork[(i+1) mod 5]); V(mutex);

eat(); V(fork[(i+1) mod 5]); V(fork[i]); } } semaphore fork[5] = (1,1,1,1,1); semaphore mutex = 1;

  • Problem: May not allow two non-adjacent

philosophers to eat at the same time.

One Answer

philosopher(int i) {

while(TRUE) {

// Think // Eat j = i % 2; P(fork[(i+j) mod 5]);

P(fork[(i+1-j) mod 5]);

eat(); V(fork[(i+1-j) mod 5]); V(fork[[(i+j) mod 5]); } } semaphore fork[5] = (1,1,1,1,1);

  • An odd numbered philosopher first picks up

her left fork and then her right fork.

  • An even numbered philosopher first picks

up his right fork and then his left fork.

  • This solution works, because adjacent

philosophers pick up their forks in differentorder.

AND Synchronization

  • Given two resources: R

and R 1

2

  • Some processes access R

, some R 1

, some 2

both in the same critical section

  • Need to avoid deadlock due to ordering of P

operations^ – As happened in our first try for dining

philosophers problem.

  • Simultaneous P

– P

simultaneous

(S^1

, …, S

)n

  • Obtain all semaphores at once or none of themat all. – The operation blocks a calling thread wheneverany of the semaphores can not be obtained.
    • Implementation of simultaneous P is tricky
      • Reading assignment:
        • Figure 9.4 Simultaneous Semaphores (Page339)

Events

  • Exact definition is specific to each OS • A process can

wait

on an event until another

process

signals

the event

  • Have

event descriptor

(“event control block”)

  • Active approach
    • Multiple processes can wait on an event – Exactly one process is unblocked when a signaloccurs – A signal with no waiting process is ignored
      • May have a

queue

function that returns

number of processes waiting on the event

UNIX Signals

  • A UNIX signal corresponds to an event
    • It is

raised

by one process (or hardware) to call

another process’s attention to an event – It can be

caught

(or ignored) by the subject

process

  • Justification for including signals was for

the OS to inform a user process of an event^ – User pressed

delete

key

  • Program tried to divide by zero – Attempt to write to a nonexistent pipe – etc.