我正在尝试使用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);
}
感谢