wait

相关函数:waitpid, fork

头文件:#include <sys/types.h>    #include <sys/wait.h>

定义函数:pid_t wait (int * status);

函数说明:wait()会暂时停止目前进程的执行, 直到有信号来到或子进程结束. 如果在调用wait()时子进程已经结束, 则wait()会立即返回子进程结束状态值. 子进程的结束状态值会由参数status 返回, 而子进程的进程识别码也会一快返回. 如果不在意结束状态值, 则参数 status 可以设成NULL. 子进程的结束状态值请参考waitpid().

返回值:如果执行成功则返回子进程识别码(PID), 如果有错误发生则返回-1. 失败原因存于errno 中.

范例
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
    pid_t pid;
    int status, i;
    if(fork() == 0)
    {
        printf("This is the child process. pid =%d\n", getpid());
        exit(5);
     }
    else
    {
        sleep(1);
        printf("This is the parent process, wait for child...\n";
        pid = wait(&status);
        i = WEXITSTATUS(status);
        printf("child's pid =%d . exit status=^d\n", pid, i);
    }
}

执行:
This is the child process. pid=1501
This is the parent process, wait for child...
child's pid =1501, exit status =5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值