Linux wait函数详解

wait和waitpid出现的原因
SIGCHLD
--当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止)
--子进程退出时,内核将子进程置为僵尸状态,这个进程成为僵尸进程,它只保留最小的一些内核数据结构,以便父进程查询子进程的退出状态
--父进程查询子进程的退出状态可以用wait/waitpid函数
wait获取staus后检测处理
宏定义  描述
WIFEXITED(status) 如果进程子进程正常结束,返回一个非零值
    WEXITSTATUS(status) 如果WIFEXITED非零,返回子进程退出码
WIFSIGNALED(status) 子进程因为捕获信号而终止,返回非零值
    WTERMSIG(status) 如果WIFSIGNALED非零,返回信号代码
WIFSTOPPED(status) 如果进程被暂停,返回一个非零值
    WSTOPSIG(status) 如果WIFSTOPPED非零,返回信号代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int arg,char *args[])
{
    pid_t pid=fork();
    if(pid==-1)
    {
        printf("fork() failed ! error message:%s\n",strerror(errno));
        return -1;
    }
    if(pid>0)
    {
        int status=0;
        printf("父进程\n");
        wait(&status);
        if(WIFEXITED(status))//WIFEXITED宏的释义: wait if exit ed
        {
            printf("子进程返回信息码:%d\n",WEXITSTATUS(status));
        }else if(WIFSIGNALED(status))
        {
            printf("子进程信号中断返回信息码:%d\n",WTERMSIG(status));
        }else if(WIFSTOPPED(status))
        {
            printf("子进程暂停返回信息码:%d\n",WSTOPSIG(status));
        }else
        {
            printf("其他退出信息!\n");
        }
    }else if(pid==0)
    {
        printf("i am child !\n");
        abort();
        //exit(100);
    }
    printf("game is over!\n");
    return 0;
}
wait()函数成功返回等待子进程的pid,失败返回-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int arg, char *args[])
{
    pid_t pid = 0;
    int i = 0, ret = 0;
    for (i = 0; i < 10; i++)
    {
        pid = fork();
        if (pid == -1)
        {
            printf("fork() failed ! error message:%s\n", strerror(errno));
            return -1;
        }
        if (pid == 0)
        {
            printf("child haved run!\n");
            exit(0);
        }
    }
    while (1)
    {
        //wait()函数的返回值是子进程的pid
        ret = wait(NULL);
        printf("子进程pid=%d\n", ret);
        if (ret == -1)
        {
            //父进程wait()函数阻塞过程中,有可能被别的信号中断,需要做异常处理
            if (errno == EINTR)
            {
                continue;
            }
            break;
        }
    }
    printf("game is over!\n");
    return 0;
}

 

waitpid
函数功能:用来等待某个特定进程的结束
函数原型:
    pid_t waitpid(pid_t pid, int *status, int options);
参数:
    status如果不为空,会把状态信息写到它指向的位置
    options允许改变waitpid的行为,最有用的一个选项是WNOHANG,它的作用是防止waitpid把调用者的执行挂起
返回值:成功返回等待子进程的pid,失败返回-1

 

posted on 2016-11-17 14:15  寒魔影  阅读(15840)  评论(0编辑  收藏  举报

导航