CY2003 Computer Systems Lecture 05 Semaphores - Theory.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
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.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Interprocess Communication
Classic Synchronization Problems
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Interprocess Communication
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
© 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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
2001 Networking Operating Systems (CO32010) 1. Operating Systems 2. Processes and scheduling 4.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Reference –text: Tanenbaum ch
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Interprocess Communication Race Conditions
Synchronization: semaphores and some more stuff
Semaphores Reference text: Tanenbaum ch
Process Synchronization: Semaphores
Background on the need for Synchronization
Interprocess Communication (3)
Lecture 13: Producer-Consumer and Semaphores
Process Synchronization
Process Synchronization
Lecture 2 Part 2 Process Synchronization
CSCI1600: Embedded and Real Time Software
Chapter 6 Synchronization Principles
Lecture 13: Producer-Consumer and Semaphores
CSCI1600: Embedded and Real Time Software
Semaphores Reference text: Tanenbaum ch
Synchronization, Part 2 Semaphores
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

CY2003 Computer Systems Lecture 05 Semaphores - Theory

© JMU, 2004CY2003- Week 052 Overview Achieving mutual exclusion –Recall Peterson’s solution Sleep and wakeup –the producer-consumer problem Semaphores –solving the producer-consumer problem Dining philosopher problem –Deadlock –Starvation

Achieving Mutual Exclusion

© JMU, 2004CY2003- Week 054 Recall: A Real Solution (Peterson’s algorithm) By combining the idea of lock variables with the idea of taking turns, the problem of mutual exclusion can be solved The solution needs the following variables –each process has an integer id number –two shared variables are needed turn: a single flag to indicate whose turn it is interested: array of flags to indicate whether each process is currently interested in entering (or is running) its critical region –each process must know the other’s process id

© JMU, 2004CY2003- Week 055 Busy Waiting So far, all the software approaches have used loops of the form while ( some_condition ) do_nothing; to test for some condition to come true Such a loop is called busy waiting –continuously executing test & wasting processor time Busy waiting can also have unexpected and unwelcome effects in process scheduling –suppose the system is implementing a priority queue –if the busy process is high priority, it may never yield from its busy waiting loop

Sleep and Wakeup

© JMU, 2004CY2003- Week 057 Sleep and Wakeup In order to get around the busy waiting problem, two system calls can be implemented – sleep causes the caller to block until another process wakes it up – wakeup wakes up another sleeping process There needs to be a method for linking a process calling sleep with the process calling wakeup –either wakeup takes a single parameter: the process id of the process to be woken up, or sleep and wakeup both take a parameter to match up

© JMU, 2004CY2003- Week 058 The Producer-Consumer Problem A producer is adding data to a shared buffer A consumer is removing data from this buffer –how can we code this problem using sleep and wakeup so that the producer won’t overfill the buffer and the consumer won’t read from an empty buffer? int count; int N= 100; shared int item; while ( TRUE ) { produce_item(item); if ( count == N ) sleep(); enter_item(item); count= count + 1; if ( count == 1 ) wakeup(consumer); } producerconsumer int item; while ( TRUE ) { if ( count == 0 ) sleep(); remove_item(item); count= count - 1; if ( count == N - 1 ) wakeup(producer); consume_item(item); }

© JMU, 2004CY2003- Week 059 Another Difficulty But there is still a problem with this producer- consumer ‘solution’ using sleep and wakeup Suppose the count variable is zero –the consumer reads it and decides to go to sleep –but before the sleep starts, the consumer is pre-empted –the producer runs, enters an item into the buffer, increments count, and sends the consumer a wakeup –the wakeup arrives at the consumer before the consumer starts to sleep and so will be lost Eventually, the producer will fill the buffer & sleep –both producer & consumer will now sleep forever!

Semaphores

© JMU, 2004CY2003- Week 0511 The Semaphore Primitive A new primitive variable type, called a semaphore, was introduced by E.W. Dijkstra in 1965 –a semaphore has two operations defined down (a generalisation of sleep ) –checks to see if the value is greater than zero –if so, the value is decremented –if not, the process is put to sleep (blocks) up (a generalisation of wakeup ) –if there are processes waiting, then one is woken up –if not, the value is incremented Both these are guaranteed to be an atomic action –no other process can access the semaphore until the operation has completed or blocked

© JMU, 2004CY2003- Week 0512 Solving Producer-Consumer Semaphores can be used to solve many process synchronisation problems –for example, the producer-consumer problem semaphore mutex= 1;/* controls mutual exclusion */ semaphore empty= 100;/* counts the empty buffer slots */ semaphore full= 0;/* counts the full buffer slots */ shared int item; while ( TRUE ) { produce_item(item); down(empty); down(mutex); enter_item(item); up(mutex); up(full); } producer int item; while ( TRUE ) { down(full); down(mutex); remove_item(item); up(mutex); up(empty); consume_item(item); } consumer

© JMU, 2004CY2003- Week 0513 Semaphore Uses In the producer-consumer solution, semaphores have actually been used in two distinct manners –for mutual exclusion the mutex semaphore is used to prevent the two processes reading and writing to the shared buffer at the same time it is a called a binary semaphore because it is only used by two processes and can only have the value zero or one –for synchronisation the full and empty semaphores are used to guarantee that sequences of events do or do not occur in the right order in this case they ensure that the producer stops running when the buffer if full, and that the consumer stops running when the buffer is empty

The Dining Philosophers A Problem of Interprocess Communication (IPC)

© JMU, 2004CY2003- Week 0515 The Dining Philosophers Five philosophers are sat around a table to eat –there are only five spoons –two (left & right) are needed to eat A philosopher thinks & eats –and nothing else! After thinking for a while, a philosopher tries to pick up left & right spoon If successful, they eat for a while then put down the spoons How can this problem be solved?

© JMU, 2004CY2003- Week 0516 A Proposed Solution while ( TRUE ) { think(); take_spoon(i); take_spoon((i + 1) mod 5); eat(); put_spoon(i); put_spoon((i + 1) mod 5); } The functions perform the following actions –think : performs whatever action(s) thinking involves –take_spoon : waits until spoon is available, then takes it –eat : performs whatever action(s) eating involves –put_spoon : puts a spoon back on the table after use

© JMU, 2004CY2003- Week 0517 Deadlock Unfortunately this first proposed solution is wrong! The solution suffers from deadlock Suppose all five philosophers happen to run at the same time –each philosopher picks up the left-hand spoon –each philosopher will then enter take_spoon to obtain the right-hand and will wait for it to become available –all philosophers wait for each other there is no provision for giving up the wait

© JMU, 2004CY2003- Week 0518 Starvation Let’s try to improve the deadlock situation by not blocking if one spoon is not available –check left is free: if so, pick it up; check right & pick up –if not, release any spoons being held and try again Consider the following situation –each philosopher in turn picks up left hand spoon the right hand spoon is not available –each philosopher puts down the left hand spoon now loop around again! This situation is called starvation –processes continue to run, but one(+) never progress

© JMU, 2004CY2003- Week 0519 Another Proposal The starvation problem seems to occur if all philosophers keep picking up the left-hand spoon and releasing it in perfect synchronisation –so let’s solve the problem by adding a random amount of time after failing to acquire the right-hand spoon –this must work eventually This solution will work in practice –however, it is not completely satisfactory –particularly in time-critical or safety-critical applications

© JMU, 2004CY2003- Week 0520 Summary Achieving mutual exclusion –Recall Peterson’s solution Sleep and wakeup –the producer-consumer problem Semaphores –solving the producer-consumer problem Dining philosopher problem –Deadlock –Starvation