Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Synchronization Principles Gordon College Stephen Brinton.
Classical Problems of Concurrency
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit.
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Chapter 6 Semaphores.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Synchronisation Examples
Fall 2000M.B. Ibáñez Lecture 08 High Level mechanisms for process synchronization Critical Regions Monitors.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
Deadlock and Starvation
Interprocess Communication Race Conditions
Dining Philosophers Five philosophers sit around a table
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Process Synchronization
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
Concurrency: Mutual Exclusion and Process Synchronization
Presentation transcript:

Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1

Relevance Dining philosopher’s problem models a large class of concurrency-control problems Highlights need for freedom from deadlock and starvation Early versions of FreeBSD failed to address this problem right, and hence parallelism was severely impacted

Simple and Obvious solution do { wait(fork[i]) wait(fork[(i+1) % 5]) … eat … signal(fork[i]); signal(fork[(i+1) % 5]); … think … } while (1); Problems with the solution?

Problems with the solution Not dead-lock free A Second Alternative – Use a back-off time – Problem? A Third Alternative – Use a random back-off time – Problem? Yet Another Alternative – Binary Semaphore – FreeBSD – Problem?

Tanenbaum’s solution (1/4) Datastructures – 3 States of phil are defined THINKING, HUNGRY, EATING – 2 Semaphores mutex – binary semaphore – Provides mutual exclusion in critical region – Initial value = 1 s[5] – array of semaphores – One semaphore per phil – Initial value=0 – Left Neighbor of ‘i’ (i+4)%5 – Right Neighbor of ‘i’ (i+1)%5

Tanenbaum’s solution (2/4) Philosopher process 'i‘ while(1) { think(); take_forks(i); /* Acquire both the forks or block */ eat(); put_forks(i); /* Put back both the forks on table */ }

Tanenbaum’s solution (3/4) Philosopher 'i' taking the fork wait(mutex);/* Enter critical section */ state[i]='HUNGRY'; test(i);/* Try to acquire 2 forks */ signal(mutex);/* Exit critical section */ wait(s[i]);/* Block if forks were not acquired */ Test procedure Test(i) { if ( state[i] == HUNGRY && state[(i+4)%5] != EATING && state[(i+1)%5] != EATING ) { state[i] = EATING; signal(s[i]); }

Tanenbaum’s solution (4/4) Philosopher 'i' putting down the fork wait(mutex);/* Enter critical section */ state[i]='THINKING';/* Phil has finished eating */ test( (i+4)%5 );/* See if left neighbour can now eat */ test( (i+1)%5 );/* See if right neighbour can now eat */ signal(mutex);/* Exit critical section */ This solution is deadlock free Provides for maximum parallelism when extended to any arbitrary ‘N’

Monitors Why Monitors? – Semaphores demand strict sequencing which are difficult to implement even in relatively simple scenarios – Monitors provide high level constructs that help write correct programs – Monitor is a collection of procedures, variables and data structures grouped together in a special kind of module – Processes may call procedures in a monitor but cannot directly access the data internal to a monitor from procedures declared outside a monitor (private data and public methods as in Java)

Bounded buffer Var f,e,s :semaphore (:=0); (In the begning s=1,f=0,e=n ) Producer Begin repeat produce; wait (e) wait(s) append; Signal(s); signal (f); forever End; Consumer Begin repeat wait(f); wait(s); take; signal(s); signal (e); consume; forever End;

Monitors – General Structure monitor monitor-name { //shared variable declarations procedure P1(...){... } procedure p2(...){... }. procedure pn(...){... } initialisation code(...){... } }//end of monitor

Monitors How do monitors achieve mutual exclusion? – Only one process is active in a monitor at any instant – Compiler handles calls to monitor procedures differently from other procedure calls – Compiler implements the mutual exclusion – Why is this an advantage over semaphores?? How to code monitors? – Just turn each critical region into a monitor procedure

Condition variables Why need them? – Provide means by which processes can block when they cannot proceed in a monitor Condition variables have two operations associated with them – wait and signal When a process cannot proceed it calls wait on some condition variable, X This could cause another process to enter the monitor and raise a signal on the same condition variable, X

What happens after a signal? Hoare’s Method – Run the newly awakened process Hansen’s Method – Process doing the signal must exit monitor immediately – how? – If signal is on a condition variable on which several processes are waiting? Third solution – Signaler runs and allow waiting process to start running only after signaler exits monitor

How is this different from Semaphore? Condition variables are not counters If a condition variable is signalled with no one waiting on it, the signal is lost

Monitor solution for Bounded Buffer