Mutual Exclusion and Synchronization
Concurrency Defined Single processor multiprogramming system Interleaving of processes Multiprocessor systems Processes run in parallel on different processors Interleaving and overlapping of processes 2
Why concurrency? Communication among processes Synchronization of multiple processes Allocation of processor time Efficient utilization of processor Sharing resources Better system performance 3
Concurrency arise in these contexts Three different contexts Multiple applications Multiprogramming Structured application Application can be a set of concurrent processes Operating- system structure Operating system is a set of processes or threads 4
Difficul7es of Concurrency Sharing of global resources Operating system managing the allocation of resources optimally Difficult to locate programming errors Relative speed of execution of processes cannot be predicted 5
Race Condi7on Shared (Global) int x = 1; Process A Process B x = x + 1; x = x + 1; CPU <<<<< Process A executing >>>>> 1) Load R1,x <<<<< Interrupt; Process Switch to Process B>>>>> 1) Load R1,x 2) Inc R1 3) Store R1,x <<<<< Interrupt; Process Switch to Process A>>>>> 2) Inc R1 3) Store R1,x 6
Race Condi7on A condition where two or more threads/processes modify a shared resource and the final result depends on the relative timing of their execution. 7
Opera7ng System Concerns Result of processing must be independent of the speed of execution Process Interactions: Processes unaware of each other Compete for resources Process directly aware of each other Cooperation by communication Processes indirectly aware of each other Cooperation by sharing 8
Compe77on for Resources Problem: Execution of one process may affect the behavior of competing processes Solution: Mutual Exclusion a critical section is the fragment of code that accesses shared data when one process is executing in its critical section, no other process is allowed to execute in its critical section Issue: Deadlock and Starvation blocked process will never get access to the resource and never terminate 9
Coopera7on by Communica7on Communication provides a way to synchronize, or coordinate, the various activities no sharing => mutual exclusion not required Possible to have deadlock each process waiting for a message from the other process Possible to have starvation two processes sending message to each other while another process waits for a message 10
Coopera7on by Sharing Processes use and update shared data such as shared variables, files, and data bases Writing must be mutually exclusive Critical sections are used to provide data integrity 11
A simple example void echo() { chin = getchar(); chout = chin; putchar(chout); } Q1 12
A Simple Example Process P1 Process P2.. chin = getchar();.. chin = getchar(); chout = chin; chout = chin; putchar(chout);.. putchar(chout);.. 13
What s causing the problem? Shared variables and shared resources Process or thread behavior must be predictable Process or thread behavior must be independent of the speed of this or other processes or threads 14
How to prevent the problem? Restricted access to shared variables and resources Implementing critical section Requiring mutual execution Q2 15
Mutual Exclusion Requirements Mutually exclusive access to critical section 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 processes that will enter the critical section next cannot be postponed indefinitely. Bounded Waiting. A bound must exist on the # of times that other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted. Assume each process executes at a nonzero speed No assumption concerning relative speed of n processes. 16
Four different approaches Hardware support Software- defined approaches Support from the OS Support from the programming language 17
Enforcing Mutual Exclusion funtion(){ <non- critical section> enter_critical_section(); <critical section> exit_critical_section(); <remainder section> } 18
Hardware Support one void func(){ <non- critical section> /* disable interrupts */ <critical section> /* enable interrupts */ <remainder section> } 19
Hardware Support one Interrupt Disabling A process runs until it invokes an operating system service or until it is terminated Disabling interrupts guarantees mutual exclusion On a uniprocessor system Processor is limited in its ability to interleave programs Multiprocessing disabling interrupts on one processor will not guarantee mutual exclusion 20
Disadvantages If the critical section is long, then control is taken away from the OS for a long period of time. This decreases overall system performance. Will not work on multi- processor systems. 21
Hardware Support two Special Machine Instructions Performed in a single instruction cycle (atomic) Access to the shared memory location is blocked for any other instructions 22
Hardware Support two (2) (Return original value of lock) boolean TestAndSet (int lock) { boolean tmp = lock; lock = 1; return tmp; } TestAndSet(Lock) Semantics If TestAndSet(Lock) = 1, someone else already has the lock If TestAndSet(Lock) = 0, lock is free and is set to 1 by the call TestAndSet(Lock) Usage (Spin- Lock) 23
Mutual Exclusion with Test- and- Set Shared data: int lock = 0; Process P i do { <non- critical section> while (TestAndSet(lock) == 1) continue; // do nothing, spin (busy wait) <critical section> lock = 0; <remainder section> } while (1); Q3 24
Proper7es Of Hardware Support (2) Advantages Applicable to any number of processes and any number of processors Simple and easy to verify Can support multiple critical sections (associate a different variable to each critical section) 25
Proper7es Of Hardware Support (2) Disadvantages Busy waiting wastes processor cycles (just like software- only solutions) Starvation is possible because of arbitrary selection of waiting process Deadlock is possible Need alternative mechanisms 26