review..wait

来源:互联网 发布:神州律师网网络培训 编辑:程序博客网 时间:2024/05/17 03:24
#include<stdio.h>#include<stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include<signal.h>int main(){        pid_t pid = 0;        pid = fork();        int status;        if(pid < 0)        {                perror("fork error....\n");                return -1;        }        if(pid == 0)        {                int i = 0;                pid_t pid = 0;                for(i = 0; i < 5; i++)                {                        pid = fork();                        if(pid == 0)                        {                                printf("son pid is %d\n",getpid());                                _exit(5);                        }                }                //sleep(5);                printf("son pid is,,,,,,, %d\n",getpid());                //_exit(5);        }else if(pid > 0)        {                int ret = 0;                sleep(1);                //kill(pid,SIGQUIT);                printf("father begin to wait....\n");                wait(&status);// there are six son,but wait can deal one son                    if((ret = WIFEXITED(status)) != 1)//is normal exit,such as exit,return and _exit                {                     if((ret = WIFSIGNALED(status))!=0)//is terminal by a signal??                     printf("terminal by signal\n");                }else{                        ret = WEXITSTATUS(status);//return value of exit                        printf("exit is %d\n",ret);                }        }        printf("fathre is break....\n");        while(1);}