Mutual Exclusion.

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.
Operating Systems Part III: Process Management (Process Synchronization)
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.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
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.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CSC 322 Operating Systems Concepts Lecture - 8: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
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.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Concurrency.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
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,
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Transmitter Interrupts Review of Receiver Interrupts How to Handle Transmitter Interrupts? Critical Regions Text: Tanenbaum
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Background on the need for Synchronization
Chapter 5: Process Synchronization
Process Synchronization - I
143a discussion session week 3
Lecture 11: Mutual Exclusion
Topic 6 (Textbook - Chapter 5) Process Synchronization
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Grades.
Chapter 6: Process Synchronization
Lecture 11: Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

Mutual Exclusion

Readings Silbershatz: Chapter 6

Note The assembly code for x = x +1 may look something like this: ld r1,x add r1,1 st r1,x An interrupt can occur between any of the assembly code language instructions

Mars Pathfinder Considered flawless in the days after its July 4th, 1997 landing on the Martian surface After a few days the system kept resetting causing losses of data Press called it a “software glitch” What was the problem? Answer later on!

Race Condition A race condition exists in a program when the result of program execution depends on the precise order of execution of the threads of the process or other processes Since thread/process execution speed varies, data values produced can be inconsistent

Race Condition Example (1) Printing a file requires that the file is copied to the spooler directory In Windows the spooler directory is in %SystemRoot%\SYSTEM32\SPOOL\PRINTERS Print spooler process takes files from a well-defined directory and sends them one-at-a-time to the printer.

Race Condition Example (1) Assume a spooler directory array (in shared memory) has a large number of slots Numbered 0, 1, 2 Each slot has a file name Two other variables: In points to the first empty slot where a new filename can be entered. Out points to the first non-empty slot, from where the spooler will read a filename and print the corresponding file.

Race Condition Example (1) Spooler Directory Slots 1,2,3 are empty indicating the files in those slots have printed Each process has a local variable next_free_slot representing an empty slot Assume both process A and B want to print files. Each wants to enter the filename into the first empty slot in spooler directory 1 Process A next_free_slot variable 2 OUT 3 4 4 Talk.exe 5 Prog.c IN 6 Prog.h 7 7 8 Process B next_free_slot variable …

Race Condition Example (1) Spooler Directory Process A reads in and stores the value 7 in its local variable, next_free_slot Process A’s time quanta expires Process B is picked as the next process to run 1 Process A next_free_slot variable 2 OUT 3 4 4 Talk.exe 5 Prog.c IN 6 Prog.h 7 7 8 Process B next_free_slot variable …

Race Condition Example (1) Spooler Directory Process B reads in and stores the value 7 in its local variable, next_free_slot Process B writes a filename to slot 7 1 Process A next_free_slot variable 2 OUT 3 4 4 Talk.exe 5 Prog.c IN 6 Prog.h 7 7 ProgB.c 8 Process B next_free_slot variable …

Race Condition Example (1) Spooler Directory Process B then updates the In variable to 8 Process A now gets control of the CPU 1 Process A next_free_slot variable 2 OUT 3 4 4 Talk.exe 5 Prog.c IN 6 Prog.h 8 7 ProgB.c 8 Process B next_free_slot variable …

Race Condition Example (1) Spooler Directory Process A writes its filename to slot 7 which erases process B’s filename. Process B does not get its file printed. 1 2 OUT 3 4 Process A 4 Talk.exe 5 Prog.c IN 6 Prog.h 8 7 ProgA.c 8 Process B …

Race Condition (2) It is not just the OS that has to deal with race conditions So do application programmers

Race Condition (2) Application: Withdraw money from a bank account Two requests for withdrawal from the same account comes to a bank from two different ATM machines A thread for each request is created Assume a balance of $1000

Race Condition (2) int withdraw(account, amount) { balance = get_balance(account); balance = balance – amount; put_balance(account, balance) return balance } What happens if both requests request that $1000 be withdrawn?

Race Condition (2) Both threads will read a balance of $1000 Both threads will allow for $1000 to be withdrawn balance = get_balance(account) balance = balance – amount; Thread 1 Execution sequence ss seen by CPU Context switch balance=get_balance(account); balance = balance – amount; put_balance(account,balance) Thread 2 Context switch put_balance(account,balance); Thread 1

Critical Sections and Mutual Exclusion A critical section is any piece of code that accesses shared data Printer example: In, Out variables are shared Bank account: account info is shared Mutual exclusion ensures that only one thread/process accesses the shared data

Conditions for Mutual Exclusion Mutual Exclusion: No two threads simultaneously in critical section Progress: No thread running outside its critical section may block another thread Bounded Waiting: No thread must wait forever to enter its critical section No assumptions made about speeds or number of CPUs

Mutual Exclusion in Critical Sections

Peterson’s Solution Solution developed in 1981 Considered revolutionary at the time Restricted to two processes Assumes that the LOAD and STORE instructions are atomic i.e., cannot be interrupted (although this is no longer true for modern processors) Illustrates some of the complexities in designing software

Peterson’s Solution Each process has its own copy of variables Two variables are shared int turn; int flag[2] The variable turn indicates whose turn it is to enter the critical section.

Peterson’s Solution do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); Code for P0 Only way P0 enters critical section is when flag[1] == FALSE or turn == 0 What if P0 and P1 are in their critical sections at the same time This implies that flag[0] == flag[1] ==TRUE If turn = 0 then P1 cannot break out of its while loop If turn = 1 then P0 cannot break out of its while loop This implies that P0 and P1 could not be in their critical sections at the same time do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE); Code for P1 If P0 wants to enter the critical section it “flags” its interest by setting flag[0]. By default it assumes that P1 is takings its turn

Peterson’s Solution do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); Code for P0 Can P0 block P1 even if P0 is not in its critical section? No. When P0 finishes with its critical section it sets flag[0] to FALSE flag[0] being FALSE allows P1 to break out of its while loop do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE); Code for P1

Peterson’s Solution Initially flag[0] = flag[1] = FALSE do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE); Code for P1 do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); Code for P0 Initially flag[0] = flag[1] = FALSE Let’s say that P0 wants to enter the critical section (CS) Assignments: flag[0] = TRUE, turn = 1 The condition flag[1] && turn == 1 is evaluated turn is 1 but flag[1] is FALSE; Thus the condition evaluates to false which allows P0 to enter the critical section

Peterson’s Solution do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE); Code for P1 do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); Code for P0 Let’s say that P1 wants to enter the critical section while P0 is in the critical section. Assignments: flag[1] = TRUE, turn = 0 The condition flag[0] && turn == 0 is evaluated turn is 0 but flag[0] is TRUE; Thus the condition evaluates to true; P1 enters the while loop. It is not allowed in the critical section

Peterson’s Solution Let’s say that P0 leaves the critical section do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE); Code for P1 do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); Code for P0 Let’s say that P0 leaves the critical section Sets flag[0] = FALSE The next time P1 evaluates flag[1] && turn == 0 It finds that flag[0] is FALSE which means that the while loop’s condition is false Now P1 can enter the critical section

Peterson’s Solution Peterson’s solution is not guaranteed to work on modern computer architectures Modern CPUs reorder accesses to improve execution efficiency Why was it presented? Illustrates the complexities in designing software that addresses the requirements of mutual exclusion

Synchronization Hardware Any solution to the critical section problem requires a lock Process must acquire a lock before entering a critical section Process must release the lock when it exists the critical section. We will present several hardware solutions for locks while (true) { acquire lock critical section release lock other }

Mutual Exclusion via Disabling Interrupts Process disables all interrupts before entering its critical region Enables all interrupts just before leaving its critical region CPU is switched from one process to another only via clock or other interrupts So disabling interrupts guarantees that there will be no process switch 29

Mutual Exclusion via Disabling Interrupts Disadvantage: Gives the power to control interrupts to user (what if a user turns off the interrupts and never turns them on again?) Does not work in the case of multiple CPUs. Only the CPU that executes the disable instruction is effected. Not suitable approach for the general case but can be used by the kernel when needed 30

Test and Lock Instruction (TSL) Many computers have the following type of instruction: TSL REGISTER, LOCK Reads LOCK into register REGISTER Stores a nonzero value at the memory location LOCK The operations of reading the word and storing it are guaranteed to be indivisible The CPU executing the TSL instruction locks the memory bus to prohibit other CPUs from accessing memory until the TSL instruction is done

Using the TSL Instruction JNE is used to compare the contents of REGISTER and 0 ; if these are not equal you jump to the point in the code labeled by “enter_region”

Using the TSL Operation Before entering its critical region, a process calls enter_region What if LOCK is 1? Busy wait until lock is 0 When leaving the critical section, a process calls leave_region

Using the TSL Operation Assume two processes: P0 and P1 LOCK is initialized to zero Assume that P0 wants to enter the critical section It executes the TSL instruction. The register value is 0 which reflects the value of LOCK LOCK is set to 1

Using the TSL Operation Now P1 wants to enter the critical section; It executes the TSL instruction The register value is 1 which reflects the value of LOCK P1 cannot enter the critical section It repeats the TSL instruction and comparison operation until it can get into the critical section P0 is done with the critical section LOCK becomes 0 The next time P1 executes the TSL instruction and comparison operation it finds that the register value (which reflects LOCK) is zero. It can now enter the critical section.

Problem with TSL This requires busy waiting While a process is in its critical section, any other process that tries to enter the critical section must loop continuously Busy waiting wastes CPU cycles that some other process might be able to use productively.

Questions How are multiple processes prevented from being in the critical section ? Why different than disabling interrupts? Which is better in a multicore system? Disabling interrupts or TSL test?

Question Assume that instead of a TSL instruction there is an instruction to swap the contents of a register and memory word in a single indivisible action. Can you write a enter_region routine based on this

Problems Peterson’s algorithm and TSL may seem to observe the four conditions for mutual exclusion. Unfortunately, they have other problems. Busy waiting: Much CPU time is wasted as processes are looping awaiting admission to their critical regions. Priority inversion: Two processes: H and L; H with high priority, L with low priority. H should run whenever it is ready. L is in its critical region, when H becomes ready. H is scheduled, but begins busy waiting. L can never leave its critical region since it is not allowed to run. H busy waits forever and L stays ready forever. This violates condition 4 for mutual exclusion.

Problems Peterson’s algorithm and TSL may seem to observe the four conditions for mutual exclusion. Unfortunately, they have other problems. Busy waiting: Much CPU time is wasted as processes are looping awaiting admission to their critical regions. Priority inversion: Two processes: H and L; H with high priority, L with low priority. H should run whenever it is ready. L is in its critical region, when H becomes ready. H is scheduled, but begins busy waiting. L can never leave its critical region since it is not allowed to run. H busy waits forever and L stays ready forever. This violates condition 4 for mutual exclusion.

Mars PathFinder Priority Inversion: Scheduling problem when lower-priority process holds a lock needed by higher-priority process Now back to the Mars Pathfinder problem High priority task was taking longer than expected to complete its work The task was forced to wait for a shared resource that was being held by a lower-priority process Lower-priority process was being pre-empted by a medium priority process Scheduler detected problem and would reset

Summary Defined race condition Examined different solutions