?? 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 + -