1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

Slides:



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

CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
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.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
Chapter 3 The Critical Section Problem
CIS 720 Mutual Exclusion. Critical Section problem Process i do (true) entry protocol; critical section; exit protocol; non-critical section od.
Concurrency.
The Critical-Section Problem
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
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.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Lecture : Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2008.
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.
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.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
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.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
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. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
G.Anuradha Reference: William Stallings
COMP 3500 Introduction to Operating Systems Synchronization: Part 1
Chapter 5: Process Synchronization
Lecture 11: Mutual Exclusion
Topic 6 (Textbook - Chapter 5) Process Synchronization
COP 4600 Operating Systems Spring 2011
The Critical-Section Problem
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Chapter 6: Process Synchronization
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Lecture 6: Concurrency: Mutual Exclusion and Synchronization
Presentation transcript:

1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012

2 Concurrency An OS has many concurrent processes that run in parallel but share common access Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place.

3 Example for Race condition Suppose a customer wants to book a seat on UAL 56. Ticket agent will check the #-of-seats. If it is greater than 0, he will grab a seat and decrement #-of-seats by 1. UAL 56: #-of-seats=12 Main memory Terminal … Ticket Agent 1 Ticket Agent 2 Ticket Agent n

4 Example for Race condition(cont.) Ticket Agent 1 P1: LOAD #-of-seats P2: DEC 1 P3: STORE #-of-seats Ticket Agent 2 Q1: LOAD #-of-seats Q2: DEC 1 Q3: STORE #-of-seats Ticket Agent 3 R1: LOAD #-of-seats R2: DEC 1 R3: STORE #-of-seats Suppose, initially, #-of-seats=12 Suppose instructions are interleaved as P1,Q1,R1,P2,Q2,R2,P3,Q3,R3 The result would be #-of-seats=11, instead of 9 To solve the above problem, we must make sure that: P1,P2,P3 must be completely executed before we execute Q1 or R1, or Q1,Q2,Q3 must be completely executed before we execute P1 or R1, or R1,R2,R3 must be completely executed before we execute P1 or Q1.

5 Critical Section Problem Goal: To program the processes so that, at any moment of time, at most one of the processes is in its critical section. Prefix 0 CS 0 Suffix 0 P0P0 Prefix 1 CS 1 Suffix 1 P1P1 Prefix n-1 CS n-1 Suffix n-1 P n-1 … Critical section: a segment of code in which the process may be changing common variables, updating a table, writing a file, and so on.

6 Solution to Critical-Section Problem Any facility to provide support for mutual exclusion should meet the following requirements: 1. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section 2. A process that halts in its noncritical section must do so without interfering with other processes. 3. A process waiting to enter its critical section cannot be delayed infinitely 4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay. 5. No assumption are made about the relative process speeds or the number of processors. 6. A process remains inside its critical section for a finite time only.

7 Three Environments 1. There is no central program to coordinate the processes. The processes communicate with each other through global variable. 2. Special hardware instructions 3. There is a central program to coordinate the processes.

8 Three Environments 1. There is no central program to coordinate the processes. The processes communicate with each other through global variable. 2. Special hardware instructions 3. There is a central program to coordinate the processes.

9 1 st Attempt Start with just 2 processes, P 0 and p 1 Global variable turn, initially turn=0 Prefix 0 While (turn  0) do {} CS 0 turn=1 suffix 0 Prefix 1 While (turn  1) do {} CS 1 turn=0 suffix 1 The processes take turn to enter its critical section If turn=0, P 0 enters If turn=1, P 1 enters This solution guarantees mutual exclusion. But the drawback is that the method is not fair, because P0 is priviledged. Worse yet, until P0 executed its CS, P1 is blocked.

10 2 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 While (flag[1]) do {} flag[0]=true CS 0 flag[0]=false suffix 0 Prefix 1 While (flag[0]) do {} flag[1]=true CS 1 flag[1]= false suffix 1 If P 0 is in critical section, flag[0] is true; If P 1 is in critical section, flag[1] is true If one process leaves the system, it will not block the other process. However, mutual exclusion is not guaranteed. P 0 executes the while statement and finds that flag[1] is false; P 1 executes the while statement and finds that flag[0] is false. P 0 sets flag[0] to true and enters its critical section; P 1 sets flag[1] to true and enters its critical section.

11 3 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 flag[0]=true While (flag[1]) do {} CS 0 flag[0]=false suffix 0 Prefix 1 flag[1]=true While (flag[0]) do {} CS 1 flag[1]= false suffix 1 If P 0 is in critical section, flag[0] is true; If P 1 is in critical section, flag[1] is true Guarantees mutual exclusion. But mutual blocking can occur. P 0 sets flag[0] to be true; P 1 sets flag[1] to be true; Both will be hung in the while loop.

12 4 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 L0: flag[0]=true If (flag[1]) then { flag[0]=false; goto L0} CS 0 flag[0]=false suffix 0 Prefix 1 L1: flag[1]=true If (flag[0]) then { flag[1]=false; goto L1}} CS 1 flag[1]= false suffix 1 Guarantees mutual exclusion. mutual blocking can occur if they execute at the same speed.

13 Correct Solution (Dekker’s Alg) – The first correct mutual exclusion alg (early 1960’s) Initially, flag[0]=flag[1]=false; turn=0 Prefix 0 flag[0]=true while (flag[1]) do {if (turn=1){ flag[0]=false; while(turn=1) do{} flag[0]=true; } CS 0 turn=1 flag[0]=false suffix 0 Prefix 1 flag[1]=true while (flag[0]) do {if (turn=0){ flag[1]=false; while(turn=0) do{} flag[1]=true; } CS 1 turn=0 flag[1]=false suffix 1

14 Peterson’s Algorithm for 2 processes – The simplest and most compact mutual exclusion alg. Initially, flag[0]=flag[1]=false Prefix 0 flag[0]=true turn=1 while (flag[1] and turn=1) do{} CS 0 flag[0]=false suffix 0 Prefix 1 flag[1]=true turn=0 while (flag[0] and turn=0) do{} CS 1 flag[1]=false suffix 1

15 Solution for n processes Global Variable 1. Flag[0..n-1] – array of size n. 2. Turn. Initially, Turn=some no. between 0 and n-1 Idle if Pi is outside Csi Want-in if Pi wants to be in CSi in-CS if Pi is in CSi Flag[i]=

16 Solutions for n processes Pi Prefix i Repeat Flag[i]=want-in; j=Turn; while j  i do {if Flag[j]  idle then j=Turn else j=(j+1) mod n} Flag[i]=in-CS j=0 while (j<n) and (j=i or Flag[j]  in-CS) do {j=j+1} Until (j  n) and (Turn=i or Flag[Turn]=idle) Turn=i; CS i j=(Turn+1)mod n While (j  Turn) and (Flag[j]=idle) do{j=(i+1) mod n} Turn=j Flag[i]=idle

17 Three Environments 1. There is no central program to coordinate the processes. The processes communicate with each other through global variable. 2. Special hardware instructions 3. There is a central program to coordinate the processes.

18 Hardware Support Disable interrupt CS Enable interrupt Won’t work if we have multiprocessors

19 Special Machine Instructions Modern machines provide special atomic hardware instructions  Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

20 TS – Test and Set Boolean TS(i)= true if i=0; it will also set i to 1 false if i=1 Initially, lock=0 P i Prefix i While(¬ TS(lock)) do {} CS i Lock=0 suffix i It is possible that a process may starve if 2 processes enter the critical section arbitrarily often.

21 End of lecture 8 Thank you!