有谁对操作系统原理熟悉?请教
题:读哲学家算法是否会发生饥饿(starvation)?若会应怎样改进?
Dining philosopher's problem
#define LEFT(i-1)%N
#define RIGHT(i-)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
typedef int semaphore
int state[N];
semaphore mutex=1;
semaphore s[N];
void philosopher(int i)
{
while(TRUE){
think( );
take-chop-stick(i);
eat( );
put-chop-stick(i);}
}
void teat(i)
{
if(state[i]==HUNGRY&&state[LEFT]!=EATING&&STATE[RIGHT]!=eating)
{state[i]=EATING;V(S[i]);}
}
void take-chop-stick(int i)
{ P(mutex);
state[i]=HUNGRY;
test(i);
V(mutex);
P(s[i]);
}
void-put-chop-stick(int i)
{ P(mutex);
state[i]=THINKING;
test[LEFT];
test[RIGHT];
V(mutex);
}
下题中是否发生饥饿?为什么?
var mutex,wrt:Semaphore;
readcount:integer;
mutex:=wrt:=1;
readcount:=0;
parbegin
Readeri:begin
Wait(mutex);
readcount:=readcount+1;
if readcount=1 then Wait(wrt);
Singnal(mutex);
读数据集;
Wait(mutex);
readcount:=reascount-1;
if readcount=0 then Signal(wrt);
Signal(mutex);
end
Writeri:begin
Wait(wrt);
写数据集;
Signal(wrt);
end
coend