{
sem_init(&(chopstick[i]),0,1);
}
for(j=0;j{
phId[j]=j+1;
pthread_create(&(philosopher[j]),NULL,philosopherProc,(void*)&phId[j]);
usleep(100);
}
pthread_join(philosopher[0],NULL); //等待线程结束
pthread_join(philosopher[1],NULL);
pthread_join(philosopher[2],NULL);
pthread_join(philosopher[3],NULL);
pthread_join(philosopher[4],NULL);
return 0;
}
void sitthink(int id) //入席及思考函数
{
posNum[id-1]=id;
srand(time(NULL));
printf("philosopher %d has sitted and thinking.../n",id);
usleep(rand()%200); //思考随机一段时间
}
void dining(int id) //就餐函数
{
printf("philosopher %d is dining.../n",id);
position();
sleep(3); //就餐3秒钟
}
void leave(int id) //离开函数
{
printf("philosopher %d has finished eating and leaved/n",id);
posNum[id-1]=0; //位置清零
sleep(3);
}