📄 philosopher5.c
字号:
/*在本次实验中,为了模拟算法应用的环境,将每个phiolosopher左右的两把叉子作为一个资源对待,且使用书上时间戳机制*/
#include "philosopher5.h"
void philosopher(int i)
{
printf("myID(%d) = %d\n",myID,getpid());
while(1)
{
think(i);
take_fork(i);
eat(i);
put_fork(i);
}
}
main()
{
int i;
pid_t pid;
initforks();
for(i = 0; i < N; i++)
{
if( (pid = fork()) < 0)
err_sys("fork error");
else
{
if( (shmptr = (char*) shmat(shmid,(char*)0,0) ) ==(char*)-1)
{
perror("get mermory shmat in child");
exit(4);
}
pids = (struct PID*)shmptr;
if(pid == 0) /*in child*/
{
myID = i;
init_child();
sleep(2);
philosopher(i);
exit(1);
}
else pids->P[i] = pid;
}
}
wait(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -