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;

}

 

 

运行结果截图:

 

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐