



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Unknown 1989;
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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(); } }