Chapter 3 The Critical Section Problem

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

Synchronization and Deadlocks
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
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.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Critical Section chapter3.
Sept COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 12 The Critical Section problem John Gurd, Graham Riley Centre for Novel.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
The Critical-Section Problem
Shared Memory Coordination We will be looking at process coordination using shared memory and busy waiting. –So we don't send messages but read and write.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Race Conditions Critical Sections Dekker’s Algorithm.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
6: Process Synchronization 1 1 PROCESS SYNCHRONIZATION I This is about getting processes to coordinate with each other. How do processes work with resources.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
Process Synchronization
Chapter 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
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,
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
The Critical Section Problem
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 Semaphores.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Proving Correctness and Measuring Performance CET306 Harry R. Erwin University of Sunderland.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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 Chapter 6. Semaphores are a simple, but successful and widely used, construct.
Process Synchronization: Semaphores
Background on the need for Synchronization
The Critical-Section Problem
Lecture 2 Part 2 Process Synchronization
Group Mutual Exclusion & Hadzilacos Algorithm
Semaphores Chapter 6.
Concurrency: Mutual Exclusion and Process Synchronization
Presentation transcript:

Chapter 3 The Critical Section Problem

Producer - Consumer Problem Producer Process Consumer Process Produce Put in buffer Consume Get from buffer BUFFER Buffer is shared (ie., it is a shared variable)

Progress in time….. Producer Consumer 1 2 c2 p1 p4 p3 p2 4 3 t Buffer 3 instead of 2! c1 Both processes are started at the same time and consumer uses some old value initially

A Race Condition Because of the timing and which process starts first There is a chance that different executions may end up with different results That is, because of interleaving, we can not predict what will happen next when processes are executing

Why Processes Need to Communicate? To synchronize their executions To exchange data and information

Critical Sections Critical Section Mutual Exclusion A section of code in which a process accesses and modifies shared variables Mutual Exclusion A method of prevention to ensure that one (or a specified number) of process(es) is/are in a critical section

Mutual Exclusion Problem Program producerconsumer; Procedure producer; begin repeat produce; putinbuffer; until false; End; Procedure consumer; Begin getfrombuffer; consume; Begin (* main program *) cobegin producer; consumer coend End. critical section critical section

Critical Section A critical section in the procedure of a process is the section in which the process refers and changes common variables (which are shared by other processes) There may be several critical sections operating on the same or different common variables in a process

Rules to Form Critical Sections No two processes may be simultaneously inside their CS -(Rule of Mutual Exclusion) No assumptions are made about relative process speeds or number of CPUs - (Race Conditions) No process can remain inside a CS indefiniely (CS must be small in code and it’s execution must take minimal time) - (Rule of Deadlock and Starvation) A process outside a CS should not block other processes - (Rule of Deadlock and Starvation) No process should wait forever before entering its CS (When a process leaves a CS, the process manager must activate a waiting process to enter its CS. Processes waiting to enter a CS usually wait in a queue maintained in a FIFO manner or by a priority rule) - (Rule of Starvation)

Correctness Specification Correctness specifications required for a solution are : Mutual exclusion : Statements from the critical sections of two or more processes must not be interleaved Freedom from Deadlock : If some processes are trying to enter their critical sections, then one of them must eventually succeed Freedom from Starvation : If any process tries to enter its critical section, then that process must eventually succeed

Synchronization A synchronization mechanism (synchronization primatives) must be provided to handle the critical section problem This synchronization mechanism consists of additional statements that are placed before and after a critical section (preprotocol and postprotocol)

First Attempt (Critical Section Problem for two processes) Program mutualexclusion; Var turn : integer; Procedure P; begin repeat non-critical section; repeat (* do nothing *) until turn = 1; (* busy wait while P2 is in CS*) critical section; turn := 2; until false; End; Procedure Q; Begin repeat (* do nothing *) until turn = 2; (* busy wait while P1 is in CS *) turn := 1; Begin (* main program *) turn := 1; (* we start with P1 *) cobegin P; Q coend End.

Comments Do (* nothing *) is implemented by timeslicing (process loops in the while instruction ) Solution satisfies mutual exclusion, since only one process can be in CS Deadlock is not possible. Both processes can never be stuck at the while statement Process starvation is possible, if a process spends a lot of time in its non-critical section. Here we assume that the other process is busy waiting to enter the critical section. If processes spend finite times in non-critical sections then there will be no starvation

Drawback The processes are not loosely connected. That is, the right to enter a CS is being explicitly passed from one process to another Suppose process P is waiting to enter the CS. If something happens to process Q while inside the non-critical section, process P is hopelessly deadlocked after the next pass

First Attempt Using Book’s Convention Await is blocking wait not a busy wait loop

Proving Correctness with State Diagrams In the first attempt, states are triples of the form (pi,qj,turn) Variables used in the non-critical and critical sections are assumed to be distinct from the variables used in the protocols so that they have no effect at the correctness of the solution. So, they are left out of the states The mutual exclusion correctness property holds if the set of all accessible states does not contain a state of the form (p3,q3,turn) for some value of turn, because p3 and q3 are the labels of the critical sections

State Diagrams How many states can be in a state diagram? Suppose the algorithm has N processes with ni statements in process i, and M variables where variable j has mj possible values. The number of possible states is n1 x ... x nN x m1 x ... x mM For the first attempt, the number of states is 4x4x2=32 since we have 4 statements per process and turn can be 1 or 2

State Diagram of the First Attempt The initial state is (p1,q1,1) Lefthand side is P executing, righthand side is Q executing The incremental construction terminates after 16 of the 32 possible states have been constructed We do not have states (p3,q3,1) or (p3,q3,2) so mutual exclusion property for correctness holds for the first attempt

Abbreviating the State Diagram Statements executed in the non-critical and critical sections are irrelevant to the correctness of the synchronization algorithm. So, they are eliminated In fact, you can think of p as p1: non-critical section; await turn = 1 p2: critical section; turn <- 2 Now we have 4 states to consider

Correctness of the First Attempt Mutual exclusion property holds (see the state diagram) Is the algorithm deadlock free? Which means : If some processes are trying to enter their critical sections, then one of them must eventually succeed

Is it Deadlock Free? A process is trying to enter its CS if it is executing p1 or q1 Consider the initial state. Turn is 1, which means p1 passes through and q1 loops on Next state is the lower right. Here p2 executes CS and eventually sets turn to 2. While it is in the CS, Q is executing q1 Next state is upper left in which Q enters its CS. Turn value decides which process should enter the CS Both processes eventually enter their CS in a mutually exclusive manner. Hence, the property of freedom from deadlock is satisfied

Is it Starvation Free? Consider a section of the original state diagram. NCS stands for a non-critical section The state on the lower left is the one in which p2 is waiting to enter its CS and q1 is in the NCS If process Q decides to stay in its NCS (or broken down), P can not enter its CS so starve Hence, freedom from starvation property does not hold for this solution Second attempt tries to solve it

Second Attempt Each process has its own flag which is true when process is in CS (p3, q3 and p5, q5) The other process waits until this process leaves the CS (p2, q2) If a process halts in its NCS then the other proceeds independently.

Second Attempt (Abbreviated)

State Diagram for Abbreviated Algorithm The last state (p3,q3, true,true) means that both processes enter CS at the same time Mutual exclusion property does not hold

A Scenario Showing that Mutual Exclusion Does Not Hold

Second Attempt (for jBACI) Program secondattempt; Var c1, c2 : integer; Procedure p; Begin repeat while c2 = 0 do; c1 := 0; CS; c1 := 1; NCS; until false; End; Procedure q; while c1 = 0 do; c2 := 0; c2 := 1; Begin (* main program *) c1:= 1; c2:= 1; (* Both processes are not in their CS *) cobegin p; q coend End. loop on while p2 is in CS process is entering the CS process leaves the CS if p2 breaks down here, p1 can still execute since it has its own flag c1

Comments & Correctness Each process periodically checks the other’s common variable cx. If the other process is not in CS (ie., cx = 1); the other process enters its CS, sets its flag c to 0 to indicate that it will enter its CS This program may not work correctly if events are as shown in the table We have no mutual exclusion so deadlock! c1 c2 initially 1 P1 checks c2 P2 checks c1 P1 sets c1 P2 sets c2 P1 enters CS P2 enters CS Both processes are in CS

Third Attempt This algorithm is a modification of the second attempt Here, “await” statements become a part of the CS Algorithm satisfies mutual exclusion (prove it yourself!)

Scenario for a Deadlock Both processes are locked at p3 and q3 The situation is a deadlock case but we may call it a livelock since processes are actively executing statements but nothing useful is done

Third Attempt (In jBACI) Program thirdattempt; Var c1, c2 : integer; Procedure p1; Begin repeat c1 := 0; while c2 = 0 do; CS; c1 := 1; NCS; until false; End; Procedure p2; c2 := 0; while c1 = 0 do; c2 := 1; Begin (* main program *) c1:= 1; c2:= 1; (* Both processes are not in their CS *) cobegin p1; p2, coend End. loop on while p2 is in CS process is entering its CS process leaves the CS

Comments & Correctness The third solution is a modification of the 2’nd attempt. Here, the variable cx (request to enter CS) is set before checking the other process whether it is in CS or not The solution is not correct as shown in the events of the table c1 c2 initially 1 P1 sets c1 P2 sets c2 P1 enters CS P2 enters CS Both processes are in CS

Dekker’s Algorithm

Dekker’s Algorithm (in jBACI) Program Dekker; Var turn : integer; wantp, wantq :boolean; Procedure p; Begin repeat wantp := true; while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; CS; turn := 2; wantp:= false; NCS; until false; End; Procedure q; wantq2 := true; while wantp do if turn = 1 then begin wantq:= false; repeat until turn = 2; wantq:=false end; turn := 1; wantq := false; Begin (* main program *) turn:= 1; wantp:= false; wantq:= false; cobegin p; q coend End.

Explanation of the Algorithm Procedure p; Begin repeat wantp := true; (* 1 *) while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; (* 2 *) CS; turn := 2; (* 3 *) wantp := false; (* 4 *) NCS; until false; End; Process makes a request to enter CS If the other process had made a request to enter CS before and if it is its turn then Take back the request to enter CS by setting the “want” variable to false” Wait for the other process to exit CS and change the turn variable Make a new request to enter CS and then enter CS Flip the turn variable so that now the other process can enter CS Set “want” variable to false to indicate that the process is now out of CS

Comments Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn If a process is blocked, the other process can still go Dekker’s algorithm is correct (prove it!) It satisfies the mutual exclusion property It is free from deadlock and starvation