Homework 3 Solution - CS423UG Fall 2005
1.
int boatLocation=0;
int boatCount=0;
ConditionVariable boatarrived, boatfull;
monitor Boat{
ArriveAtBoat(int location){
while(true){
if(boatlocation==location && boatcount < 3){
boatcount++;
if(boatcount < 3)
boatfull.wait();
else boatfull.broadcast();
return;
}
else boatarrived.wait();
}
}
GetOffOfBoat(int location){
boatcount--;
if(boatcount==0){
boatlocation=1-location;
boatarrived.broadcast();
}
}
2.
extern int N;
int turn=0;
semaphore mutex=1;
static semaphore everyonereached[2]={0,0};
static int count[2]={0,0};
void barrier(int processid){
down(mutex);
barrierturn(processed, turn);
}
void barrierturn(int processed, int thisturn){
count[thisturn]++;
if(count[thisturn]!=N){
up(mutex);
down(everyonereached[thisturn]);
}
else{
count[thisturn]=0;
for(i=1; i<=N-1; i++)
up(everyonereached[thisturn]);
turn=1-turn; // global turn updated here: next barrier
will
// use new turn value
up(mutex);
}
3.
No matter what x is, A's needs for resource type 5 can never be satisfied
because there aren't enough instances of that resource type. The system is
unsafe.
4.
Order all accounts by their (unique) id (perhaps id=account number). Each
process locks accounts only in increasing order of the account id's. This
can never result in a deadlock since the Circular Wait condition will never
be satisfied.