Synchronisation Examples

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.
Ch 7 B.
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 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
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
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.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
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.
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.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CS 346 – Sect Process synchronization –What is the problem? –Criteria for solution –Producer / consumer example –General problems difficult because.
© 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.
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.
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.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.
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.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chien-Chung Shen CIS/UD
Deadlock and Starvation
Interprocess Communication Race Conditions
Classical IPC Problems
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.
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
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Critical section problem
Chapter 7: Synchronization Examples
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Synchronization CSE 2431: Introduction to Operating Systems
Presentation transcript:

Synchronisation Examples Last time: Producer-Consumer using semaphores Today: some more examples Bounded Buffer Readers and Writers Dining Philosophers Sleeping Barber 159.302

What is the bounded buffer problem? This is just another name for the Producer-Consumer problem with a buffer. The solution uses two semaphores thingsinbuffer – initialised to zero spaceinbuffer – initialised to the buffer size The consumer waits on thingsinbuffer and signals spaceinbuffer after it has consumed. The producer waits on spaceinbuffer and signals thingsinbuffer after it has produced. 159.302

What is the Readers and Writers problem? Imagine a large database. Many processes are accessing the database Some only read (readers) and some read and write (writers). A Race conditions may exist where more than one writer is active or a writer and a reader is active. Any number of processes can read at the same time, but if a writer is writing then no other process must be able to access the data. 159.302

Solution to Readers and Writers Shared data semaphore mutex,norw; int readcount=0; mutex=create(1); norw=create(1); Writer process wait(norw); ... writing is performed ... signal(norw); Reader process wait(mutex); readcount = readcount + 1; if (readcount == 1) // first wait(norw); // one signal(mutex); ... reading is performed readcount = readcount - 1; if (readcount == 0) // first signal(norw); // one 159.302

Readers Writers Example 3 Processes At t=10 p1 read for 30ms At t=20 p2 read for 30ms At t=30 p3 write for 40ms At t=70 p1 read for 10ms At t=80 p2 read for 10ms 159.302

Readers Writers Example 1 -1 1 mutex 1 -1 -1 1 norw 1 2 1 1 2 readcount W P3 R R P2 R R P1 159.302

What is the dining philosophers problem? A Problem posed by Dijkstra in 1965. 5 people (philosophers) around a table each need 2 forks to eat. The philosophers alternately eat and think. 159.302

A Possible solution void philosopher(int no) { while(1) { ...think.... take_fork(no); /* get the left fork */ take_fork((no+1) % N); /* get the right fork */ ....eat..... put_fork(no); /* put left fork down */ put_fork((no+1) % N); /* put down right fork */ } "take_fork" waits until the specified fork is available and then grabs it. Unfortunately this solution will not work... what happens if all the philosophers grab their left fork at the same time. 159.302

A Better Solution Shared data Code int p[N]; /* status of the philosophers */ semaphore s[N]=0; /* semaphore for each philosopher */ semaphore mutex=1; /* semaphore for mutual exclusion */ Code #define LEFT(n) (n+N-1)%N /* Macros to give left */ #define RIGHT(n) (n+1)%N /* and right around the table */ void philosopher(int no) { while(1) { ...think.... take_forks(no); /* get the forks */ ....eat..... put_forks(no); /* put forks down */ } return NULL; 159.302

A Better Solution void test(int no) { /* can philosopher 'no' eat */ if ((p[no] == HUNGRY) && (p[LEFT(no)] != EATING) && (p[RIGHT(no)] != EATING) ) { p[no]=EATING; signal(s[no]); /* if so then eat */ } void take_forks(int no) { /* get both forks */ wait(mutex); /* only one at a time here please */ p[no]=HUNGRY; /* I'm Hungry */ test(no); /* can I eat? */ signal(mutex); wait(s[no]); /* wait until I can */ void put_forks(int no) { /* put the forks down */ wait(mutex); /* only one at a time here */ p[no]=THINKING; /* let me think */ test(LEFT(no)); /* see if my neighbours can now eat */ test(RIGHT(no)); 159.302

Philosopher Practice Think for a while Wait until mutex available and grab it. Look at people to your left and right If either of them is eating put down mutex and go to sleep otherwise put down mutex Eat for a while Wait until mutex available and grab it, meanwhile keep eating. Stop eating Look at person on your left If they are asleep wake them up if the person to their left is not eating. Look at person on your right If they are asleep wake them up if the person to their right is not eating. Repeat forever. 159.302

Fin 159.302