Operating Systems - Problem Set 2 with Solution | COMP 310, Assignments of Operating Systems

Material Type: Assignment; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-fz7
koofers-user-fz7 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 310: Problem Set 2: Solutions
All problems from OS Concepts by Silberschatz, Galvin, and Gagne
7.4 Proof that Dekker algorithm satisfies the three requirements for critical sections.
First, we prove mutual exclusion. We assume some process Pi(for i= 1or2is running in the CS
and show the other process Pj, i 6=jcannot enter the CS until Pileaves.
Since Piis in the CS, then flag[i]must be true. The only way for Pjto get into the CS is to break
out of the while loop, which requires that f lag[i]be false. But we know that this cannot be true,
so there is no way for Pjto enter the CS.
Next, we prove progress. A process Pican enter the CS flag[j]is false. If Pjdoes not want to
enter the CS and is not inside the CS, then flag[j]will be false and Piwill be free to enter. So Pj
cannot prevent Pifrom entering the critical section. If both Piand Pjwish to enter, then at least
one will be allowed to enter eventually because the process that does not have the turn will set its
flag to false and then be forced to wait until the other process has completed the CS and changes
the value of turn.
Finally, we prove bounded waiting. We show that if both Piand Pjwant to enter the CS, then Pi
can enter the CS at most once before Pjenters the CS. Assume Pienters the CS. When it exists,
it will set turn to j. Because Pjwants to enter the CS, f lag[j]will be true, so Piwill again enter
the while loop. If Pjhas not yet run through the CS, turn will still equal j. So Piwill neter the
inner while loop and will not get out of the loop until Pjsets turn back to i, which is after the CS.
So there is no way for Pito enter the CS again until Pjenters and leaves.
5.1 Proof that Eisenberg and McGuire’s algorithm satisfies CS requirements.
First we prove mutual exclusion. We assume some process Pcis in the CS, and show that any other
process Pa(a6=c)cannot enter the CS.
To enter the CS, Pamust satisfy both conditions of the if statement. We show by contradiction that
j >=ncannot be true so Pacannot enter the CS.
Assume j >=nfor some process Pa. This means the while loop immediately before the for loop
must have considered all processes Pj(j6=a)and found that flag[j]6=incs. Because c6=awe
know that flag[c]6=incs. But the fact that Pcis in the CS means that flag[c] = incsthus we have
a contradiction and our original assumption (j >=n) must be false. Therefore Pacannot enter the
CS if Pcis inside.
1
pf3
pf4
pf5

Partial preview of the text

Download Operating Systems - Problem Set 2 with Solution | COMP 310 and more Assignments Operating Systems in PDF only on Docsity!

COMP 310: Problem Set 2: Solutions

All problems from OS Concepts by Silberschatz, Galvin, and Gagne

7.4 Proof that Dekker algorithm satisfies the three requirements for critical sections.

First, we prove mutual exclusion. We assume some process Pi (for i = 1or 2 is running in the CS and show the other process Pj , i 6 = j cannot enter the CS until Pi leaves.

Since Pi is in the CS, then f lag[i] must be true. The only way for Pj to get into the CS is to break out of the while loop, which requires that f lag[i] be false. But we know that this cannot be true, so there is no way for Pj to enter the CS.

Next, we prove progress. A process Pi can enter the CS f lag[j] is false. If Pj does not want to enter the CS and is not inside the CS, then f lag[j] will be false and Pi will be free to enter. So Pj cannot prevent Pi from entering the critical section. If both Pi and Pj wish to enter, then at least one will be allowed to enter eventually because the process that does not have the turn will set its flag to false and then be forced to wait until the other process has completed the CS and changes the value of turn.

Finally, we prove bounded waiting. We show that if both Pi and Pj want to enter the CS, then Pi can enter the CS at most once before Pj enters the CS. Assume Pi enters the CS. When it exists, it will set turn to j. Because Pj wants to enter the CS, f lag[j] will be true, so Pi will again enter the while loop. If Pj has not yet run through the CS, turn will still equal j. So Pi will neter the inner while loop and will not get out of the loop until Pj sets turn back to i, which is after the CS. So there is no way for Pi to enter the CS again until Pj enters and leaves.

5.1 Proof that Eisenberg and McGuire’s algorithm satisfies CS requirements.

First we prove mutual exclusion. We assume some process Pc is in the CS, and show that any other process Pa(a 6 = c) cannot enter the CS.

To enter the CS, Pa must satisfy both conditions of the if statement. We show by contradiction that j >= n cannot be true so Pa cannot enter the CS.

Assume j >= n for some process Pa. This means the while loop immediately before the for loop must have considered all processes Pj (j 6 = a) and found that f lag[j] 6 = incs. Because c 6 = a we know that f lag[c] 6 = incs. But the fact that Pc is in the CS means that f lag[c] = incs thus we have a contradiction and our original assumption (j >= n) must be false. Therefore Pa cannot enter the CS if Pc is inside.

Next we show progress. First, a process in its remainder section cannot prevent another process from entering the CS. If a process is not in the CS and not waiting to enter the CS, then its flag is set to idle. Only processes that are not idle can hold up the incrementing of j in the first loop. Only proocesses whose flags are incs can affect the j >= n condition in the if statement, and an idle process can never cause the second condition in the if statement to fail, so an idle process can never keep another process from entering the CS.

Second, at least one process who is waiting to get into the CS will alays be allowed in (if there is no other process in the CS). The first while loop will set turn equal to the index of the first process whose flag is not idle. Because we assume no process is in the CS, this will be one of the processes whose flag is set to wantin. Then, this process will make it through the second while loop because it will be the only process whose flag is set to incs—all others will be stuck in the first while loop with their flags set to wantin. So it will be allowed to enter the CS.

Finally we prove bounded waiting. We show that a process who wants into the CS will have to wait for at most n − 1 processes to go through the CS before it is allowed to enter. Processes waiting to enter the CS get stuck in the first while loop. When a process exists the CS, it sets turn equal to the next process that is not idle. It searches the processes in order of index, so it can never skip a process who wants into the CS. This will allow the next process (in order of index) to break out of the first while loop. Because turn moves forward in order of index and can never skip a process waiting to get into the CS, a process will have to wait for at most n − 1 other processes before its turn to enter the CS.

7.9 Smokers

Semaphore Solution

Semaphore paperAndMatches = 0; Semaphore matchesAndTobacco = 0; Semaphore tobaccoAndPaper = 0; Semaphore done = 1;

Agent { while ( 1 ) { done.wait(); int choice = rand( 0, 3 ); if ( choice == 0 ) { paperAndMatches.signal(); } else if ( choice == 0 ) { matchesAndTobacco.signal(); } else { tobaccoAndPaper.signal();

ingredients[0] = TRUE; ingredients[1] = TRUE; ingredients[2] = TRUE;

int supply = rand( 0, 3);

// Don’t supply one ingredient and signal the smoker who has that one ingredient[supply] = FALSE; ingredientCVs[supply].signal();

doneVar = FALSE; }

void getIngredient( int i ) { while ( ingredients[i] ) { ingredientCVs[i].wait(); } // grab stuff doneVar = TRUE; done.signal(); } }

SmokingTable st;

// i corresponds to the ingredient the smoker has Smoker( int i ) { while ( 1 ) { st.getIngredient( i ); // smoke } }

Agent() { while( 1 ) { st.supply(); } }

7.17 Alarm

Monitor Alarm { int ellapsedTime = 0;

Condition sleeper;

void delay( int time ) { int wakeUpTime = time + ellapsedTime; while ( wakeUpTime < ellapsedTime ) { sleeper.wait(); } }

void tick() { ellapsedTime++; sleeper.broadcast(); } }