关闭

Unix环境编程学习笔记-----编程实例---- the normal exit2

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

NORMAL 2:

         THESOURCE CODE:

 

#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;

         inttmpstatus;

        

         Spid_t=fork();

        

         if(Spid_t<0)

         {

                   perror("forkerror");

                   _exit(-1);

                  

         }elseif(Spid_t==0)

                            {

                                     //子进程要做的事。

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

                                     _exit(2);//wheni change the doc what will happen ???? example : _exit(2)  ???

                                    

                            }

                            else

                            {

                                     //父进程要做的事。

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

                                    

                                     //我先使用waitpid

                                    

                                     rpid_t=waitpid(Spid_t,&tmpstatus,NULL);

                                    

                                    

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

                                    

                                     if(WIFEXITED(tmpstatus))

                                       {

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

                                                

                                       }

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

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

                                    

                                     _exit(2);

                                      

                                      

                            }

         return0;

}

 

此时,我们传给_exit的值是2

  “_exit(2);//wheni change the doc what will happen ???? example :”

我们看运行结果:

  

1
0

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