c - waitpid() 的参数

标签 c fork pid waitpid

#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/

相关文章:

C 中的 clone() 系统调用和共享

linux - 如何使用 bash 锁定文件

php - 如何获取当前用户在MYSQL中的第一个进程号?

linux - 进程 ID 在 Linux 中是按顺序分配的吗?

无法比较字符

c - 基于不透明结构的 C API 的单元测试

c - 写入管道总是失败

process - Visual Studio Code,调试子进程不起作用

c - 加载文件时出现段错误(核心转储)

c - 简单的宏导致编译错误