Process Synchronization and Deadlocks PDF

Summary

This document discusses process synchronization topics, including critical sections, semaphores, monitors, and examples like the producer-consumer and dining-philosophers problems, presented in PDF format.

Full Transcript

PRESIDENCY COLLEGE Presidency College (Autonomous) UNIT II (Autonomous) Process Synchronization and deadlocks Reaccredited by NAAC with A+ Presidency Group Process Synchronization Presidency College (Auto...

PRESIDENCY COLLEGE Presidency College (Autonomous) UNIT II (Autonomous) Process Synchronization and deadlocks Reaccredited by NAAC with A+ Presidency Group Process Synchronization Presidency College (Autonomous) Reaccredited by NAAC with A+ Cooperative Processes : Share -> variable, memory, Presidency code, resources(CPU,printer,scanner etc) Group Independent Processes : Don’t share Process Synchronization Presidency College (Autonomous) int shared = 5; P1 P2 int x=shared; int y= shared; x++; y--; Race Condition sleep(1); sleep(1); Reaccredited by shared=x; shared=y; NAAC with A+ ->Uniprocessor system. Presidency Group ->Both processes are running parallelly. What is Critical Section? Presidency College The critical section refers to a specific (Autonomous) part of a program where shared resources are accessed by processes(cooperative processes), and concurrent execution may lead to Reaccredited by NAAC with A+ conflicts or inconsistencies. It is essential for the operating system to provide mechanisms like locks and Presidency Group semaphores to ensure proper synchronization and mutual exclusion in the critical section. What is the Critical Section Problem in OS? Presidency College (Autonomous) When there is more than one process accessing or modifying a shared resource at the same time, then the value of that resource will be determined by the last process. This is called the race condition. Consider an example of two processes, p1 and p2. Let value=3 be a variable present in the shared resource. Reaccredited by NAAC with A+ Let us consider the following actions are done by the two processes: Presidency Group The original value of,value should be 6, but due to the interruption of the process p2, the value is changed back to 3. This is the problem of synchronization. Presidency College (Autonomous) The critical section problem is to make sure that only one process should be in a critical section at a time. When a process is in the critical section, no other processes are allowed to enter the critical section. This solves the race condition. Reaccredited by NAAC with A+ Presidency Group Presidency College (Autonomous) Process P1 Process P2 main() main() { { Non critical Section Non critical Section a,b x,y ------- ------- ------ ------ Reaccredited by NAAC with A+ ------entry section ------entry section Critical Section Critical Section Count ++ Count -- -----exit section -----exit section Presidency Group ---- ---- ----- ----- ---- ---- } } General structure of a typical process Pi, Presidency College (Autonomous) Reaccredited by NAAC with A+ Presidency Group Presidency College (Autonomous) The critical section problem is to design a protocol that the processes can use to cooperate. Entry Section: Each process much request permission to enter its critical section. The section of code implementing this request is the entry section. Reaccredited by Exit Section: The critical section will be followed by NAAC with A+ exit section. Presidency Group Solutions to the Critical Section Problem/Synchronization mechanisms/4 conditions Presidency College (Autonomous) 1. Mutual Exclusion: This means that when one process is executing within its critical section, no other process should be allowed to enter its own critical section. This ensures that shared resources are accessed by only one process at a time, preventing conflicts and data corruption. 2. Progress: When no process is currently executing in its critical section, and there is a process that wishes to enter its Reaccredited by NAAC with A+ critical section, it should not be kept waiting indefinitely. The system should enable processes to make progress, ensuring that they eventually get a chance to access their critical sections. 3. Bounded Waiting: There must be a limit on the number of Presidency Group times a process can execute in its critical section after another process has requested access to its critical section but before that request is granted. This ensures fairness and prevents any process from being starved of critical section access.c Synchronization Hardware in OS Presidency College (Autonomous) Overview: Hardware Locks are used to solve the problem of process synchronization. The process synchronization problem occurs when more than one process tries to access the same resource or variable. If more than one process tries to update a variable at the same time Reaccredited by NAAC with A+ then a data inconsistency problem can occur. This process synchronization is also called synchronization hardware in the operating system. Presidency Group Presidency College (Autonomous) The hardware solution is as follows: 1. Test and Set 2. Swap 3. Unlock and Lock Reaccredited by NAAC with A+ Presidency Group Unlock and lock Presidency College (Autonomous) do 1.while(LOCK == 1) ;//Entry Code { 2.LOCK = 1 acquire lock 3. Critical Section Critical Section 3.LOCK = 0; //Exit Code Release lock } Reaccredited by NAAC with A+ Initially Lock =0 -> Vacant Lock =1 -> full Execute in user mode. Presidency Group No guarantee mutual exclusion is achieved(case 2). Test and Set Presidency College (Autonomous) boolean lock = false; 1.while(test_and_set(&LOCK));//E boolean TestAndSet(boolean *target) ntry code { 2. Critical Section boolean r = target; 3.LOCK = false; //Exit Code target = true; return r; } True -> 1 Reaccredited by NAAC with A+ False -> 0 Presidency Group lock target r false 1000 false 1000 Swap Presidency College (Autonomous) while(true) { boolean key,Lock=false void swap(Boolean *a,Boolean *b) Key = True; { while(key == True) Boolean temp = *a; swap(&Lock,&Key); *a=*b; CS Reaccredited by NAAC with A+ *b=temp; Lock=false; } Remaining section } Presidency Group Semaphores Presidency College (Autonomous) What is Semaphore in OS? Semaphore is essentially a non-negative integer that is used to solve the critical section problem by acting as a signal. It is a concept in operating systems for the synchronization of concurrent processes. Reaccredited by Method or tool used to prevent race condition. NAAC with A+ -After initialization it can only be accessed by two atomic operations: wait(s) signal(s) Presidency Group { { while(s

Use Quizgecko on...
Browser
Browser