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

******PLEASE EXPLAIN HOW YOU GOT THE ANSWER, because i have my own answers to co

ID: 3802654 • Letter: #

Question

******PLEASE EXPLAIN HOW YOU GOT THE ANSWER, because i have my own answers to compare it too and how you obtained your answer.

Operating System Concurrent processes PROBLEM SAMPLE 2 Q5 (18 points)

N customers enter the bakery to buy cookies. Each customer gets its turn[i], by computing a next number, and waits to be served. The clerk uses a counter to keep track of the served customers. The clerk serves the customer whose turn[i] is equal to the counter. After each serve( ), the clerk increments the counter. When the counter reaches N, the clerk considers that it is done and leaves for home.

* Initial condition: (Shared variables)

turn[i] = 0 i = 1, .. , N (N is initialized to 10); number= 0; served[i]=0; counter = 0;

* Customer program

customer() {
number++;
turn[i]=number;
while(!server[i]) {
busyWait };
getServed();
go home;
}

* Clerk program

clerk() {
while (counter < N) {
counter++;
for(int j=1;j<=N;j++) {
if(counter==turn[j]) {
served[j]==True;
serve();
served[j]=False;
}//false
}//forloop
}//while leave;
}//clerk

All customer( ) and clerk( ) processes execute concurrently.

a) Is it possible for two customers to compute the same number? Explain. If yes, give the execution sequence that will show it.

b) Under the hypothesis that each customer has computed a different number value, is it possible for customers to compete for the same cookies (because their turn[i] is the same)? Explain. If yes, give the execution sequence that will show it.

c) Under the hypothesis that all customers have their turn[i] set before the clerk starts executing, is it possible for a customer to starve (busy wait forever)? Explain. If yes, give the execution sequence that will show it.

d) Is it possible for the clerk to never go home? Explain. If yes, give the execution sequence that will show it.

e) If there are N cookies on the shelf, is it possible for the clerk to run out of cookies before all the customers got served? Explain. If yes, give the execution sequence that will show it.

Reformat based on expert feedback.

Explanation / Answer

a)Yes,it is posible for two costumers to compute the same number.there is no lock on the turn[].

when two costumers execute the customer process concurrently,suppose the customers be A and B,

A.customer() and B.customer() are executed at the same time,both of them see number as 0,and the statement number++ sets number to 1 and the statement turn[0]=1 is set by both the processes at the same time.

b)Number variable which is the critical section has to be locked before setting and unlocked after setting.

Mutual exclusion is violated here,where by the customers are accessing are not prevented from accessing the critical section at the same time.

Hence if the customers have executed the customer process at the same time,

A.customer()

B.customer()

clerk() will have the counter value 1 which is the turn value of both A and B.

so,there is a race condition here,and the first one to execute customer() wll get his order processed by clerk.

c)suppose the customer C has seen the number value as 4.

before he executes the customer process and setting the value to the turn,suppose another customer D has changed the value to 5.

C has observed the non updated value of the number as the writing of the number has not yet completed by the other process D.It has marked its turn value as 4.But the clerk() process has served the turn 4 and waits for the higher value of turn.C will wait for ever to get its order,which is not done forever.

d)clerk() process will continue basing on the value of the counter.If the counter reaches the maximum number it is assumed that all the customers have been served and the clerk goes home.Suppose the customer that last requested the cookie has the number value N-1.counter will have the value N-1. And the customer will be served the cookie.

However clerk() will wait for the customer with the turn set to N to come last.But as we have seen previously,one of the customer has set a lesser number as turn by mistake and he is waiting for ever.

Clerk() also waits for ever as the counter will not get incremented to N.it will be in N-1 only.

e)There may be a chance of one customer getting serviced two times before the counter got incremented.Then the total number of cookies get exhausted leaving many out of service.