Data Inconsistency / Critical Section

Consider the following program segments for two different processes (P1, P2) executing concurrently, where a and b are not shared variables, but x starts at zero and is a shared variable.

Case 1:
"Processor #1 "
for (a = 1; a <= 3; a++)
x = x + 1;

"Processor #2"
for (b = 1; b <= 3; b++)
x = x + 1;

Case 2:
"Processor #1"
x = 0; x = 0;
for (a = 1; a <= 3; a++)
x = x + 1;

"Processor #2"
for (b = 1; b <= 3; b++)
x = x + 1;

a. If the processes P1 and P2 execute only once at any speed, what are the possible resulting values of x in Case 1 and Case 2? Explain your answers.
b. Suggest some modifications on the original code to eliminate the problem. (Answer part (b) only if you have identified data inconsistency / critical section problems in part (a))

Questions by fatema sawan

Showing Answers 1 - 0 of 0 Answers

Answer part(a):
Case #1
Since the both the processes have executed for once, the possible resulting values are 3,4,5,6.We get minimum i.e. 3 when all the times processor#1 and processor #2 concurrently and at the same time access the shared x .
When they execute with a bit lapse in time..no overlapping then we get 6.

case #2
Possible values 1,2,3...think of this why

  Was this answer useful?  Yes

nancyagg

  • Jul 11th, 2010
 

Part A : Since in case1 X is not initialized. Therefore the resulting values in case 1 will be some garbage values stored in X earlier.Whereas in case2 X has been initialized therefore the possible values of X in case2 will be 1,2,3 but since both the processes are executing concurrently therefore there will be a conflict between processor#1 and processor#2 for data inconsistency. since for processor#2 now the value of X is different to apply the further addition.And there will exists the problem of mutual exclusion which leads to Deadlock
Part B : To eliminate this problem either 
  • We can lock the unsafe part of the coding 
  • We can simply make X to be non sharable by using some another variable for processor#2.
  • or we can declare X to be a global variable
There are many ways to eliminate this problem....

  Was this answer useful?  Yes

I think it is mentioned that 'x' is initialized to 0.


I would solve it this way: Unrolling the loops, we get the following instructions to be executed in each of the following cases:

Case 1:

x = 0

Proc 1:

x = x + 1
x = x + 1
x = x + 1

Proc 2:

x = x + 1
x = x + 1
x = x + 1

Case 2:

x = 0

Proc 1:

x = 0
x = 0
x = x + 1
x = x + 1
x = x + 1

Proc 2:

x = x + 1
x = x + 1
x = x + 1

These instructions can be interleaved in any possible order. Eventually they should get computed in a sequential fashion.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions