4,286 views
7 votes
7 votes
Consider the following program:

Const int n= 10 
int Count= 0 
Void A( ) 
{ 
    int i; 
    for(i= 1 to n) 
    Count= Count + 1; 
} 

Main ( ) 
{ 
    Par begin 
A( ); 
A( ); 
A( ); 
A( ); 
    Par end 
    
}


5 What is the minimum and maximum possible value of count after the completion of the program?

(a) 1, 40                               (b) 2, 40

(c) 3, 40                               (d) 4, 40
 

2 Answers

26 votes
26 votes

MIN = 2

MAX = 40

Maximum is obvious.

For Min -

Lets consider increment of count is in 3 micro instruction 

step 1  Load Ri M[count]

step 2 INCR Ri

step 3 M[count] Ri

Where Ri is the local register allocated by each function called and used for temporary storage.

Lets called   first A() is A1()

Second A() is A2()

Third A() is A3()

Fourth A() is A4().

Now every function from A1() to A4() will execute concurrently and each function will execute all 3 micro instruction; 

1> Now consider A1() call first, it increment COUNT from 0 to 1 in 3 micro instruction but after executing 2 micro instruction and gets preempted so at this time the value of R1(temprrary register ) = 1 but the count value will be still 0.

 2> Now let A2() and A3() complete the execution so count value will be 20.

3> A4() will start it execution and let it iterate for "9" times so the count value will be 29 and preempt the A4().

4> Let A1() will start it's execution and it will execute its 3 micro instruction and overwrite count value from 29 to 1 in it's first iteration and again preempt.

5> lets A4() start it's execution now the count value is 1 so it will iterate its 10 'th iteration and execute 2 micro instruction and then preempt A4(). at this time R4(temporary register = 2) and count = 1.

6> Now execute remaining iteration of A1 form i=2 to 10 so it will make count value as 10 and complete it's execution.

7>In final step A4() will start it's execution and will execute 3 micro instruction for 9'th iteration and overwrite count = 1 and in the 10 iteration it will increment count from 1 to 2 and finish the execution.

So Min = 2

Max = 40. 

11 votes
11 votes

Option B --2,40 

Devid count=count+1 into lower lvl instruction..

​plz follow the fig...and correct me if I am wrong..Thanks..

Related questions

2 votes
2 votes
1 answer
2
junaid ahmad asked Jan 20, 2018
477 views
States which of the statements are true and please give some reference :S1:Bounded waiting and progress implies no starvationS2:Bounded waiting doesn't imply starvation f...
3 votes
3 votes
1 answer
3
srestha asked Oct 14, 2016
1,316 views
In process synchronozation , when we can say progress is satisfied and when progress not satisfied?Explain with example.(Donot give just definition, need clear difference...
0 votes
0 votes
1 answer
4
N3314nch41 asked Sep 10, 2023
361 views
How to approach synchronization (specifically semaphore) question, there size are really intimidating and i’m unable to decode the code written? What to do??