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.

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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
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.
Interprocess Communication
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
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.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
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,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
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.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
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.
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.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

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 the Critical Section (CS)  If several processes access and modify shared data concurrently, the outcome can depend solely on the order of accesses (sans ordered control): Race Condition  In a situation involving shared memory, shared files, shared address spaces and IPC, how do we find ways of prohibiting more than 1 process from accessing shared data at the same time and providing ordered access? Mutual Exclusion (ME) [Generalization of the Race Condition Problems]

2 Race Condition : concurrent accesses to shared memory next_file_to_print next_free_slot next print request A & B generate print jobs To print a file, both A and B enter f.n to spooler directory A (next_free_slot)  7 (reads “in”) A interrupted; B switches in (and then A comes back) B  reads in (7)A  next_free_slot (7) B  f.n slot 7A  f.n slot 7in = 7+1 = 8 A prints, B stuck

3 Critical Regions Mutual exclusion using critical regions

4 Critical Regions & Mutual Exclusion Four conditions need to hold to provide mutual exclusion 1. Unity/ME: No two processes are simultaneously in the CS 2. Fairness: No assumptions can be made about speeds or numbers of CPUs except that each process executes at non-zero speed 3. Progress: No process running outside its CS may block another process (from accessing the CS) 4. Bounded Waiting: No process must wait forever to enter its CS

5 Potential ME Solutions: Interrupts Interrupt Masking (Disable Interrupts) –each interrupt has a priority level (ipl) –for each executing process, kernel maintains “current ipl” –incoming_interrupt > current_ipl  handle interrupt, else not For ME Enter CS Current_ipl  max_ipl ; disable interrupts CS Restore_ipl Exit CS  Restore_ipl lost? Process hangs  other processes blocked?  Multiproc: 1 CPU disabled, process from CPU 2 can come in meaning of “ipl” across processors?

6 ME: Using Locks for Access? unlocked?  acquire lock enter CS done  release lock do non-CS

7 ME: Lock Variables flag(lock) as global variable for access to shared section  lock = 0 ; resource_free  lock = 1 ; resource_in_use  Unix flags: locked (0,1); wanted Check lock; if free; set lock to 1 and then access CS  A reads lock(0); initiates set_to_1  B comes in before lock(1) finished; sees lock(0), sets lock(1)  Both A and B have access rights  race condition   Happening as “locking” (the global var.) is not an atomic action

8 Process Alternation: ME with Busy Waiting A sees turn=0; enters CSB sees turn=0; busy_waits (CPU idle) A exits CS, sets turn =1B sees turn=1; enters CS B finishes CS; sets turn=0; A enters CS; finishes CS quickly; sets turn=1; B in non-CS, A in non-CS; A finishes non-CS & wants CS; BUT turn=1  A waits (Condition 3 of ME? Process seeking CS should not be blocked by a process not using CS!!! But, no race condition given the strict alternation!) Turn 0Turn 1

9 Peterson’s Busy Waiting Sans Strict Alternation Process 0: other = 1; interested[0] = TRUE; turn = 0; interested[1] = FALSE; P0 exits call and gets CS Process 1: other = 0; interested[1] = TRUE; turn = 1; interested[0] = TRUE; loops Step 1: Step 2: turn is “global”var; written by last requester ? shared var

10 Petersons ME (Alternate implementation) int turn; “turn” to enter CS boolean flag[2]; TRUE indicates ready (access) to enter CS do { flag[i] = TRUE; turn = j ; set access for next_CS access while (flag[j] && turn = = j); CS only if flag[j]=FALSE or turn = i CS flag[i] = FALSE; non-CS } while(TRUE); Work out that conditions of ME, Fairness, Progress, Bounded-Waiting Hold!!!

11 Refresh: Race Conditions using Lock Variables flag(lock) as global variable for access to shared section  lock = 0 ; resource_free  lock = 1 ; resource_in_use  Unix flags: locked (0,1); wanted Check lock; if free; set lock to 1 and then access CS  A reads lock(0); initiates set_to_1  B comes in before lock(1) finished; sees lock(0), sets lock(1)  Both A and B have access rights  race condition   Happening as “locking” (the global var.) is not an atomic action

12 Is finer granularity & speed possible at the HW level?  Provide “concurrency control” or “synchronizer” instructions  Make “test & set lock” as an atomic (“uninterruptible op”)

13 ME with Busy Waiting: Atomic TSL Entering and leaving CS using TSL (HW Based) global variable! 0 =‘s unlocked

14 Busy Waiting  Wasted CPU cycles (Busy-waiting locks  Spin locks) Some spin lock problems: “The Priority Inversion Case” –2 threads: H (high priority); L (low priority)… L not scheduled if H is there! –H runs whenever “ready” –L in CS; H gets “ready”; H busy-waits –BUT: L is not scheduled anytime H is running; so L is stuck in CS (cannot exit) while H loops forever (waiting for L to get out). –So does H really have high priority or is L over-riding it?  Is ME condition 4: Bounded Waiting holding? Can we use “block” or “sleep” state than busy-waiting?

15 Sleep and Wakeup (Bounded Buffer PC) … but can have a fatal race condition!!! Shared fixed-size buffer - Producer puts info IN - Consumer takes info OUT

16 Producer-Consumer: Sleep/Race Condition T1 T2 locks locked(1) unlocks locked(0) any req? locked(0) T2 tried locking, a little late. T1 wins … so T2 goes to sleep time taken for T2 to sleep T2 sleeping; misses call put “wanted” flag wakeup!

17 Do Semaphores help? Do SW mechanisms exist for synchronization than HW TSL? Semaphores (S): Integer Variables [System Calls] –Accessible only through 2 standard atomic operations (i.e., operation must execute indivisibly) of: wait() signal()  Wait(S) { ; sleep while S 0 ; // no-op S--; }  Signal(S) { ; wakeup S++; } S can be an integer resource counter; If S is binary, it is called a “mutex” wait(0)  block if 0 wait(1)  decrement & progress (DOWN) signal(0)  increment and unblock (UP)

18 ME using Semaphores do { wait (mutex);”mutex” variable initialized to 1 // critical section signal (mutex); // non critical section } while (TRUE); wait(0)  block if 0 wait(1)  decrement & progress (DOWN) signal(0)  increment and unblock (UP)