Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Chapter 6: Process Synchronization
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.
Secure Operating Systems Lesson 5: Shared Objects.
Interprocess Communication
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
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.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
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.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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)
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Process Synchronization 1. while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced;
Lecture 6: Process Synchronization Dr. Nermin Hamza 1.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CS703 – Advanced Operating Systems
COT 4600 Operating Systems Fall 2009
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Lecture 2 Part 2 Process Synchronization
Global Environment Model
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Mutual Exclusion -- Addendum

Mutual Exclusion in Critical Sections

RoadMap r Today there are libraries that provide application programmers with semaphores r Semaphores are used by programmers to provide for critical sections r Let’s first talk about semaphores and then the OS support for them.

What is a semaphore? r A semaphore is an integer variable with the following three operations. 1. Initialize: You can initialize the semaphore to any non-negative value. 2. Decrement: The down operation Semaphore value > 0: Decrement by 1. Semaphore value == 0: If value is 0, the process blocks (gets put into queue). It is said to be sleeping on the semaphore.. 3. Increment: The up operation. Semaphore value == 0 and some processes are sleeping on the semaphore, one is unblocked. Otherwise, value is incremented.

What is a Semaphore? r Use down before entering a critical section r Use up after finishing with a critical section i.e., r Example: Assume S is initialized to 1. ……… down(S); //critical section up(S);

Using Semaphores Process P 0 ……………….. down(S); //critical section up(S); ……………….. Process P 1 ……………….. down(S); //critical section up(S); ………………..

Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Initialize the semaphore variable, S, to 1 m Why not zero?

Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Now what would happen if P 0 executes the down operation? m The semaphore S is currently 1. m It becomes 0 and P 0 enters the critical section

Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Now what would happen if P 1 executes the down operation? m The semaphore S is currently 0 m P 1 blocks

Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Assume now that P 0 is done with the critical section r It calls the up function m P 1 is unblocked m If there was no process waiting to enter the critical section the value of s would become one

Using Semaphores r What happens if there are three processes: P 0,P 1,P 2 r Assume P 0 enters its critical section r If P 1 and P 2 execute the down operation they will block r When P 0 leaves the critical section then P 1 is unblocked allowing P 1 to enter its critical section m P 2 is still blocked r What if P 0 wants to enter its critical section again when P 1 is in it?

Using Semaphores r What if we want to allow 10 processes to use a critical section? r How would we initialize a semaphore, s?

Semaphore r Binary Semaphore: Value is either 0 or 1 m Often referred to as a mutex r Counting Semaphore: Value ranges from 0 to N where N can be any integer number

Deadlock r Deadlock – Two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes r Something to watch for

Deadlock r Example: Let S and Q be two semaphores initialized to one Process P 0 Process P 1 down(S); down(Q); down(Q); down(S); …… …. up(S); up(Q); up(Q); up(S);

Implementation r With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: m value (of type integer) m pointer to next record in the list

Implementation r A semaphore can be defined as a C struct along these lines: typedef struct { int value; struct process *list; } semaphore

Implementation r down() operation can be defined as down(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } r The block() operation suspends the process that invokes it.

Implementation r up() operation can be defined as up(semaphore *S) { S->value++; if (S->value <= 0) { remove process P from S->list; wakeup(P); } r The wakeup() operation sends a signal that represents an event that the invoking process is no longer in the critical section

Implementation r BTW, the implementation just described is how Linux implements semaphores r The up and down operations represent require access to a critical section which is the semaphore variable r Need hardware/OS support e.g., m Hardware support e.g., TSL. m Signals

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

Test and Lock Instruction (TSL) r Many computers have the following type of instruction: TSL REGISTER, LOCK r One use of this instruction is to provide a lock for a critical region such code that operations on a semaphore variable

Using the TSL Instruction

TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r TSL Register, Lock m Reads LOCK into register REGISTER m Stores a nonzero value at the memory location LOCK m The operations of reading the word and storing it are guaranteed to be indivisible

TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r TSL Register, Lock m The CPU executing the TSL instruction locks the memory bus to prohibit other CPUs from accessing memory until the TSL instruction is done

TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r CMP Register, #0 m Compare register value with 0 r JNE Enter_Region m If Register value is not 0 then go to the start of enter region

TSL enter_region: leave region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r Return to caller only if Register value is 0

TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r Before entering its critical region, a process calls enter_region r What if LOCK is 1? m Busy wait until lock is 0 r When leaving the critical section, a process calls leave_region

Using TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r Assume two processes: P 0 and P 1 r LOCK is initialized to zero

Using TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r Assume that P 0 wants to enter the critical section r It executes the TSL instruction. m The register value is 0 which reflects the value of LOCK m LOCK is set to 1

Using TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r Now P 1 wants to enter the critical section; It executes the TSL instruction m The register value is 1 which reflects the value of LOCK m P 1 cannot enter the critical section m It repeats the TSL instruction and comparison operation until it can get into the critical section

Using TSL enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r P 0 is done with the critical section m LOCK becomes 0

Using TSL Enter_region: leave_region: TSL Register, Lock move Lock, #0 CMP Register, #0 RET JNE enter_region RET //to caller r The next time P 1 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.

Implementing TSL r Implementing atomic TSL instructions on multiprocessors is not trivial. r This is a subject for a computer architecture course

Signals r A signal is used in UNIX systems to notify a process that a particular event has occurred m A signal is generated by the occurrence of a particular event m A generated signal is delivered to a process m Once delivered, the signal must be handled r A signal process is used to associate computation with a signal

Signals r Example m is entered m This causes an event to be generated to a running process that is in the foreground m When that process receives the event it executes a signal handler which terminates the process

Signals r Two possible handlers m Default signal handler: Provided by kernel m User-defined signal handler: Provided by user and overrides the default r What if a process has multiple threads? m How a signal is handled depends on the type of signal. Options Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific threa to receive all signals for the process r

Semaphore Implementation r When the up is executed a blocked process is woken up. This is done using signals r Semaphore operations are critical sections – use TSL.

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

Question r 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. m Can you write a enter_region routine based on this

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

Mars PathFinder r The fix m The OS had a global variable to enable priority inheritance on all semaphores m It was off m It was set to on and then everything worked

Summary r Defined race condition r Examined different solutions