Dining Philosophers Five philosophers sit around a table

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.
Operating Systems Mehdi Naghavi Winter 1385.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 14: Deadlock & Dinning Philosophers.
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.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Cosc 4740 Chapter 6, Part 2 Process Synchronization.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
Deadlock CS Introduction to Operating Systems.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Classic Synchronization Problems.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Classic Synchronization Problems
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.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
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.
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
Process Synchronization
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Classical IPC and Synchronization Problems CS 342 – Operating Systems Ibrahim.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Jonathan Walpole Computer Science Portland State University
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,
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
1 Sleeping Barber Problem There is one barber, and n chairs for waiting customers If there are no customers, then the barber sits in his chair and sleeps.
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.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Synchronisation Examples
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.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
Classical IPC Problems
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization
Deadlock and Starvation
2.4 Classic IPC Problems Dining philosophers Readers and writers
Chapter 6-7: Process Synchronization
Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
Chapter 6: Process Synchronization
Lecture 16: Readers-Writers Problem and Message Passing
Chapter 5: Process Synchronization (Con’t)
תרגול 3 – תהליכים, תאום תהליכים
Practical Session 12 Deadlocks
Lecture 25 Syed Mansoor Sarwar
CS399 New Beginnings Jonathan Walpole.
Critical section problem
Chapter 7: Synchronization Examples
CS510 Operating System Foundations
More IPC B.Ramamurthy 4/15/2019.
Presentation transcript:

Dining Philosophers Five philosophers sit around a table with five forks and spaghetti to eat. Philosophers think for a while and they want to eat, only spaghetti, for a while. To eat a philosopher requires two forks, one from the left and one from right. Assume a philo. can only pick up one fork at a time. After eating, forks are placed down and philo. goes back to thinking.

Dining Philosophers Devise an algorithm that will allow philosophers to eat. Must satisfy mutual exclusion - no two philos can use the same fork at the same time. Avoid deadlock and starvation!

Dining Philosophers First attempt: take left fork, then take right fork Wrong! Results in deadlock. Second attempt: take left fork, check to see if right is available, if not put left one down. Still has race condition and can lead to starvation. Change so wait a random time. Will work usually, but want a solution that will always work, guaranteed.

Dining Philosophers One solution is to protect obtaining forks with a binary semaphore (mutex) - but only one philo can eat at a time rather than two. Following code uses one semaphore per philo and states of eating, thinking, and hungry. A philo can advance to eating if neither neighbor is eating.

Dining Philosophers #define N 5 /* number of philos */ #define LEFT (i+N-1)%N /* # of i's left */ #define RIGHT (i+1)%N /* # of i's right */ #define THINKING 0 #define HUNGRY 1 #define EATING 2 int state[N]; /* keep track of state */ semaphore mutex = 1; semaphore s[N]; /* semaphore per philo */

Dining Philosophers void philosopher(int i) { while (TRUE) { think(); /* thinking */ take_forks(i); /* get two forks, block */ eat(); /* eating */ put_forks(i); /* give up forks */ }

Dining Philosophers void take_forks(int i) { wait(mutex); state[i] = HUNGRY; test(i); // try getting 2 forks signal(mutex); if state[i] != EATING wait(s[i]); // block if no forks acquired }

Dining Philosophers void test(int i) { if ( state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING ) { state[i] = EATING; signal(s[i]); }

Dining Philosophers void put_forks(int i) { wait(mutex); state[i] = THINKING; test(LEFT); test(RIGHT); signal(mutex); }

Dining Philosophers Another solution is to have some philos be left handed first. Others: - get more forks, learn to eat with one fork. (changes the problem) - only let four at a time to the table

Readers/Writers Models access to a database. Some processes want to read the DB others write it. Many readers can read at the same time. But when writing mutual exclusion is needed. Following code could shut out writers if enough readers.

Readers/Writers semaphore mutex = 1; semaphore db = 1; int rc = 0; void writer() { while (TRUE) { think_up_data(); wait(db); write_data_base(); signal(db); } }

Readers/Writers void reader() { while (TRUE) { wait(mutex); rc = rc + 1; if ( rc == 1 ) wait(db); signal(mutex); read_data_base(); wait(mutex); rc = rc – 1; if ( rc == 0 ) signal(db); use_data_read(); } }

Sleeping Barber Barber shop with one barber, one barber chair and N chairs to wait in. When no customers the barber goes to sleep in barber chair and must be woken when a customer comes in. When barber is cutting hair new customers take empty seats to wait, or leave if no vacancy. Program barber and customers so no race condition exists.

Sleeping Barber The following code uses 3 semaphores. - customers – number of waiting custs - barbers – number of barbers (0 or 1) that are idle - mutex

Sleeping Barber #define CHAIRS 5 /* # of chairs */ semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;

Sleeping Barber void barber() { while (TRUE) { wait(customers); wait(mutex); waiting = waiting – 1; signal(barbers); signal(mutex); cut_hair(); }

Sleeping Barber void customer() { wait(mutex); if ( waiting < CHAIRS ) { waiting = waiting + 1; signal(customers); signal(mutex); wait(barbers); get_haircut(); } else { signal (mutex); } }