The Critical-Section Problem

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
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.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Synchronization Solutions
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
The Critical Section Problem
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
3.1. Concurrency, Critical Sections, Semaphores
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Synchronicity Introduction to Operating Systems: Module 5.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally INTERPROCESS COMMUNICATION AND SYNCHRONIZATION SYNCHRONIZATION.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Process Synchronization: Semaphores
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
ICS 143 Principles of Operating Systems
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Lecture 20 Syed Mansoor Sarwar
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Grades.
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
Chapter 6: Synchronization Tools
Operating Systems {week 10}
Presentation transcript:

The Critical-Section Problem Operating Systems The Critical-Section Problem A. Frank - P. Weisberg

Cooperating Processes Introduction to Cooperating Processes Producer/Consumer Problem The Critical-Section Problem Synchronization Hardware Semaphores A. Frank - P. Weisberg

The Critical-Section Problem n processes competing to use some shared data. No assumptions may be made about speeds or the number of CPUs. Each process has a code segment, called Critical Section (CS), in which the shared data is accessed. Problem – ensure that when one process is executing in its CS, no other process is allowed to execute in its CS. A. Frank - P. Weisberg

CS Problem Dynamics (1) When a process executes code that manipulates shared data (or resource), we say that the process is in it’s Critical Section (for that shared data). The execution of critical sections must be mutually exclusive: at any time, only one process is allowed to execute in its critical section (even with multiple processors). So each process must first request permission to enter its critical section. A. Frank - P. Weisberg

CS Problem Dynamics (2) The section of code implementing this request is called the Entry Section (ES). The critical section (CS) might be followed by a Leave/Exit Section (LS). The remaining code is the Remainder Section (RS). The critical section problem is to design a protocol that the processes can use so that their action will not depend on the order in which their execution is interleaved (possibly on many processors). A. Frank - P. Weisberg

General structure of process Pi (other is Pj) do { entry section critical section leave section remainder section } while (TRUE); Processes may share some common variables to synchronize their actions. A. Frank - P. Weisberg

Solution to Critical-Section Problem There are 3 requirements that must stand for a correct solution: Mutual Exclusion Progress Bounded Waiting We can check on all three requirements in each proposed solution, even though the non-existence of each one of them is enough for an incorrect solution. A. Frank - P. Weisberg

Solution to CS Problem – Mutual Exclusion Mutual Exclusion – If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Implications: Critical sections better be focused and short. Better not get into an infinite loop in there. If a process somehow halts/waits in its critical section, it must not interfere with other processes. A. Frank - P. Weisberg

Solution to CS Problem – Progress Progress – If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the process that will enter the critical section next cannot be postponed indefinitely: If only one process wants to enter, it should be able to. If two or more want to enter, one of them should succeed. A. Frank - P. Weisberg

Solution to CS Problem – Bounded Waiting Bounded Waiting – A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed. No assumption concerning relative speed of the n processes. A. Frank - P. Weisberg

Types of solutions to CS problem Software solutions – algorithms who’s correctness does not rely on any other assumptions. Hardware solutions – rely on some special machine instructions. Operating System solutions – provide some functions and data structures to the programmer through system/library calls. Programming Language solutions – Linguistic constructs provided as part of a language. A. Frank - P. Weisberg

Software Solutions We consider first the case of 2 processes: Algorithm 1 and 2/3 are incorrect. Algorithm 4 is correct (Peterson’s algorithm). Then we generalize to n processes: The Bakery algorithm. Initial notation: Only 2 processes, P0 and P1 When usually just presenting process Pi (Larry, I, i), Pj (Jim, J, j) always denotes other process (i != j). A. Frank - P. Weisberg

Initial Attempts to Solve Problem General structure of process Pi (other is Pj) – do { entry section critical section leave section remainder section } while (TRUE); Processes may share some common variables to synchronize their actions. A. Frank - P. Weisberg

Algorithm 1 – Larry/Jim version Shared variables: string turn; initially turn = “Larry” or “Jim” (no matter) turn = “Larry”  Larry can enter its critical section Process Larry do { while (turn != “Larry”); critical section turn = “Jim”; remainder section } while (TRUE); Jim’s version is similar but “Larry”/“Jim” reversed. A. Frank - P. Weisberg

Algorithm 1 – Pi/Pj version Shared variables: int turn; initially turn = 0 turn = i  Pi can enter its critical section Process Pi do { while (turn != i); critical section turn = j; remainder section } while (TRUE); Satisfies mutual exclusion and bounded waiting, but not progress. A. Frank - P. Weisberg

Algorithm 2 – Larry/Jim version Shared variables boolean flag-larry, flag-jim; initially flag-larry = flag-jim = FALSE flag-larry= TRUE  Larry ready to enter its critical section Process Larry do { while (flag-jim); flag-larry = TRUE; critical section flag-larry = FALSE; remainder section } while (TRUE); A. Frank - P. Weisberg

Algorithm 2 – Pi/Pj version Shared variables boolean flag[2]; initially flag [0] = flag [1] = FALSE flag [i] = TRUE  Pi ready to enter its critical section Process Pi do { while (flag[j]); flag[i] = TRUE; critical section flag [i] = FALSE; remainder section } while (TRUE); Satisfies progress, but not mutual exclusion and bounded waiting requirements. A. Frank - P. Weisberg

Algorithm 3 – Larry/Jim version Shared variables boolean flag-larry, flag-jim; initially flag-larry = flag-jim = FALSE flag-larry= TRUE  Larry ready to enter its critical section Process Larry do { flag-larry = TRUE; while (flag-jim); critical section flag-larry = FALSE; remainder section } while (TRUE); A. Frank - P. Weisberg

Algorithm 3 – Pi/Pj version Shared variables boolean flag[2]; initially flag [0] = flag [1] = FALSE flag [i] = TRUE  Pi wants to enter its critical section Process Pi do { flag[i] = TRUE; while (flag[j]); critical section flag [i] = FALSE; remainder section } while (TRUE); Satisfies mutual exclusion, but not progress and bounded waiting (?) requirements. A. Frank - P. Weisberg

Algorithm 4 – Larry/Jim version Combined shared variables of algorithms 1 and 2/3. Process Larry do { flag-larry = TRUE; turn = “Jim”; while (flag-jim and turn == “Jim”); critical section flag-larry = FALSE; remainder section } while (TRUE); A. Frank - P. Weisberg

Algorithm 4 – Pi/Pj version Combined shared variables of algorithms 1 and 2/3. Process Pi do { flag [i] = TRUE; turn = j; while (flag [j] and turn == j); critical section flag [i] = FALSE; remainder section } while (TRUE); Meets all three requirements; solves the critical-section problem for two processes. A. Frank - P. Weisberg

Algorithm 5 – Larry/Jim version Like Algorithm 4, but with the first 2 instructions of the entry section swapped – is it still a correct solution? Process Larry do { turn = “Jim”; flag-larry = TRUE; while (flag-jim and turn == “Jim”); critical section flag-larry = FALSE; remainder section } while (TRUE); A. Frank - P. Weisberg

Critical Section for n processes: Bakery Algorithm (1) Critical Section for n processes: Before entering its critical section, a process receives a number (like in a bakery). Holder of the smallest number enters the critical section. The numbering scheme here always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is served first (PID assumed unique). A. Frank - P. Weisberg

Bakery Algorithm (2) Choosing a number: max (a0,…, an-1) is a number k, such that k  ai for i = 0, …, n – 1 Notation for lexicographical order (ticket #, PID #) (a,b) < (c,d) if a < c or if a == c and b < d Shared data: boolean choosing[n]; int number[n]; Data structures are initialized to FALSE and 0, respectively. A. Frank - P. Weisberg

Bakery Algorithm for Pi do { choosing[i] = TRUE; number[i] = max(number[0], …, number[n – 1]) +1; choosing[i] = FALSE; for (j = 0; j < n; j++) { while (choosing[j]) ; while ((number[j] != 0) && ((number[j],j) < (number[i],i))) ; } critical section number[i] = 0; remainder section } while (TRUE); A. Frank - P. Weisberg

What about process failures? If all 3 criteria (ME, progress, bounded waiting) are satisfied, then a valid solution will provide robustness against failure of a process in its remainder section (RS). since failure in RS is just like having an infinitely long RS. However, no valid solution can provide robustness against a process failing in its critical section (CS). A process Pi that fails in its CS does not signal that fact to other processes: for them Pi is still in its CS. A. Frank - P. Weisberg

Drawbacks of software solutions Software solutions are very delicate . Processes that are requesting to enter their critical section are busy waiting (consuming processor time needlessly). If critical sections are long, it would be more efficient to block processes that are waiting. A. Frank - P. Weisberg