2
我試圖在運行fork()
命令後打印pid
的進程。這裏是我的代碼 -在外殼的叉指令打印進程ID
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0)
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
else
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
return 0;
}
這是回答我getting-
I am the parent.My pid is 2420. My childs pid is 3601
I am the child.My pid is 3601 .My parents pid is 1910
爲什麼在2號線的父母不ID。爲什麼2420
我收到1910
我怎樣才能得到這個值?
'INT PID;'應該被定義爲'將爲pid_t PID;'有父'waitpid函數()'上了孩子,所以當孩子叫'getppid()'父仍在運行。 'fork()'函數有三個返回值:= 0表示孩子,> 0表示父母,<0表示錯誤。總是檢查所有三個條件,不要認爲操作成功 – user3629249