What is the minimum value of count and why?

int count=0;

void test()


    int i,n=5;











what is the minimum value of the count?

Habib Mohammad Khan @habibkhan
11 Aug 2016 03:50 pm

Minimum value of count is 2.Here u have to assume that increment or decrement operation is a series of three step.Loading the count value into register from memory,incrementing it and storing it back into the memory.Consider 2 processes P1 and P2 calling test() function in the main().Each process performs 5 iterations.Let R1 and R2 be the respective registers for storing the value of count.Initially count = 0.P1 starts the execution.It uses the 2 steps of incrementing count in the first iteration as mentioned earlier.So R1 value = 1.But count = 0 still since value of R1 is not stored.Then we preempt P1 and we run 4 iterations of P2 and performing all the 3 steps of incrementation in each iteration.Then we preempt P2 and store the value of R1 in count and 1st iteration is over.Count value = 1.Now we preempt P1 and run 1st 2 steps of final iteration in P2.So count value will be incremented to 2 and hence content of R2 = 2.But now we preempt and run the 2nd to 5th iteration of P1.With this P1 execution is over and finally we store the value of count from R2.So final value of count = 2.With this P2 also gets completed.Hence minimum possible value of count is 2 with this approach.

Pritom Hazarika @mimikaan
12 Aug 2016 09:51 am

Thank you :-)