SUNY Poly CS 521 - Concurrency, Synchronization and Mutual exclusion models.

Unformatted text preview:

Concurrency, Synchronization and Mutual exclusion models.The issue is IPC or InterProcess Communication. Processesmust communicate with each other (or be synchronous witha subset of processes) while sharing common resources. A typical example: A unidirectional communication buffer, called Pipe. A|B : A produces data that B receives as its input.e.g. 0: standard input 1: standard output 2: standard error3: read from pipe 4: standard output 5: standard errorUNIX PIPEA Bsortpipels0123 45Implication? Communication becomes complicated if race condition is allowed to prevail. So, no race condition!A general treatment of concurrency starts with the notion ofcritical section of a program. This is a code-fragment in which a shared resource is accessed. Efficient solutions to critical section problems = synchronization of processes using their critical sections in such a way that ● only one process or thread is in its CS while others wait to execute their CS. (Mutual exclusion problem)● No assumption could be made about the number of available CPUs or their speed.● No process running outside its CS can block another process from executing its CS.● No process should wait forever (starve) to get into Process 11CSProcess 2Process 32CS3CSits CS. (bounded waiting)Notion of Atomic Operations.An AO (Atomic Operation) is an operation that, once started, completes in a logical indivisible way without any other related instructions interleaved. Most solutions of CSproblem rely on the existence of certain AO. On many machines with single CPU, individual machine instructions are not interruptible and hence are atomic. On many machines with several CPU, AO is a complicated oneto be realized. CSAB CSB attempts to enter its CSB is blockedB is released from its blocked statea. A hard approach!Disabling interrupts.A enters its CS and disable all interrupts. After it completes its work in CS it enables all interrupts. When interrupts are all disabled, clock would stop working.CPU cannot do context switching since the clock is now dead.User now assumes the power to turn interrupts off/on. Undesirable. What about doing this in a multiprocessor system? What about OS kernel turning off interrupts? Is that safe?b. shared global variable: a lockInitially, lock  0. The lock access code:a: | while (lock)b: | ;c: | lock =1;d: | <critical section>e: | lock = 0;A process executes the loop as long as lock is 1. Locking fails if two or more processes are in their while loops while lock is zero.Both processes one and two execute their CS at the same time in the following sequence: 2111222211eedcdcbabaThis example fails to satisfy (a) ME constraint, and (b) bounded waiting problem. c. Another busy-waiting solution: Strict alternation.P0; P1;{… {… … …while (true) while (true){ { while(turn !=0); while(turn !=1); <critical region>; <critical region>; turn=1; turn=0; <usual_chores>; <usual_chores>;} } main( ){int turn = 0;cobeginP0; P1;Coend;}Violation of rule 3. d. Dekker’s solution. With two keys and a turn variable: all global. Initialization part:key[0]=1; key[1]=1; turn = 0;iP ;{key[i]=0;while (key[j] = = 0){if (turn != i){key[i]=1;while (turn != i);key[i]=0;}}<critical_section>;turn = j;key[i] = 1;<usual_chores>;}The mutual exclusion requirement is assured. No process will enter its CS without setting its flag. Every process checks the other flag after setting its own. If both are set, the turn variable is used to allow only one process to proceed.The progress requirement is assured. The turn variable is only considered when both processes are using, or trying touse, the resource.No deadlock is possible. Bounded waiting is assured. Show why!e. Peterson’s solution. A simpler solution. Same initialization process as above. iP;{key[i] = 1;turn = j; // give away the turnwhile ((key[j] = = 1) && (turn != i));<critical_section>;key[i] = 0;<usual_chores>;}f. Hardware solution: TSL instruction.Based on the lock solution; however, checking the lock andsetting it to 1 are done in one step as an AO. enter_cs: TSL register, lock ; get value of lock & write 1 to lock CMP register, #0 ; compare "old" value of lock to 0 JNE enter_cs ; if not equal, loop again RET ; return to caller; CS entered exit_cs: MOVE lock, #0 ; simply write 0 to lock RET ; return to caller a. Using Semaphores.Processes can cooperate through signaling protocols. Semaphore is a globally shared flag. In binary, it has a 0/1 value. In general case, it has an integer value n.Cannot be read, cannot be printed, cannot be stored and compared .. Only operations on a semaphore s:and, Normally blocked processes on a semaphore configure in the following manner. Only one process will be released upon receiving signal(s).Note all semaphore calls are atomic. Each semaphore stands for a “resource” (logical).Wait(s) : if (s = = 0) Get_blocked; Atomic elses = s-1;signal (s) : s = s+1; release a blocked process Atomic on semaphore sImplementation of ME setup:semaphore lock = 1; // initializationprocess k;{…wait(lock);critical_section;signal (lock);}Allocation of multiple copies of same resource using semaphore.semaphore s = n; // general semaphoreSemaphore, sSemaphore, sBlocked processesNon-deterministic removalProcesses on futileWait(s) callsprocess k;{…wait(s); // attempt to get a resourceuse_resource;signal(s); // return resource} Allocation of multiple resources using multiple


View Full Document

SUNY Poly CS 521 - Concurrency, Synchronization and Mutual exclusion models.

Download Concurrency, Synchronization and Mutual exclusion models.
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Concurrency, Synchronization and Mutual exclusion models. and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Concurrency, Synchronization and Mutual exclusion models. 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?