gpt4 book ai didi

c - waitpid() 的参数

转载 作者:行者123 更新时间:2023-11-30 21:25:07 27 4
gpt4 key购买 nike

#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 by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is mod- ifiable via the options argument [...]

简而言之,waitpid 等待子进程终止。

关于c - waitpid() 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com