关闭

Unix环境编程学习笔记-----编程实例---- the fist exit

120人阅读 评论(0) 收藏 举报
分类:

The first _exit 函数:

 

 

源代码:

 

                  

#include<unistd.h>

#include<errno.h>

#include<sys/types.h>

#include<sys/wait.h>

#include<stdio.h>

#include<stdlib.h>

 

 

 

 

 

int main()

{

         intPpid_t,Spid_t,rpid_t;

         int* tmpstatus;

        

         Spid_t=fork();

        

         if(Spid_t<0)

         {

                   perror("forkerror");

                   _exit(-1);

                  

         }elseif(Spid_t==0)

                            {

                                     //子进程要做的事。

                                     printf("subprocess is going to over!\n");

                                     _exit(0);

                                    

                            }

                            else

                            {

                                     //父进程要做的事。

                                     //父进程获取子进程的结束状态,并打印出来

                                    

                                     //我先使用waitpid

                                    

                                     rpid_t=waitpid(Spid_t,tmpstatus,NULL);

                                    

                                    

                                     //如果正常推出则打印出进程终止装填的编号  WIFEXITED(status)

                                    

                                     if(WIFEXITED(tmpstatus))

                                       {

                                                printf("the status is :%d\n",WEXITSTATUS(tmpstatus));

                                                

                                       }

                                       else if(WSTOPSIG(tmpstatus))  //判断子进程是不是由信号杀死,如果是打印该信号值。

                                                        printf("thesignal value is :%d \n",WTERMSIG(tmpstatus));

                                    

                                     _exit(2);

                                      

                                      

                            }

         return0;

}

 

 

运行结果截图:

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:238246次
    • 积分:7595
    • 等级:
    • 排名:第2810名
    • 原创:490篇
    • 转载:337篇
    • 译文:2篇
    • 评论:1条
    最新评论