CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Classical IPC and Synchronization Problems CS 342 – Operating Systems Ibrahim.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Operating Systems Mehdi Naghavi Winter 1385.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 14: Deadlock & Dinning Philosophers.
Chapter 6: Process Synchronization
© 2004, D. J. Foreman 1 The Dining Philosophers Problem.
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.
CS 149: Operating Systems February 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
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
Yet another synchronization problem
Classical Problems of Concurrency
Interprocess Communication
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
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.
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
Monitors CSCI 444/544 Operating Systems Fall 2008.
IPC and Classical Synchronization Problems
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
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.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Classical IPC Problems
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Synchronization II: CPE Operating Systems
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.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.
Synchronisation Examples
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Dinning Philosophers, and Misc.
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.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
Deadlock and Starvation
Yet another synchronization problem
Dining Philosophers Five philosophers sit around a table
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
Classical Synchronization Problems
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
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
CS510 Operating System Foundations
More IPC B.Ramamurthy 4/15/2019.
Lecture 26 Syed Mansoor Sarwar
Switching to Blackboard
Presentation transcript:

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Classical IPC and Synchronization Problems CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University Computer Engineering Department

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 2 Dining Philosophers Problem 5 philosophers want to eat dinner (spaghetti) There are 5 plates and 5 forks on the table Each philosopher needs to use two forks to eat. When it want to read, he need to pickup left and right forks one at a time in any order.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 3 Dining Philosophers Problem Problem: Write a program for each philosopher that simulates the situation and never get stuck?  A philosopher is either thinking or eating.  Before it eats, it has two acquire two forks.

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 4 #define N 5 void philosopher (int i) { while (TRUE) { think(); /* just think – not eat */ take_fork(i); /* take left fork */ take_fork( i + 1 % N); / * take right fork */ eat(); /* eat the spagetti */ put_fork(i); put_fork(i + 1 % N); } Initial attempt for solution What if all five philosophers take their left fork at the same time? This solution does not work!

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 5 Improvement Use semaphore. Problem: only one philosopher can eat, although it is possible two of them to east simultaneously. #define N 5 semaphore mutex; void philosopher (int i) { while (TRUE) { think(); /* just think – not eat */ down (mutex); take_fork(i); /* take left fork */ take_fork( i + 1 % N); / * take right fork */ eat(); /* eat the spagetti */ put_fork(i); put_fork(i + 1 % N); up (mutex); }

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 6 Solution #define N 5 #define LEFT (i+N-1)%N /* number of i’s left neighbor */ #define RIGHT (i+1)%N /* number of i’s right neighbor */ #define THINKING 0 #define HUNGRY 1 #define EATING 2 typedef int semaphore; int state[N]; /* state of each philosopher */ semaphore mutex = 1; /*mutual exlusion for critical regions */ semaphore s[N]; /* one semaphore per philosopher */

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 7 void philosopher (int i) /* i is between 0 and N-1 */ { while (TRUE) { think(i); take_forks(i); /* acquire two forks or block */ eat(); put_forks(i); /* put both forks on the table */ } void take_forks (int i) { down(&mutex); /* enter critical region */ state[i] = HUNGRY; /* express interest to eat */ test(i); /* try to acquire two forks */ up(&mutex); down(&s[i]); /* block if forks were not acquired */ }

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 8 void put_forks (i) { down(&mutex); state[i] = THINKING; test[(LEFT]); test(RIGHT); up(&mutex); } void test (i) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); }

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 9 hungrythinkingeating hungrythinkingeating hungrythinkingeating hungrythinkingeating hungrythinkingeating