Process Synchronization: Chapter-6
Process Synchronization: Chapter-6
com
www.jwjobs.net
CHAPTER-6
PROCESS SYNCHRONIZATION
Synchronization:Synchronization is to block a process until an appropriate condition is fulfilled. Race condition:- A race condition on data items arises when many processes concurrently update its value. To avoid the race condition, we need to ensure that only one process at a time can manipulate the data . To make such guarantee , we require some form of synchronization of the processes. The CRITICAL SECTION Problem:A system consist of n processes {p0, p1,.pn-1}. Each process has a segment of code, called critical section. The important feature of the system is that, when one process is executing in its critical section, no other process is to be allowed to execute in its critical section. That is the execution of critical section by the processes is mutually exclusive in time. the problem of critical section is to design a protocol that the processes can use to cooperate. Each process must request the permission to enter its critical section. The section of the code implementing this request is the entry section. The CS (critical section)may follow the exit section. The remaining code is the remainder section. The CS must satisfies the following three requirements: 1) mutual exclusion:- If process pi is executing in its CS, then no other processes can be executing in their CS. 2) Progress:- If no process is executing in its CS and there exist some processes that wish to enter their CS, then only those processes that are not executing in their remainder section can participate in the decision of which will enter its CS next. 3) Bounded waiting:- There exist a bound on the number of times that other processes are allowed to enter their CS after a process has made a request to enter its CS and before that request is granted. General structure of a typical process:Repeat Entry section Critical section .. Exit section Remainder section Until false.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Two process solution:Here we have 3 algorithms that are applicable to only two processes at a time. the processes are numbered p0 and p1 . for our convent we call process pi and other process is pj. Where j=1-i; Algorithm1:Here the processes share a common integer variable turn initialized to 0 or 1. If turn=i , then process pi is allow to execute in its CS.
The structure of processes pi and pj are Repeat Repeat While turn!= i do no-op; while tuen!= j do no-op; Critical section critical section Turn:=j; turn:=i; Remainder section remainder section Until false until false; (for pi) (for pj) this solution ensures that only one process at a time can be in its CS. How ever it does not satisfies progress requirement. Ex:- If turn=i and Pj is ready to enter its CS, Pj can not do so, even through Pi may be in its remainder section. Algorithm 2 :Here we can replace the variable turn with the following array: Var flag: array[0..1] of Boolean; The element of the array are initialized to false. If flag[i] is true, this value indicates that Pi is ready to enter the CS. The structure of processes Pi, Pj are as follows Repeat repeat Flag[i]:= true flag[j]:=true While flag[j] do no-op; while flag[i] do no-op; Critical section critical section Flag[i]:=false; flag[j]:=false; Remainder section; remainder section; Until false until false (for process Pi) (for Pj) In this algorithm process Pi first set flag[i] to be true, signaling that it is ready to enter its CS. Then Pi checks to verifying that process Pj is ready or not to enter the CS. If Pj ready, then Pi would wait until flag[j]=false. At this point, Pi would enter the CS. On exiting CS Pi would set its flag to be false. In this mutual exclusion requirement satisfies, but progress requirement not met. Algorithm 3:By combining the key ideas of algorithm 1 and algorithm 2 obtain a correct solution to the CS problem. Where all three requirements are met. The processes share two variables.
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Var flag: array[0..1] of Boolean; Turn: 01; Initially fiag[0]=flag[1]=false and the value turn is either 0 or 1. Repeat Flag[i]:= true Turn:=j ; While (flag[j] and turn=j)do no-op; Critical section Flag[i]:=false; Remainder section Until false (for process Pi) repeat flag[j]:= true turn:=i; while(flag[i] and turn=i) do no-op; critical section; flag[j]:= false; remainder section until false. (for process Pj)
To enter the CS, process Pi first sets flag[i] to be true, and then turn is set to other process. if both processes try to enter at the same time, turn will be set to both I and j at roughly the same time. only one of these assignments will last, the other will occur. The value of turn decides which of the two processes is allowed to enter its CS first. This solution can satisfies the 3 requirements. i) mutual exclusion is preserved. j) The progress requirement is satisfies. k) The bounded waiting requirement.
www.jntuworld.com