我正在嘗試使用fork系統調用創建進程,然後等待子進程。我已經使用了以下內容:使用Linux系統調用等待的進程等待
waitpid(pid,& status,0);
1)第一個問題是狀態是8位向左移動,例如,如果子進程返回1,則waitpid函數返回狀態變量中狀態的值爲256.請讓我知道爲什麼它這樣做。
2)根據手冊,waitpid等待子進程改變狀態。但隨後又表示:
「等待()系統調用掛起調用進程的執行,直到 它的一個子終止呼叫等待(&狀態)相當於 到:
waitpid函數( - 1,& status,0);「
我在這裏有點困惑waitpid和等待調用是否等待狀態更改或子進程終止。請清楚這一點。
第三個參數中的零是什麼意思? 3)如果我把子進程置於睡眠狀態,那麼通過等待5秒等等,子進程的狀態不會變成等待狀態嗎?
以下是我的程序:
int main(int argc, char ** argv)
{
pid_t pid = fork();
pid_t ppp;
if (pid==0)
{
sleep(8);
printf ("\n I am the first child and my id is %d \n", getpid());
printf ("The first child process is now exiting now exiting\n\n");
exit (1);
}
else {
int status = 13;
printf ("\nI am now waiting for the child process %d\n", pid);
waitpid (pid, &status, 0);
printf ("\n the status returned by the exiting child is %d\n", status>>8);
}
printf("\nI am now exiting");
exit(0);
}
感謝