Twitter Delicious Facebook Digg Stumbleupon Favorites More

Friday 16 September 2011

Process Synchronisastion

Process Synchronization

Introduction: In operating system if two or more co-operating processes some logical space i.e both data and code section or only data using some message call. In the process of sharing if two processes starts manipulating the same data at the same time then data inconsistency may occur, hence we need some rules by which we can restrict two or more process to manipulate data at the same time, so that data consistency is maintained.

we can view this problem from this example.
Let two process P1 and P2 is working on the same resource(data)  a=30(initially).

     P1                                          P2
 temp = a;                                temp = a;
 temp = temp+10;                    temp = a-5;
 a = temp;                                a = temp;

Here temp is a local variable

Let first process P1 starts executing it and stores value of   'a'   to temp then it adds 10 to temp.
now a = 30   and   temp = 40 (here temp is local to P1)
let there is a context switch and process P2 starts executing then it stores  a =30 to its local variable temp and decrements it by 5 i.e
now a = 30(still)    temp = 25 (here temp is local to P2)
now let again their is a context switch and process P1 starts executing  and it stores 40 to 'a' (from its local temp). and gets terminated, again process P2 starts and finally it stores 25 (from its local temp) to 'a' .

Hence we end up with 'a = 25' which may be different (i.e  40) if process ends lastly.
so we can see that for different execution sequences we have different end results. This anomaly is known as 'Race Condition'.
Hence we need to insure that only one process may change the variable at a time, in order to achieve this we require that process should be synchronized.

Critical Section

Let n different processes are executing and all of them have a part of code which manipulates some share data, file etc... then we must ensure that at the same time no any two processes are manipulating the shared resources.
This segment of code is known as critical section.
we can represent the code section or text section of a process like this.

do{
    enter;
    critical section (C.S);
    exit;
    remainder section (R.S);
   }while(true);

here critical section is between 'enter' and 'exit' which shows that every process has to make a request before going to enter its critical section and after coming out the process should indicate every process via exit call, here Do While loop shows that this section of code is a infinite loop and process may run in this loop unlimited.

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates