background image
 
-2- 
开始
信号量chair[4]赋初值1
信号量chopstick[4]赋初值1
信号量counter赋初值4,限定
每次只能4个人坐席
create10个进程,表示10个
哲学家
join这10个进程,直到运行
结束
结束
 
5.2、添加函数的代码(所用到的各子函数) 
void p_operate(int semid,ushort semnum)  

 
struct sembuf thesops; 
    thesops.sem_num=semnum; 
    thesops.sem_op=-1;  
 
thesops.sem_flg=SEM_UNDO; 
    //thesops.sem_flg=0;  
    semop(semid,&thesops,1);  

void v_operate(int semid,ushort semnum)  

 
struct sembuf thesops; 
    thesops.sem_num=semnum; 
    thesops.sem_op=1;  
 
thesops.sem_flg=SEM_UNDO; 
    //  thesops.sem_flg=0;  
    semop(semid,&thesops,1);  

 
void think(int argi) 

 
printf("I'm 
philosopher 
#%d,I 
will 
think 
for 
%d 
seconds\n",argi,argi+THINKTIME);