Operating Systems ECE344 Ashvin Goel ECE University of Toronto Classic Synchronization Problems.

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 Mehdi Naghavi Winter 1385.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 14: Deadlock & Dinning Philosophers.
CSEN5322 Quiz-5.
Chapter 6: Process Synchronization
Cosc 4740 Chapter 6, Part 2 Process Synchronization.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
CONDITION VARIABLE. Announcements  Quiz  Getting the big picture  Programming assignments  Cooperation  Lecture is cut 20 mins short for Quiz and.
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.
1 OMSE 510: Computing Foundations 7: More IPC & Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
Monitors CSCI 444/544 Operating Systems Fall 2008.
IPC and Classical Synchronization Problems
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
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.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
© 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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Today’s Agenda  Quick Review  Semaphore and Lock Advanced Topics in Software Engineering 1.
Operating Systems Inter-Process Communications. Lunch time in the Philosophy Department. Dining Philosophers Problem (1)
Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.
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.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
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.
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Synchronization Deadlocks and prevention
Classical IPC Problems
Dining Philosophers Five philosophers sit around a table
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
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 5: Process Synchronization (Con’t)
Lecture 15: Dining Philosophers Problem
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
Module 7a: Classic Synchronization
CS399 New Beginnings Jonathan Walpole.
Chapter 7: Synchronization Examples
CS510 Operating System Foundations
Lecture 15: Dining Philosophers Problem
Chapter 7: Synchronization Examples
Switching to Blackboard
, Part II Process Synchronization
Presentation transcript:

Operating Systems ECE344 Ashvin Goel ECE University of Toronto Classic Synchronization Problems

Classic Synchronization Examples  Dining philosophers  Readers and writers 2

The Dining Philosophers Problem  Five philosophers sit at a table  A fork lies between every pair of philosophers  Philosophers (1) think, (2) grab one fork, (3) grab another fork, (4) eat, (5) put down one fork, (6) put the other fork 3

Dining Philosophers: Try 1  Assume take_fork and put_fork have locks in them to make them atomic o Is this solution correct? 4 #define N 5 Philosopher() { while(TRUE) { Think(); take_fork(i); take_fork((i+1)% N); Eat(); put_fork(i); put_fork((i+1)% N); } Each philosopher is modeled with a thread

Dining Philosophers: Try 2 5 #define N 5 Philosopher() { while(TRUE) { Think(); take_fork(i); take_fork((i+1)% N); Eat(); put_fork(i); put_fork((i+1)% N); } take_forks(i) put_forks(i)

Take Forks 6 // test whether philosopher i // can take both forks // only called with mutex set! test(int i) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; // Signal Philosopher i V(sem[i]); } int state[N]; lock mutex; sem sem[N] = {0}; take_forks(int i) { lock(mutex); state[i] = HUNGRY; test(i); unlock(mutex); P(sem[i]); }

Put Forks 7 // test whether philosopher i // can take both forks // only called with mutex set! test(int i) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; // Signal Philosopher i V(sem[i]); } put_forks(int i) { lock(mutex); state[i] = THINKING; test(LEFT); test(RIGHT); unlock(mutex); } int state[N]; lock mutex; sem sem[N] = {0};

The Readers and Writers Problem  Multiple reader and writer threads want to access some shared data  Multiple readers can read concurrently  Writers must synchronize with readers and other writers  Synchronization requirements o Only one writer can write the shared data at a time o When a writer is writing, no readers must access the data  Goals o Maximize concurrency o Prevent starvation 8

Readers/Writers - Basics 9 Reader () { rc = rc + 1; // Read shared data rc = rc – 1; // non-critical section } Mutex lock = UNLOCKED; Semaphore data = 1; int rc = 0; Writer () { // non-critical section // Write shared data }

Readers/Writers - Mutex 10 Reader () { lock(lock); rc = rc + 1; unlock(lock); // Read shared data lock(lock); rc = rc – 1; unlock(lock); // non-critical section } Mutex lock = UNLOCKED; Semaphore data = 1; int rc = 0; Writer () { // non-critical section // Write shared data }

Readers/Writers – Synchronization  Any problems with this solution? 11 Reader () { lock(lock); if (rc == 0) P(data); rc = rc + 1; unlock(lock); // Read shared data lock(lock); rc = rc – 1; if (rc == 0) V(data); unlock(lock); // non-critical section } Mutex lock = UNLOCKED; Semaphore data = 1; int rc = 0; Writer () { // non-critical section P(data); // Write shared data V(data); }