in Operating System edited by
47,619 views
112 votes
112 votes

Consider the following proposed solution for the critical section problem. There are $n$ processes : $P_0....P_{n-1}$. In the code, function $\text{pmax}$ returns an integer not smaller than any of its arguments .For all $i,t[i]$ is initialized to zero.

Code for $P_i$;

do {
    c[i]=1; t[i]= pmax (t[0],....,t[n-1])+1; c[i]=0;
    for every j != i in {0,....,n-1} {
        while (c[j]); 
        while (t[j] != 0 && t[j] <=t[i]);
    }
    Critical Section;
    t[i]=0;
    
    Remainder Section;
    
}  while (true);

Which of the following is TRUE about the above solution?

  1. At most one process can be in the critical section at any time
  2. The bounded wait condition is satisfied
  3. The progress condition is satisfied
  4. It cannot cause a deadlock
in Operating System edited by
47.6k views

4 Comments

while( t[j] != 0 && t[j]<=t[i]);

what does above line means?

I conclude it like this-

if (t[j] !=0 and t[j]<= t[i]) is true then process will be in the while loop (because of semicolon in while loop) and if any of the condition is false ( means if ( t[j] == 0 ) or ( t[j] > t[i] )) then it will enter into critical section?

Please clear my doubt.
1
1

consider 3 processes P0,P1,P2 with t[]= {0,0,0} //Will be ignoring c[i] for this example.

P0 P1 P2
t[0]=1    
  t[1]=2  
    t[2]=3 // t[i] can be viewed as a timestamp for a process, relative to the other processes (hence the reason for max(ti)+1)
while (t[j] != 0 && t[j] <=t[i]) //As no other t[j] (where i!=j) is zero, which implies no other processes has been to critical section yet. Also, t[j] <=t[i] implies the current process should busy wait, until the older process (smaller timestamp) still exists in the queue. Overall, the point of this whole condition is that older processes must enter CS first. As a result P0 enters CS and t[0] becomes 0.    
  Now that t[0] = 0, no need for comparing its time stamp with t[1], as it has been already been to CS, being an older process. for other j ie t[2]=3, t[1]<t[2], which means P1 should enter CS next. t[1] becomes 0  
Now, what if P0 tries entering the CS again? t[0] shall become 4, implying P0 is a relatively newer process trying to get into CS, compared to P2.{ while (c[j]) } will be 0 only, moving to the next condition, for j=1, t[1] =0, hence no comparison and we will skip to t[2]=3, which is lesser than t[1], so we keep busy waiting.    
    P2 will successfully enter and leave CS resulting t[2]=0.
as t[2] becomes 0, P1 now enters and leaves CS, making t[0]=0.    

Hence, we can conclude only one process is allowed to enter CS at a time // Not concrete proof though
Also, I believe bounded waiting is satisfied, but read the below answers for more details on that.

2
2
update the tags in go book also
1
1

7 Answers

91 votes
91 votes
Best answer

Answer is (A).
 

 while (t[j] != 0 && t[j] <=t[i]);

This ensures that when a process $i$ reaches Critical Section, all processes $j$ which started before it must have its $t[j] = 0$. This means no two process can be in critical section at same time as one of them must be started earlier.

returns an integer not smaller


is the issue here for deadlock. This means two processes can have same $t$ value and hence

while (t[j] != 0 && t[j] <=t[i]);

can go to infinite wait. ($t[j] == t[i]$). Starvation is also possible as there is nothing to ensure that a request is granted in a timed manner. But bounded waiting (as defined in Galvin) is guaranteed here as when a process $i$ starts and gets $t[i]$ value, no new process can enter critical section before $i$ (as their $t$ value will be higher) and this ensures that access to critical section is granted only to a finite number of processes (those which started before) before eventually process $i$ gets access.

But in some places bounded waiting is defined as finite waiting (see one here from CMU) and since deadlock is possible here, bounded waiting is not guaranteed as per that definition.

edited by
by

69 Comments

Can u please explain it properly
1
1

Consider the critical section point. When a process ii reaches here, it has $c[i]=1$c[i]=1

Are you sure? Because just before for(){} loop, there is c[i] = 0, at the end of the first line of the do{}while() loop. So c[i]=1 only during the execution of pmax() and assignment of pmax()+1 to t[i]

1
1
Are we supposed to assume that following statement

t[i] = pmax()+1

can be executed by at most one process at a time. There is no indication of holding lock on t, neither on c, before executing above statement.
0
0
nopes. we can't assume that. And the earlier one I missed that :( I'll correct the answer now.
0
0
edited by

Should possibility of deadlock means violation of bounded wait and progress?

0
0
see answer now, any doubt still?
0
0
@Arjun Veteran, I disagree with the finite waiting condition that you have cited for bounded wait. This condition must hold for progress and not BW. Bounded waiting means bounds on no of times other processes are allowed to enter in CS before this process once it has requested. Its not bound on time. So, BW is satisfied.

Last 2 options are easy to be discarded. If no deadlock, it means progress also satisfied. So, 2 options cant be correct.
0
0
0
0

The statement on CMU's site:

Bounded Waiting: No process can wait forever for a resource -- otherwise an easy solution: no one gets in.

Now, I think what they are trying to say is if I am not allowed to get into critical section forever while others are getting a chance to enter (as indicated by the partial sentence after '--'), then why this partiality against me and hence, easy solution that no one gets in.

I think the CMU's statement is subject to benefit of doubt.

2
2
@arjun sir,can you give any example in which bounded wait is nt satisfied here??
0
0
Sir if bounded waiting is defined for No of processes and not for Time , then in this question BW is satisfied.

If Infinite time waiting is also called bounded waiting , then it's ok to say bounded waiting is violated.
0
0
@Debashish. Even the CMU's statement says the 1st thing which is same as in Galvin.
0
0
please, elaborete what you are trying to say.
0
0
Galvin book says: There should be bound on number of times other processes are allowed to enter the crtical section when a process has already made a request i.e process is waiting for a resource.

CMU statement on bounded wait: No process can wait forever for a resource -- otherwise an easy solution: no one gets in.

This effectively means I should get a chance and I shouldnt be waiting for infinite time on others to allocate resource to me.

The reason for infinite waiting is always others get a chance to go into the CS.

There may be other reasons for infinite waiting like deadlock. That doesnt necessarily mean bounded waiting is hampered.
1
1

so infinite waiting for others to release resources should be treated as no bounded waiting .. correct ?

0
0

@Debashish. If there is partiality in granting resources, only then that infinite waiting can be called unbounded waiting.

If you are infinitely waiting just because you are stuck in deadlock(or any other reason), that doesnt become unbounded waiting.

This is what CMU statement says. "I shouldnt be waiting for a resource forever. If I dont get in, no one gets in". i.e. there is clear indication of partiality as the criteria while deciding if a waiting is to be considered unbounded waiing or something else.

So, bounded waiting just says that scheduler shouldnt be partial/biased while granting resources.

1
1
just a straight forwards answer ! BW waiting is satisfied here. Y/N ?
0
0
I understood your statement earlier
0
0
No biasing, so satisfied :)
0
0
what if more than one process executes the while loop just as the ith processs makes t[i]=0,then more than 1 process can get into the critical section......
0
0

I read all the above comments  , i think that answer given by Arjun is correct though need one edit, he took all four options and trying to analyzed them one by one. In this question Deadlock is occur as you see in the  comment , so option D is False, now when deadlock occur progress should not be there this makes option C False. Now there is starvation as there is No time limit within which a resource is granted to a process , if starvation occurs then there is NO Bounded wait ( " For a set of processes to satisfy bounded wait, there should be an upper bound on the no of processes that enter into their critical sections before a process can enter into it's critical section ") hence option B is false . So all three options D, C B is false left option A to be true .

Need to edit Bounded wait point in that best answer , rest is ok.

5
5

Sir,

Now there is starvation as there is No time limit within which a resource is granted to a process , if starvation occurs then there is NO Bounded wait ( " For a set of processes to satisfy bounded wait, there should be an upper bound on the no of processes that enter into their critical sections before a process can enter into it's critical section 

  • There is no limit in time here, but in this QS there is a limit on the no of processes going in-out, while a particular thread is in the blocked state.
0
0

See, in Arjun's answer  read this part " But bounded waiting (as defined in Galvin) is guaranteed here as when a process i starts and gets t[i] value, no new process can enter critical section before i (as their t value will be higher) and this ensures that access to critical section is granted only to a finite number of processes (those which started before) before eventually process i gets access. " 

read that bold line, there i think need a change .

Now you ask me Why so ?

Arjun's comment says - " A process must know after how many process it will get chance to enter in CS " 

there is a subtle difference between Number of process and Number of turns, Bouded Wait related to later not former. 

That's why it needs to change.

The definition of Bounded waiting says -  " After a process made a request to enter its critical section and before it is granted the permission to enter, there exists a bound on the number of turns that other processes are allowed to enter."

 We need Progress + Bounded-Waiting to imply Starvation-Freedom

 But in this question there is a Starvation then how Bounded waiting is satisfy when progress does not satisfy ?

Reference :

https://gateoverflow.in/?qa=blob&qa_blobid=8914138165937713753

and i think that  limit of no of processes going in-out is there to satisfy Mutual Exclusion condition which is Option A .

4
4
@Bikram

So deadlock always implies bounded wait is hampered?
0
0

@Sushant

Starvation-freedom imply deadlock-freedom But starvation-freedom DOES NOT  imply bounded-waiting  

Deadlock is not related with Bounded wait , both are independent , thats what i understand. 

Deadlock is related to progress and progress is Not related to Bounded Wait.

Hence  Deadlock always implies bounded wait hampered ---> No it is not correct. 

1
1

@Bikram

In the pdf link you cited here , just see the title "Progress vs. Bounded Waiting".

They have clearly said "Bounded wait doesnt imply  progress"  i.e. Even though there is bounded waiting, there can be a deadlock. This contradicts your statement "Deadlock always implies bounded wait  hampered".

1
1

Sushant Gokhale it's ok! I guess too much discussion in this thread! :) :)

1
1

@Sushant 

Thanks for that correction , i understood that Deadlock is not related to Bounded wait .

Bounded waiting means no process should wait for a resource for infinite amount of time.

( in that slide heading Bounded Waiting you can find it, ----- > where it says Finite is not the same as bounded means " Finite means any value you can write down (e.g., billion, trillion, etc) while the Bounded means this value has to be no larger than a particular ones , that means not infinite value . )

And here is starvation , it occurs because of this t[j]  == t[i] in second while loop .

as if t[j] == t[i] is possible then starvation is possible means infinite waiting is also possible .

It means there is No Bounded wait . Thats why Mutual Exclusion i.e.  option A only correct.

I hope this answer is full proof now. 

4
4
Yes, now its correct. Chapter closed..  :)
1
1
edited by

@Bikram and @Debashish

We have went completely in wrong direction. Infinite waiting due to deadlock in above example has nothing to do with bounded waiting. Bounded waiting in above problem is dissatisfied not due to possibility of deadlock but due to some other reason.

Lets see how:

Let the request vector be as shown at some point of time.

So, P1, P2, P3 ........P10, P11 have made a request to enter the critical section. For the moment, lets assume all have t[i] are distinct so that there is no possibility of deadlock (and to distinguish it from bounded waiting ).

Now, the request get satisfied sequentially in the manner P1, P2, P3......

Lets say P1, P2, P3 finish their execution. So, after this the request vector is as shown.

Now, execution of P4 into the critical section starts. Now, lets say P1, P2 again make a request to enter the critical section while P4 executes in critical section. So, after P4 finishes executing in the critical section, the request vector is as follows:

If it happens that pmax()+1 wraps around for P1 and P2 (here it cant happen since #processes=11 but if Pn happens to be last integer that can be represented then its possible and we are considering this possibility), then again P1 starts executing. Then P2.

Now, if it happens that P1, P2 make request to enter the critical section in an alternating manner, P10 or P11 will never get a chance to enter the critical section.

So, there is no bound on number of times other processes are allowed to enter the critical section before P10 or P11 is allowed to enter.

So, here in this example, deadlock( or infinite waiting due to deadlock ) had nothing to do with bounded waiting.

Also, @Bikram, even the pdf link you gave, they say the same:

"Bounded waiting doesnt imply progress"

Also, they say:

"Progress and bounded waiting concepts are independent, totally unrelated".

2
2

1st assumption :

So, P1, P2, P3 ........P10, P11 have made a request to enter the critical section. For the moment, lets assume all have t[i] are distinct so that there is no possibility of deadlock (and to distinguish it from bounded waiting ).

  • They have calculated their t[] values distinct somehow.
  • So you can also assume t[11] = {1,2,3,4,5,6,7,8,9,10,11} 
  • Till now ok !

  • After some point of time $P_1$ and $P_2$ and $P_3$ completes and sets their corresponding t[] values to zero.
  • $\Rightarrow$ t[1] = t[2] = t[3] = 0

2nd hypothetical assumption

Now, execution of P4 into the critical section starts. Now, lets say P1, P2 again make a request to enter the critical section while P4 executes in critical section. So, after P4 finishes executing in the critical section, the request vector is as follows:

Now, again P1 starts executing. Then P2.

  •  $P_1$ and $P_2$ will start from starting of the program.
  •  $P_1$ and $P_2$ will calculate their t[] values
  • t[1] and t[2] must be greater than $11$
  • How come you concluded that $P_1$ and $P_2$ will again get a chance repeatedly ?
0
0

If all the processes are having some positive t[]  values each (means they want to enter into CS) , then this algorithm allows one with the minimum positive token value. 

1
1

Sushant Gokhale .

We have went completely in wrong direction

  • I guess we are not going in wrong direction. 
0
0

@Debashish.

They have said that pmax() returns an integer.

if  pmax()+1  value wraps around, then P1 will again get a value 0 and similarly for P2.

Again, this is just a possibilty and completely depends upon the imlementation details.

If not, then certainly bounded waiting is satisfied. 

0
0
I had a good discussion on a similar problem but I am not getting the link for the same.

So, I am sure that if the wrapping around doesnt happen, then bounded wait is satisfied.
1
1
see @sushant what i understand is there is a deadlock, and for that No progress that makes option3 and 4 false.

Now there is starvation and that starvation time is large enough to become No bounded wait thats why only mutual exclusion is occuring here .
0
0
@Bikram. There can be starvation due to deadlock in this example.

 

Otherwise,

It can be   

no bounded wait -> starvation

 

But, following cant be necessarily true:

starvation -> no bounded wait
0
0

1:)while (t[j] != 0 && t[j] <=t[i]) deadlock possible when t[j]==t[i]...so progress is not satisfied here..
 

2:)starvation is also possible because of same reason here and hence infinite waiting and hence no bounded waiting is satisfied!!

1
1
@sudsho. I dont understand why you all arent understanding that deadlock occurence has nothing to do with bounded waiting as given in the pdf by Bikram.
1
1

i also agree that deadlock has nthing to do with bounded waiting
i said there is starvation possible in this question thats why no bounded waiting....for this question only it's happening that deadlock is leading to starvation ..this doesnt happens always in crirtical section problems...!!

1
1

Deadlock can lead to Starvation 

But

Starvation does not lead to Deadlock .

And there is no relation between Bounded wait and Deadlock, i also coment this before.

relation is between Bounded wait and starvation in this question .

Starvation time is large so No bounded wait happens. This is the answer why B is false. 

0
0
Sorry guys, I disagree that starvation here is hampering  bounded wait. If so, just prove that there is no limit on number of turns other processes are getting in CS before requesting process gets into CS.
1
1

@Debashish and @Bikram

See this link. See the explanation by Amit Pal. He has explained bounded waiting beautifully.

Now, can you find similar scenario here?

1
1
I also agree with Bikram sir.

Starvation does not lead to deadlock and there is no relation between Bounded waiting and Deadlock.
1
1

Sir, we can get t[i] of two process same ONLY IN THE CASE when,

t[i]= pmax (t[0],....,t[n-1])+1

of a process is preempted and

 if we modify above statement with

P(s)

t[i]= pmax (t[0],....,t[n-1])+1

V(s)

where s is any binary semaphore with initial value of 1, then we can never have deadlock.

m i right or wrong?

Is there any other case when the two process can have same value for their t[i]??

'

0
0
what definition of Bounded wait are we supposed to use in exam? Bound on number of turns or " No process waits forever"??
2
2
Bound on number of turns.....i.e. if one process waits infinitely but other process/processes are allowed to enter CS, then its bounded wait voilation.

If all processes wait for a resource infinitely, then it deadlock and not unbounded wait.
4
4

@Sushmita

Bounded wait does not have ambiguous meaning ..

it have only one meaning that is " Bound on number of turns ", it means if a process P wants to enter into it's CS then there are some number of turns( read it as number of Bounds - it have some Upper Bound) it have to enter into CS. 

Here number of turns means Number of times P can enter into it's Critical Section . 

Like i can go to  Library 5 times in a day, that 5 is my BW to go to Library . If it is 6 then i can not enter into the Library.

Hope it is clear.

No process waits forever is the effect of BW, not the definition itself !!

2
2
Thanx a lot. clear
0
0
yeah now its clear. Actualy that case of deadlock confused me. But now clear. Thanx again. Well this is such a question. Very conceptual.
0
0

I think @Sushant Gokhale has given the correct explanation for No Bounded Waiting i.e wrapping up of the integer value.

0
0
@Bikram Sir can we say not satisfying bounded waiting means starvation??
0
0

Shubhanshu

we say not satisfying bounded waiting means starvation??

No. It does not simple like that .

Bounded-Waiting does not say if a process can actually enter. It only says there is a bound.

For example, all processes are locked up in the enter section (i.e., failure of Progress).

We need Progress + Bounded-Waiting to imply Starvation-Freedom .

So , all it means Starvation freedom does not satisfy Bounded Waiting .

4
4

Shubhanshu

Progress is related to starvation..there is no progress means starvation possible (long waiting == starvation )

Progress and Bounded Waiting are independent means not related to each other .

so we can say BW and starvation is not related .

Hence your assumption " not satisfying bounded waiting means starvation? " is not correct as there is no relation holds between BW and starvation.

0
0

Here 2 lines are missing with original bakery algorithm. And that must make some difference in answering this question

Entering[i] = true;//missing in question
Number[i] = 1 + max(Number[1], ..., Number[NUM_THREADS]);
Entering[i] = false;//missing in question

for (integer j = 1; j <= NUM_THREADS; j++) {
        while (Entering[j]) { 
      /* nothing */ 
    }

     while ((Number[j] != 0) && ((Number[j], j) < (Number[i], i))) { 
      /* nothing */ 
    }
}

    <Critical Section>

    Number[i] = 0;
    

for loop executing one time, then where is wrap around condition here?

2
2

@srestha mam

for loop executing one time, has nothing to do with wrap around condition beacuse the wraparound is happening for the array contents of t[] or if we consider the snapshot of bakery algorithm provided by you , in the array Number[]

PS: Keeping in mind the comments of Sushant Gokhale sir.

1
1

@VS

yes , thank u :)

but still can we assume 2 process could get same id? So, deadlock satisfied?

In question it is given there are n processes P0 to Pn-1

So, every process should be distinct. rt?

0
0
@arjun sir @srestha.How is it possible for two process to have same t[i]? Are we assuming that after calling pmax a process is preempted and then some other process can get same pmax?
0
0
edited by

yes possible

then smallest index value will preempt first

chk this

24
24
but here why progress is not satisfied?
0
0
Deadlock => No progress.

Assume that I have two process P1 and P2.And both gets same t[i] value.(I dont know how they will get.But it is mentioned.May be because after pmax P1 is prempted and P2 comes and get same pmax)

Now both will stuck at while loop.And critical section is free and the two processes are trying to enter but no-one will ever get a chance.So progress not satisfied

In simple, Deadlock => No progress always holds
4
4
How to decide atomicity, if an expression is c = a + b would this be atomic or addition and assignment would be considered seperately?
0
0

@prayas

user level programs doesn't have atomicity

semaphores are kernel level programs, so they have atomicity

4
4
what does array c imply here? Pls explain...
0
0
edited by

When there is deadlock, all the processes are in the entry section. Now as per the definition of the progress in Galvin, “If no process is executing in its critical section ...those processes that are not executing in their remainder sections can participate in deciding which will enter its CS next and this section cannot be postponed indefinitely


I do not get the concept as to how Deadlock $\implies$ No Progress

Is it due to the underlined part of the definition?

1
1
@hitechga, yes
1
1

As it is not assumed that the statement t[i]=pmax()+1 is atomic more than one process can take up the same value. And if all the processes take up the same value then the system will be in deadlock right? 

And if that statement is atomic then no deadlock right?

@Sachin Mittal 1 sir please help.

0
0
edited by

@HitechGa @Sachin Mittal 1 @Shaik Masthan @Deepak Poonia Sir can we think in this way,

→ We can see initial values of c[i] are not given. So, we can initialize c[0] to c[n-1] with 1.

Acc to the definition of progress “ if no process is executing in it’s critical section and some processes wish to enter the CS , then those processes that are not executing in their remainder sections can participate in deciding which will enter its CS next and this section cannot be postponed indefinitely”.

Let’s say process P1 wants to enter CS and other processes don’t want to enter the CS. So value of j will be {0,2,3….,n-1} and while(c[j]) will be true for all values of j and P1 can’t enter the CS. P1 has to wait indefinitely for no reason. It has to wait for other processes. So, we can say progress is not guaranteed.


How Deadlock is possible?

-> Let's say there are only 2 processes P0 and P1 and both want to enter the CS.

t[i]= pmax (t[0],....,t[n-1])+1 this is not an atomic expression. 

R1 <-  pmax (t[0],t[1])

R1 <- R1 + 1

t[i] <- R1

P0                                                                                                 P1

c[0]=1

                                                                                                     c[1] = 1

R1 <-  pmax (t[0],t[1]) // R1 <- 1

                                                                                          R2 <-  pmax (t[0],t[1]) // R2 <- 1

R1 <- R1 + 1 // R1<- 2

                                                                                          R2 <- R2 + 1 // R2<- 2

t[0] <- R1 // t[0] <- 2

                                                                                          t[1] <- R2 // t[1] <- 2

c[0]=0;

                                                                                            c[1]=0;

value of j is 1

while (c[1]); // false             

while (t[1] != 0 && t[1] <=t[0]); // condition satisfied

                                                                                                                value of j is 0

                                                                                                                 while (c[0]); // false             

                                                                                while (t[0] != 0 && t[0] <=t[1]); // condition satisfied

 Both get stuck and can't proceed. So these processes are in deadlock bcz t[j] =t[i].


Bounded waiting is also guaranteed.

t[0]t[1]t[2]t[3]t[4]
23765

c[0]c[1]c[2]c[3]c[4]
23765


1. First P0 will enter the CS bcz (t[j= 1,2,3,4] > t[0]) and make t[0]=0.

t[0]t[1]t[2]t[3]t[4]
03765

2. P1 will enter the CS bcz ((t[j= 2,3,4] > t[1]) and t[j=0] == 0 ) and make t[1] =0.

t[0]t[1]t[2]t[3]t[4]
00765

3. P2 will enter the CS bcz ((t[j=3,4] > t[2]) and t[j=0] == 0, t[j=1] == 0 ) and make t[2] =0.

t[0]t[1]t[2]t[3]t[4]
00065

4. P3 will enter the CS bcz ((t[j=4] > t[3]) and t[j=0] == 0, t[j=1] == 0, t[j=2] =0) and make t[3] =0.

t[0]t[1]t[2]t[3]t[4]
00005

3. P4 will enter the CS bcz ( t[j=0] == 0, t[j=1] == 0, t[j=2] =0, t[j=3] =0 ) and make t[4] =0.

So, all processes can enter the CS(critical section), and bounded waiting is satisfied. Bounded waiting means providing a bound time of waiting for each process. 


Mutual Exclusion is also satisfied.

P1, P2, P3, and P4 want to enter CS.

Let's say P0 is in CS it means (c[j = 1,2,3,4 ] =0) and (t[j=1,2,3,4] > t[i=0])

P1 can't enter into the CS bcz while (t[j=0] != 0 && t[j=0] <=t[1]) will be satisfied and will keep on checking this condition. Similarly for P2,P3,P4. In CS there will be atmost 1 process at a time. So, mutual exclusion is satisfied.

1
1
Sir there will never be a case when "[i]

== t[j]" then how can it go in deadlock.
0
0
55 votes
55 votes

Given question is a wrongly modified version of actual bakery algorithm, used for N-process critical section problem.

Bakery algorithm code goes as follows : (as in William stalling book page 209, 7th edition)

Entering[i] = true;
Number[i] = 1 + max(Number[1], ..., Number[NUM_THREADS]);
Entering[i] = false;

for (integer j = 1; j <= NUM_THREADS; j++) {
    // Wait until thread j receives its number:
    while (Entering[j]) { 
      /* nothing */ 
    }

    // Wait until all threads with smaller numbers or with the same
    // number, but with higher priority, finish their work:
    while ((Number[j] != 0) && ((Number[j], j) < (Number[i], i))) { 
      /* nothing */ 
    }
}

    <Critical Section>

    Number[i] = 0;
    /*remainder section */

code explanation:

The important point here is that due to lack of atomicity of max function multiple processes may calculate the same Number.

In that situation to choose between two processes, we prioritize the lower process_id.

(Number[j], j) < (Number[i], i)) this is a tuple comparison and it allows us to correctly select only one process out of i and j.but not both (when Number[i] = Number[j] )


Progress and Deadlock:

The testing condition given in the question is while (t[j] != 0 && t[j] <=t[i]); which creates deadlock for both i and j ( and possibly more) processes which have calculated their Numbers as the same value. C and D are wrong.


Bounded waiting :

If the process is waiting and looping inside the for loop. Why is it waiting there ? Two reasons,

  1. Its number value is not yet the minimum positive value.
  2. Or, its Number value is equal to some other's Number value.

Reason1 does not dissatisfy bounded waiting , because if the process has the Number value = 5 then all processes having less positive Number will enter CS first and will exit. Then Process will definitely get a chance to enter into CS. 

Reason2 dissatisfy bounded waiting because assume process 3 and 4 are fighting with the equal Number value of 5. whenever one of them (say 4) is scheduled by the short term scheduler to the CPU, it goes on looping on  $Number[3] \Leftarrow Number[4]$ .Similarly with process 3 also. But when they are removed from the Running state by the scheduler , other processes may continue normal operation. So for process 3 and 4 although they have requested very early, because of their own reason, other processes are getting a chance of entering into CS. B is wrong.

note : in this all the processes go into deadlock anyway after a while.


How mutual exclusion is satisfied ?

Now we assume all processes calculate their Number value as distinct.

And categorize all concurrent N processes into three groups;

  1. Processes which are now testing the while condition inside the for loop.
  2. Processes which are now in the reminder section. 
  3. Processes which are now about to calculate its Number values.

 In Category 1, assume process wins the testing condition, that means no one else can win the test because has the lowest positive value among the 1st category of processes.

Category 3 processes will calculate Number value more than the Number of using max the function.

Same goes with Category 2 processes if they ever try to re-enter. 


detail of bakery algorithm Link1 and Link2 and Link3_page53

 


 

For curious minded people:

https://cacm.acm.org/magazines/2022/9/263810-deconstructing-the-bakery-to-build-a-distributed-state-machine/fulltext

 

 

edited by
by

4 Comments

amazing explanation. That little confusion has gone away now. Thanx a lot.
0
0
@Akriti

Bounded waiting means after a Process request to go in CS, how many other process it allows to enter CS before it going to CS.

Here as deadlock possible , so this bound is infinite. So, BW dissatisfied
2
2

@srestha So it  means that as per this problem Bounded Waiting is considered as no of processes that a given process has to wait in succession to enter the CS again.

Like if we have process  P0 to P9999 then for Process P0(after its first round of execution) to enter again,has to wait until all the processes from P1 to P9999 has completed.Then only it can enter?

Is this waiting for P1 considered to be unbounded? i.e.,BW here is considered based on no. of processes but not on wait time?

Please clarify.

0
0
34 votes
34 votes
1.do {
2.    c[i]=1; 
3.   t[i]= pmax (t[0],....,t[n-1])+1;
4.    c[i]=0;
5.    for every j != i in {0,....,n-1} {
6.        while (c[j]); 
7.        while (t[j] != 0 && t[j] <=t[i]);
8.   }
9.    Critical Section;
10.    t[i]=0;
    
11.    Remainder Section;
    
12.}  while (true);

The above code is N process synchronization solution (Lamport's bakery algorithm)

For detailed information on it , I'd request you to please refer below link and then come to this question.It'd really help you to understand the algorithm in detail and then the below solution would become very trivial.

https://www.youtube.com/watch?v=3pUScfud9Sg

Lines 2-4 can be termed as doorway for getting the token number. Each process which wants to enter into Critical section, gets a token number similar to restaurant system and wait for his token to be announced.

2. c[i]=1; //announce that process Pi is now ready to take it's token number


3. t[i]= pmax (t[0],....,t[n-1])+1; //get token number

This line 3, can be further decomposed as

(a)LOAD R1  MAX[0..N-1] where max[0..n-1] is the same function which returns max of t[0..n-1]

(b) INCR R1

(c) STORE t[i]


4. c[i]=0; //announce that process Pi has taken it's token number.

Now consider a scenario when processes P1, P2 P3 and P4 want to enter critical section

now since in question, there is no way indicated that line 3 is atomic, two or more processes may get same token value.

So when P1, P2 , P3 and P4 want to enter say

below is an instance of array t[i] for the above processes.

P1 P2 P3 P4
0 1 1 1

As you can see, processes P2, P3 and P4 have got same t[i] value because line 3 was not atomic in nature.

Suppose their respective C[i] values are all zero as of now,

then 

Say P2 wanted to enter C.S. so it executed below line

while (t[j] != 0 && t[j] <=t[i]);

Now for J!=i, P2 scans for processes P1,P3 and P4 and checks if one any one of them would have it's timestamp less than or equal to it's own timestamp or token value.

P2 finds this to be true and loops at this condition.

Similarly processes P3, and P4 keep looping at while condition.

Suppose now P1 wants to enter C.S. and now t[i] array is as follows

P1 P2 P3 P4
2 1 1 1

P1 gets a timestamp value of 2.

Now when P1 gets to execute line number 7

while (t[j] != 0 && t[j] <=t[i]);

it finds that all other processes have their timestamp non-zero and have value less than that of token of P1, so P1 also waits.

As, we can see now a deadlock is created. No process will now be able to proceed.

Hence option (d) is ruled out.

When there is deadlock possibility, progress is definitely not satisfied, because progress states that in a finite amount of time decision must be taken which process will get to execute CS next.

When a deadlock is present, bounded waiting may or may not be satisfied.

Good Read on the above point is : https://cs.stackexchange.com/questions/63730/how-to-satisfy-bounded-waiting-in-case-of-deadlock

Let's examine case of bounded waiting here.

Consider we have  4 processes and they have been assigned timestamp as below

$P_0$ $P_1$ $P_2$ $P_3$
1 2 3 3

Now, process $P_0$ and $P_1$ will get chance to execute CS, and after they complete the $t[i]$ values are as under:

$P_0$ $P_1$ $P_2$ $P_3$
0 0 3 3

Now, process $P_2$ and $P_3$ would keep looping in while loop and never get to execute CS.

Now, consider a scenario where we have 7 processes which have been assigned t[i] values as under

$P_0$ $P_1$ $P_2$ $P_3$ $P_4$ $P_5$ $P_5$
1 2 3 4 5 6 6

 

Now, processes $P_0,P_1,P_2,P_3,P_4$ will get to execute CS but $P_5,P_6$ won't get a chance to execute CS.

Similarly another scenario can be there where some processes are allowed into CS, who have got distinct t[i] values and the processes with the same t[i] values(say t[i]=k) and the processes having t[i] values greater than the k, won't get a chance to execute CS.

Now I ask you this question. Since this code is a solution for "N" processes, is your "K" bounded?

And bounded waiting says that there exist a bound on the number of times that other processes are allowed entry into CS between the time the process placed its request for CS and the time the request for CS is finally granted.

Does, your code guarantee that under any circumstances, for a constant k , after k processes execute finally process $P_i$ would get a chance to get into CS?

The answer is NO. K is variable here and not constant.K depends on the moment when two or more processes take the same t[i] value.

So, Bounded waiting is NOT Satisfied.

 

Now let's prove option (a)

Consider that again we have four processes and below is their token value

P1 P2 P3 P4
0 1 0 0

Suppose only now P2 wants to enter C.S. so, it gets its token value as 1.

When P2, executes line number 7, it succeeds as no other process has t[i] non-zero.

P2 enters into C.S.

Now suppose P2 is executing in C.S. and it is preempted.

P3 now wants to enter C.S. and gets it token

P1 P2 P3 P4
0 1 2 0

When P3 will execute line number 7, it will find that process P2 has it's token value non-zero and it is less than it's own token value(1<2). So, P3 will now wait.

Hence, Mutual Exclusion is satisfied.

Answer- Option (a)

edited by

4 Comments

@Ayush Upadhyaya I think Bounded Waiting is not satisfied since a process can apply to get the token any number of times after exiting its critical section. So bounded waiting is not satisfied.

0
0

@Shaik Masthan commented that:

problem is, more than one process waiting for entering into CS, then it lead to DEADLOCK.

let P0 only want to enter into CS ===> it can enter.

while P0 is in CS, P1 and P2 in the for loop for waiting into the for loop.

P0 out from CS, then P1 and P2 both are strucked in the loop ===> DEADLOCK

A processes (P1 & P2) get stucked in while loop whenever a process ,say P0, exist whose non-zero token number is less than the current process. P1 & P2 comes out of the while loop when P0 makes its token 0 i.e it comes out of CS and resumes while loop from where it get stucked.

0
0

Does, your code guarantee that under any circumstances, for a constant k , after k processes execute finally process Pi would get a chance to get into CS?

We can guarantee that it will be n-bounded. Where n is the number of processes, can’t we?

0
0
2 votes
2 votes

This Question is a little bit wrong modification of bakery algorithm. You can learn about bakery algorithm from this 15 min video of NPTEL. 

NPTEL - Bakery Algorithm

https://youtu.be/3pUScfud9Sg

2 Comments

Wrong modification or modification?
0
0
yes it is wrong modification, because in the correct modification of original bakery algorithm, it is free from deadlock, it insures progress as well as bounded waiting also. for further information you can refer to the answer provided by the arjun sir.
0
0
Answer:

Related questions