一般判断进程用到三个函数

status的值即为exit(a)函数里的a值,代码中列举进程正常结束。

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

int main()
{
    pid_t pid;
    int status;  //查看子进程状态

    pid = fork();
    if(0 == pid)
    {
        printf("this is a child\n");
        exit(0);
    }

    wait(&status); //等待收尸
    
    //子进程正常退出
    if(WIFEXITED(status))
    {
        printf("this is parent, child normal, status = %d\n",WEXITSTATUS(status));
    }    

    return 0;
}

 

Logo

更多推荐