Linux wait函数用法

wait 函数是用于等待子进程结束并获取子进程的终止状态的系统调用。它在父进程中使用,用于等待其子进程终止并获得子进程的退出状态。

函数原型:

pid_t wait(int *status);

status 是一个指向整型的指针,用于存储子进程终止时的退出状态,可以通过它获取子进程的退出状态。

wait 函数返回已终止子进程的进程ID,如果没有已终止的子进程,wait 会一直阻塞父进程,直到有子进程结束为止。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>

int main() {
    pid_t val;
    printf("PID before fork(): %d\n",getpid());
    val = fork();
    if(val == 0) {
        printf("I am the child process, PID is %d\n", getpid());
    } else if(val > 0) {
        wait(NULL);
        printf("I am parent process, PID is %d\n", getpid());
    } else {
        printf("Error");
    }
}

image-20231121153009314

[【精选】Linux中wait()函数_linux wait-CSDN博客](https://blog.csdn.net/modi000/article/details/124605195?ops_request_misc=%7B%22request%5Fid%22%3A%22170054928216800182745173%22%2C%22scm%22%3A%2220140713.130102334…%22%7D&request_id=170054928216800182745173&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-124605195-null-null.142v96pc_search_result_base3&utm_term=linux wait&spm=1018.2226.3001.4187)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

golemon.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值