作者minhong (minhong)
看板Programming
標題Re: [問題] FORK()與PID
時間Sat Oct 21 22:20:44 2006
※ 引述《mingchieh.bbs@bbs.cis.nctu.edu.tw (Bug J.)》之銘言:
: getppid() -> get parent pid
it works, but not always .
程式碼:
#include <stdio.h>
#include <unistd.h>
int main()
{
if ( fork() == 0 )
{
printf("PID of child process is %d.\n", getpid() );
printf("PID of my parent is %d.\n", getppid() );
}
else
{
printf("PID of parent process is %d.\n", getpid() );
}
return 0 ;
}
執行結果:
(正確)
PID of child process is 20107.
PID of my parent is 20106.
PID of parent process is 20106.
(出問題)
PID of parent process is 20110.
PID of child process is 20111.
PID of my parent is 1.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.174.198.169
推 sekya:那是因為parent執行結束..由init接手..221.169.193.182 10/22 01:12
→ sekya:再加一個wait(NULL);在parent的最後就行了221.169.193.182 10/22 01:13
推 minhong:感謝回應與指導 ^^220.131.138.110 10/22 19:28