Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each question in this section you will be given two processes that may inter

ID: 3887168 • Letter: F

Question

For each question in this section you will be given two processes that may interleave in such a manner that produces a non serializable result. For each problem Write out the steps for a potential erroneous interleaving. Identify the lines to add lock and unlock statements to prevent these errors.

1.

a1. X=getRow(10).amount;

a2. X=X+10;

a3. saveRow(10, amount, X);

b1. A=getRow(10).amount;

b2. A=A+25;

b3. saveRow(10, amount, A);

2. a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. X=X+Y;

a4. saveRow(10, amount, X);

b1. A=getRow(10).amount;

b2. B=getRow(11).amount;

b3. A=A+5;

b4. B=B+5;

b5. saveRow(11, amount, B);

b6. saveRow(10, amount, A);

3.

a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. X=X+Y;

a4. saveRow(11, amount, X);

b1. A=getRow(10).amount;

b2. B=getRow(11).amount;

b3. A=A+B;

b4. saveRow(11, amount, A);

4.

a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. Z=getRow(12).amount;

a4. if(X==Y)

{

a5. saveRow(10, Z);

a6. saveRow(11, X);

a7.

}

else

{

a8. saveRow(11, Z);

a9. saveRow(10, Y);

a10.

}

Explanation / Answer

1.

a1. X=getRow(10).amount;

a2. X=X+10;

a3. saveRow(10, amount, X);

b1. A=getRow(10).amount;

b2. A=A+25;

b3. saveRow(10, amount, A);

The expectation above is that process A will first save the modifed value which will be 10 more. After that process B will fetch the value and then incrmeent it by 25 and save,.. So if initial value was 10, A would save 20, and B would save 45.

Now if the processes are interleaved such as if a1 and b1 runs parallely.. both will observer initial value as X=10. Now process A will try to save 10 + 10, means 20, while process B would save 10 + 25, i.e. 35. now in race condition whoever wins, its values will be persisted.. So either 20 or 35 is save, which is not what is expected.

2. a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. X=X+Y;

a4. saveRow(10, amount, X);

b1. A=getRow(10).amount;

b2. B=getRow(11).amount;

b3. A=A+5;

b4. B=B+5;

b5. saveRow(11, amount, B);

b6. saveRow(10, amount, A);

If row10 contains 10 and row11 contains 15, then the expectation is that process A will save sum of these in row10, i.e. row10 will become 25. Now Process B will increment 5 each in values of rowA and rowB, so they should become 30 and 20 respectively.

Now if a1 and b1 both execute parallely, b1 will not get the updated value of row10, and hence the error will occur.

3.

a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. X=X+Y;

a4. saveRow(11, amount, X);

b1. A=getRow(10).amount;

b2. B=getRow(11).amount;

b3. A=A+B;

b4. saveRow(11, amount, A);

Here process A is trying to update row11 with sum of row10 and row11. Then the process B again goes to fetch both rows and updates their sum in row11..

So if row10=10 and row11=15 initially, then Process A will make row11 as 25, and then process B will make it as 25 + 10 = 35.

Now if the a1,a2 and b1,b2 runs parallely.. they both will get row10 as 10 and row11 as 15.. and they both will update row11 as 25, while we saw the end result should be 35.

4.

a1. X=getRow(10).amount;

a2. Y=getRow(11).amount;

a3. Z=getRow(12).amount;

a4. if(X==Y)

{

a5. saveRow(10, Z);

a6. saveRow(11, X);

a7.

}

else

{

a8. saveRow(11, Z);

a9. saveRow(10, Y);

a10.

}

Here all the lines are executed by process A only, hence there will be no data anomaly because all the lines will be executed in serial order by process A.