Sie sind auf Seite 1von 3

Operating Systems ASSIGNMENT

Problem 1: Race conditions are possible in many computer systems. Consider a banking system with two functions: deposit(amount) and withdraw(amount). These two functions are passed the amount that is to be deposited or withdrawn from a bank account. Assume a shared bank account exists between a husband and wife and concurrently the husband calls the withdraw() function and the wife calls deposit(). Describe how a race condition is possible and what might be done to prevent the race condition from occurring. Solution: Race condition is possible if husband and wife use the above two functions concurrently. For eg. if Balance =5000 and husband withdraws =1000 then balance is 4000. And balance = 5000 and wife deposits= 1000 then balance is 6000. We can clearly see an inconsistency, which is not acceptable. To solve this problem we apply the Petersons Algorithm (to prevent race condition) Flags can be used for husband and wife accessing the bank account, int turn = 0 boolean flag : F F User 1: (Husband) do{ flag[i]=TRUE; turn=(i+1)%2 while(flag [(i+1)%2] && turn=(i+1)%2); withdraw(); flag[i]=FALSE (remainder section) } while (TRUE); This process can be repeated with user 2(Wife) wherein i will be replaced by j. i=0 or 1 and j=1 or 0. The turn indentifies which user is going to access the bank account next. The flags make sure the events of 2 users accessing the software are mutually exclusive. Hence the race condition is prevented from occurring.

==>

critical section

Problem 2 : The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes, P0 and P1, share the following variables: boolean flag[2]; /* initially false */ int turn; do{ flag[i]=TRUE; while(flag[j]){ if(turn==j){ while(turn==j) ; //do nothing flag[i]=TRUE; } } //critical section turn= j; flag[i]=FALSE; //remainder section }while(TRUE); The structure of process Pi(i==0 or 1) is shown in the above code; the other process is Pj(j==1 or 0). Prove that the algorithm satisfies all three requirements for the critical-section problem. Solution: The 3 requirements for critical section problem are as follows: Mutual Exclusion Progess Bounded Wait. To prove Mutual Exclusion, Pi enters its critical section only if either flag[j]=false or turn=i. Assuming the flag[i]=true and flag[j]=false then process Pi will enter the critical section. If flag[i]=false and flag[j]=true then process Pj will enter the critical section. If flag[i]==true and flag[j]==true (both processes can be executing in their critical sections at the same time) then we can imply that Pi and Pj could not have successfully executed at the same time, since the value of turn can be either i or j but not both. This ensures that mutual exclusion is preserved.

To prove Progress we observe in the program given above that before the process Pi enters its remainder section, it sets flag[i]=false and also sets turn=j. Thus the Progress property is maintained as Pi passes control to Pj to enter its critical section. To prove Bounded wait if both the process are waiting for the critical section flag[i]=true and flag[j]=true and turn=j, Pj gets to enter critical section and flag[i]=true. So Pi will enter critical section after at most one entry by Pj. This proves it is bounded.

Das könnte Ihnen auch gefallen