573 views
1 votes
1 votes

Through lock variable we try to provide synchronization

Entry section

1.while(LOCK!=0) ;
2.LOCK=1

3.CRITICAL SECTION

Exit section :

4.LOCK=0
I am unable to get that this code belongs to which process in general since P1 and P2 both execute this code and also they must be having some other code also in addition to this one so is it that both of the processes have this piece of code present in them and if it is so then how come the value of LOCK changed by one process is seen by the other process since LOCK is present in the entry section and not in the critical section which implies that it is not a shared variable 

 since if it were a shared variable it would have been inside critical section, but it is not .Also each process would make changes in its own register contents only so then how come they are actually able to see the actual values of LOCK variable

1 Answer

0 votes
0 votes
Hi , no the above code is right . this is because both process will use same code . I will explain you LOCK is a synchronisation variable . Let us see how . you will have LOCK=0 as initial value to get into critical section. Suppose if i have 2 process p1 p2 . And if anyone of the process P1 come then it , it will find lock value as 0 ryt ? Now while statement (0!=0) . false right . so it will go out of while loop and enter into critical section by making LOCK variable =1 . Now suppose this time if p2 come , it will grab lock varaible and check (lock!=0) -->(1!=0) ;which is true , so it will loop in while loop only (see ; after whileloop ). Hence you can get this would be exceution if any 1 process come first and then another one (while p1 is executing ) Now imagine a scenario when both process come together and grab a lock varaible (inital lock value is 0 ) . both will compare (0!=0) which is wrong so both of them simulatenously will enter critical section . And the thing that here Lock is used as one of door to critical section . Its not a critical section !

Related questions

1 votes
1 votes
2 answers
3
radha gogia asked Aug 9, 2015
1,945 views
If I write #include<stdio.h int a; main() { //code }then since a is a global variable so what is the storage class of it , is it extern ?
0 votes
0 votes
2 answers
4
Sanjay Sharma asked Jun 18, 2016
4,567 views
Assume transaction A holds a shared lock R. If transaction N also requests for a shared lock on R, it willresult in a deadlock situationimmediately be grantedimmediately ...