#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int status;
int pid = fork();assigned to variable "pid"
if(pid == 0)
{
printf("I am the child with pid = %d\n", getpid());
}
else
{
printf("I am the parent with pid = %d\n", getpid());
waitpid(pid, &status, 0); // line 51
}
return 0;
}
在第 51 行中,请澄清“pid”参数。这个过程会等待什么?
最佳答案
fork
为子进程返回 0
,出错时返回 -1
,为父进程返回其他内容。 else
开始父进程的部分,这意味着 waitpid
中的 pid
包含子进程的 PID。
引用man wait
:
The
waitpid()
system call suspends execution of the calling process until a child specified bypid
argument has changed state. By default,waitpid()
waits only for terminated children, but this behavior is mod- ifiable via theoptions
argument [...]
简而言之,waitpid
等待子进程终止。
关于c - waitpid() 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934145/