1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.

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.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Classic Synchronization Problems.
Classic Synchronization Problems
Classical Problems of Concurrency
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
1 OMSE 510: Computing Foundations 6: Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
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 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 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
1 OMSE 510: Computing Foundations 7: More IPC & Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
Process Synchronization
Jonathan Walpole Computer Science Portland State University
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.
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
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.
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.
18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
CS510 Concurrent Systems Introduction to Concurrency.
CS533 Concepts of Operating Systems Jonathan Walpole.
Outline Monitors Barrier synchronization The sleeping barber problem
© 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.
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.
Synchronization, part 3 Monitors, classical sync. problems
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
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.
CSC321 Concurrent Programming: §4 Semaphores 1 Section 4 Semaphores.
CS533 Concepts of Operating Systems Jonathan Walpole.
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.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Interprocess Communication Race Conditions
Classical IPC Problems
Announcement server: server.o common.o $(CC) -o server server.o common.o $(LIBS) -lsock -lpthread and change it to: server: server.o common.o threadpool.o.
Dining Philosophers Five philosophers sit around a table
Process Synchronization
Classical Synchronization Problems
2.4 Classic IPC Problems Dining philosophers Readers and writers
Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
Chapter 5: Process Synchronization (Con’t)
Jonathan Walpole Computer Science Portland State University
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
CS399 New Beginnings Jonathan Walpole.
Critical section problem
CS510 Operating System Foundations
More IPC B.Ramamurthy 4/15/2019.
Chapter 7: Synchronization Examples
Oregon Health and Science University
Jonathan Walpole Computer Science Portland State University
Presentation transcript:

1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University

2 Classical IPC problems  Producer Consumer (bounded buffer)  Dining philosophers  Sleeping barber  Readers and writers

3 Producer consumer problem  Also known as the bounded buffer problem 8 Buffers InP OutP Consumer Producer Producer and consumer are separate threads

4 Is this a valid solution? thread producer { while(1){ // Produce char c while (count==n) { no_op } buf[InP] = c InP = InP + 1 mod n count++ } thread consumer { while(1){ while (count==0) { no_op } c = buf[OutP] OutP = OutP + 1 mod n count-- // Consume char } n-1 … Global variables: char buf[n] int InP = 0 // place to add int OutP = 0 // place to get int count

5 0 thread consumer { 1 while(1) { 2 while (count==0) { 3 sleep(empty) 4 } 5 c = buf[OutP] 6 OutP = OutP + 1 mod n 7 count--; 8 if (count == n-1) 9 wakeup(full) 10 // Consume char 11 } 12 } How about this? 0 thread producer { 1 while(1) { 2 // Produce char c 3 if (count==n) { 4 sleep(full) 5 } 6 buf[InP] = c; 7 InP = InP + 1 mod n 8 count++ 9 if (count == 1) 10 wakeup(empty) 11 } 12 } n-1 … Global variables: char buf[n] int InP = 0 // place to add int OutP = 0 // place to get int count

6 Does this solution work? 0 thread producer { 1 while(1){ 2 // Produce char c... 3 down(empty_buffs) 4 buf[InP] = c 5 InP = InP + 1 mod n 6 up(full_buffs) 7 } 8 } 0 thread consumer { 1 while(1){ 2 down(full_buffs) 3 c = buf[OutP] 4 OutP = OutP + 1 mod n 5 up(empty_buffs) 6 // Consume char... 7 } 8 } Global variables semaphore full_buffs = 0; semaphore empty_buffs = n; char buff[n]; int InP, OutP;

7 Producer consumer problem  What is the shared state in the last solution?  Does it apply mutual exclusion? If so, how? 8 Buffers InP OutP Consumer Producer Producer and consumer are separate threads

8 Dining philosophers problem  Five philosophers sit at a table  One fork between each philosopher  Why do they need to synchronize?  How should they do it? while(TRUE) { Think(); Grab first fork; Grab second fork; Eat(); Put down first fork; Put down second fork; } Each philosopher is modeled with a thread

9 Is this a valid solution? #define N 5 Philosopher() { while(TRUE) { Think(); take_fork(i); take_fork((i+1)% N); Eat(); put_fork(i); put_fork((i+1)% N); }

10 Working towards a solution … #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)

11 Working towards a solution … #define N 5 Philosopher() { while(TRUE) { Think(); take_forks(i); Eat(); put_forks(i); }

12 Picking up forks // only called with mutex set! test(int i) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; up(sem[i]); } int state[N] semaphore mutex = 1 semaphore sem[i] take_forks(int i) { down(mutex); state [i] = HUNGRY; test(i); up(mutex); down(sem[i]); }

13 Putting down forks // only called with mutex set! test(int i) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; up(sem[i]); } int state[N] semaphore mutex = 1 semaphore sem[i] put_forks(int i) { down(mutex); state [i] = THINKING; test(LEFT); test(RIGHT); up(mutex); }

14 Dining philosophers  Is the previous solution correct?  What does it mean for it to be correct?  Is there an easier way?

15 The sleeping barber problem

16 The sleeping barber problem  Barber:  While there are people waiting for a hair cut, put one in the barber chair, and cut their hair  When done, move to the next customer  Else go to sleep, until someone comes in  Customer:  If barber is asleep wake him up for a haircut  If someone is getting a haircut wait for the barber to become free by sitting in a chair  If all chairs are all full, leave the barbershop

17 Designing a solution  How will we model the barber and customers?  What state variables do we need? .. and which ones are shared?  …. and how will we protect them?  How will the barber sleep?  How will the barber wake up?  How will customers wait?  What problems do we need to look out for?

18 Is this a good solution? Barber Thread: while true Down(customers) Lock(lock) numWaiting = numWaiting-1 Up(barbers) Unlock(lock) CutHair() endWhile Customer Thread: Lock(lock) if numWaiting < CHAIRS numWaiting = numWaiting+1 Up(customers) Unlock(lock) Down(barbers) GetHaircut() else -- give up & go home Unlock(lock) endIf const CHAIRS = 5 var customers: Semaphore barbers: Semaphore lock: Mutex numWaiting: int = 0

19 The readers and writers problem  Multiple readers and writers want to access a database (each one is a thread)  Multiple readers can proceed concurrently  Writers must synchronize with readers and other writers  only one writer at a time !  when someone is writing, there must be no readers ! Goals:  Maximize concurrency.  Prevent starvation.

20 Designing a solution  How will we model the readers and writers?  What state variables do we need? .. and which ones are shared?  …. and how will we protect them?  How will the writers wait?  How will the writers wake up?  How will readers wait?  How will the readers wake up?  What problems do we need to look out for?

21 Is this a valid solution to readers & writers? Reader Thread: while true Lock(mut) rc = rc + 1 if rc == 1 Down(db) endIf Unlock(mut)... Read shared data... Lock(mut) rc = rc - 1 if rc == 0 Up(db) endIf Unlock(mut)... Remainder Section... endWhile var mut: Mutex = unlocked db: Semaphore = 1 rc: int = 0 Writer Thread: while true...Remainder Section... Down(db)...Write shared data... Up(db) endWhile

22 Readers and writers solution  Does the previous solution have any problems?  is it “fair”?  can any threads be starved? If so, how could this be fixed?  … and how much confidence would you have in your solution?

23 Quiz  When faced with a concurrent programming problem, what strategy would you follow in designing a solution?  What does all of this have to do with Operating Systems?