我正在尝试使用fork系统调用创建一个进程,然后等待子进程 . 我使用了以下内容:
waitpid(pid,&status,0);
1)第一个问题是状态向左移8位,例如,如果子进程返回1,则waitpid函数将状态变量中状态的值返回为256.请让我知道它为什么这样做那 .
2)根据手册,waitpid等待子进程改变状态 . 但后来它还说:
“wait()系统调用暂停执行调用进程,直到其子节点之一终止 . 调用wait(&status)等效于:
waitpid(-1,&status,0);“
我在这里有点困惑,waitpid和wait调用是等待状态更改还是子进程终止 . 请明确这一点 .
第三个论点中的零点是什么规定的?
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);
}
谢谢
1 回答
status
参数编码的不仅仅是子代码的退出代码 . 从man waitpid
:main waitpid
解释了第三个参数的作用 .国家变化的定义非常精确和狭隘 . 从
man waitpid
:睡觉不是状态的改变 . 被
SIGSTOP
/SIGTSTP
拦住了 .