CS414 Prelim 1.�
Four problems
One lane bridges have a weight limit.� The bridge can only hold 2 cars at one time.
Assume that cars are processes.� Modify the readers and writers monitor (code
is given below to remind you how it looks) into a new monitor with four methods
��� int
rcnt = 0
condition wantread
public StartRead() |
public StartWrite() |
public EndRead() { |
public EndWrite() |
int scount=0, ncount=0, nwaiting=0, swaiting=0;
condition wantsouth, wantnorth;
public go_north_wait() |
public
go_south_wait() |
public go_north_done() ��� { ������� ncount--; ������� if (nwaiting
> 0) ����������� wantnorth.signal(); ����������� wantsouth.signal(); |
public
go_south_done() ��� { ������� scount--; ������� if (swaiting
> 0) ����������� wantsouth.signal(); ����������� wantnorth.signal(); |
�
2.�� Below is the Bakery algorithm, using the same code and notation we employed
in class.� Recall that (a,x)<(b,y)
means �(a < b) or (a=b and x<y)�. ��Assume that N=6
#define ���� true ��������������� 1
#define ���� false �������������� 0
int number[N],
choosing[N];� /* Initially number[i] = 0,
choosing[i] = false */
�����
Process Pi:
����� while (1) {
����������������� ��� do
something else
choosing[i] = true;
number[i] = max(number[0]�number[N-1])+1;
choosing[i] = false;
for(k = 0; k < N; ++k) {
while(choosing[k] ) loop;
while((number[k]!=0)&&((number[k],k)
< (number[i],i))) loop;
}
number[i] := 0;
}
Suppose that
a)
process 0 is in the �do
something else� code
b)
process 2 enters the
critical section
c)
processes 1
Now suppose
that process 0 finishes the �do something else code� while process 2 is still
in the critical section. �The next line
that process 0 will execute is �choosing[0] = true;��
Which of the
following are possibilities?� Explain
Note: You only need to describe one scenario in
which the condition described can occur.�
Even if there are many other scenarios in which it would not
occur
a) Process 2 has not yet left the critical section, yet process 0 doesn�t
wait when it executes the while loop �while((number[2],2) < (number[0],0)) loop�
No, this cannot happen.� If Process 2 is still in the critical
section, number[2] = 5.� When Process 0 chooses its number, number[0] must be >= 6.�
Thus it will wait in the while loop until Process 2 exits the critical
section, setting number[2] to 0.
b) Process 2 is able to leave and re-enter the critical section before
Process 0 is able to enter it even once.
Yes, this is possible.� Consider the case in which Process 0 is
switched out by the OS before choosing its number.� Then if Process 2 is run until it picks a
number, before Process 0 has a chance to run again, Process 2 will have a lower
number and thus re-enter the critical section before Process 0 is able to enter
it.
c) Process 1 enters the critical section before process 0 enters it even
once.�
Yes, this is possible.� Suppose Process 1 has already picked a number
when Process 0 picks.� Then Process 1
will enter while Process 0 loops on
����������������������� while((number[1], 1)
< (number[0],0))
d) When process 0 enters the critical section, it prints number[0].� The value
is 1.
Yes, this is possible.� Suppose all other processes exit the critical
section, setting number[p]=0 before Process 0 has a
chance to pick a number.� Then when Process
0 picks, it will get
����������� number[0] = max(0,0,0�,0)+1;
����������� Thus number[0]==1 when Process 0
prints in its critical section.
e) Process 0 never enters the critical section, although other processes
continue to do so.
No, this is not possible.� Suppose Process 0 picks number[0]=
k.� It then proceeds to wait on each of
the other processes.� Eventually each
process will choose a number.� So Process
0 will eventually get through
����������� while(choosing[k])loop;� for each of the other processes.
Then, after each process
exits its critical section once (after Process 0 has picked its number), it
could try to reenter but would then pick a number k�>k.� Thus Process 0 will no longer wait on
while((number[k]!=0)&&((number[k],k)
< (number[i],i))) loop;
for each process.� Process 0 will
then be able to enter the critical section.
3.�� You are developing a new computer game
called �multiplayer scavenger hunt�.� A
scavenger hunt is a kind of race to be the first to discover a treasure.� The players are given clues which are
puzzles.� When you solve a clue (a
puzzle)
The game will maintain an
elaborate map of
Keep in mind that players
move from place to place one step at a time
1.
Mutual Exclusion � Only one
player can stand on a particular space at a time.� The closet has a maximum capacity of 1 so
that only one player can be in it at once.
2.
Hold and Wait � The space
that the player is standing on is still being used by the player while the
player is waiting to move to a new space.�
So if
3.
Circular Wait � Suppose player
1 is standing in a full room where player 2 wants to go to, and player 1 wants
to go into the full room where player 2 is standing.� This is an example of circular wait.� We can imagine n people all standing in a
circle waiting to move to the next position clockwise around the circle.
4.
No Preemption � No one can
move anyone else from where they are standing. ��No one can tell a player to stop waiting.
There is no deadlock.� The graph can be reduced, in the following
order:� P0, P3, P1, P6, P4, P2, P5
This graph cannot be reduced.� The entire graph is a non-trivial component.� The key element causing the deadlock is the
cycle consisting of P1, P0, and P5.
There are several different reduction orders, one of which is :
P2, P5, P3, P0,
P6, P1, P4
Again there are several different reduction orders, one of which is:
��������������� P5, P2, P0, P6, P1, P4, P3