Linux进程等待

Posted Huang_ZhenSheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux进程等待相关的知识,希望对你有一定的参考价值。

目录

进程等待的必要性:

进程等待的方法:

wait方法(测试):

waitpid方法(测试): 

完整的等待过程:

进程的阻塞/非阻塞等待: 

 非阻塞等待的相关代码:

进程等待的必要性:

1:回收僵尸,解决内存泄露

2:需要获取子进程的运行状态

3:尽量父进程要晚于子进程退出,可以规范化进行资源回收

进程等待的方法:

wait方法(测试):

 #include<stdio.h>
    2 #include<sys/types.h>
    3 #include<sys/wait.h>
    4 #include<unistd.h>
    5 int main()
    6 
    7   pid_t id = fork();
    8   if(id < 0)
    9   
   10     perror("fork");
   11     return 1;
   12   
   13   else if(id ==0)
   14   
   15     int count = 5;
   16     while(count)
   17     
   18       printf("child is running:%d,ppid:%d,pid:%d\\n",count--,getppid(),getpid());
   19       sleep(1);
   20     
   21     printf("child quit...\\n");
E> 22     exit(0);
   23   
   24   else
   25   
   26     printf("father is waiting...\\n");                                                                                                                     
   27     pid_t ret = wait(NULL);
   28     printf("father is wait done,ret:%d\\n",ret);
   29   
   30   return 0;
   31 

waitpid方法(测试): 

int main()
    6 
    7   pid_t id = fork();
    8   if(id == 0)
    9   
   10     int count = 5;
   11     while(count)
   12     
   13       printf("child is running:%d,ppid:%d,pid:%d\\n",count--,getppid(),getpid());
   14       sleep(1);
   15     
   16     printf("child quit ...\\n");
E> 17     exit(0);
   18   
   19   sleep(8);
   20   //father
   21   pid_t ret = waitpid(id,NULL,0);
   22   printf("father wait done,ret:%d\\n",ret);
   23   sleep(3);                                                                                                                                               
   24 

完整的等待过程:

int main()
    6 
    7   pid_t id = fork();
    8   if(id == 0)
    9   
   10     int count = 5;
   11     while(count)
   12     
   13       printf("child is running:%d,ppid:%d,pid:%d\\n",count--,getppid(),getpid());
   14       sleep(1);
   15     
   16     printf("child quit ...\\n");
E> 17     exit(10);
   18   
   19   int status = 0;
   20   pid_t ret = waitpid(id,&status,0);
   21   if(ret > 0)
   22   
   23     printf("wait succes!\\n");
   24     if((status & 0x7F) ==0)
   25     
   26       printf("process quit normal!\\n");
   27       printf("exit code : %d\\n",(status >> 8)&0xFF);
   28     
   29     else
   30       printf("process quit error!\\n");
   31       printf("sig:%d\\n",status&0x7F);                                                                                                                     
   32     
   33   
   34 

错误测试:

 系统提供了一堆的宏,可以用来判断退出码,退出状态

 if(ret > 0)
   22   
   23     printf("wait success!\\n");
   24     if(WIFEXITED(status))
   25     
   26       printf("normal quit!\\n");
   27       printf("quit code:%d\\n",WEXITSTATUS(status));                                                                                                       
   28     
   29     else
   30     
   31       printf("process quit error!\\n");
   32     

进程的阻塞/非阻塞等待: 

目前我们所调用的函数,都是阻塞函数

如何理解阻塞等待:父进程在等(将当前进程放入等待队列,并将进程状态设置为非R状态),子进程在跑

 

 非阻塞等待的相关代码:

int main()
    6 
    7   pid_t id = fork();
    8   if(id == 0)
    9   
   10     int count = 5;
   11     while(count)
   12     
   13       printf("child is running:%d,ppid:%d,pid:%d\\n",count--,getppid(),getpid());
   14       sleep(1);
   15     
   16     printf("child quit ...\\n");
E> 17     exit(10);
   18   
   19   int status = 0;
   20   while(1)
   21   
   22     pid_t ret = waitpid(id,&status,WNOHANG);
   23     if(ret == 0)
   24     
   25       printf("wait next !\\n");
   26       printf("father do other thing!\\n");
   27     
   28     else if(ret > 0)
   29     
   30       printf("wait succes,ret:%d,code:%d\\n",ret,WEXITSTATUS(status));
   31       break;
   32     
   33     else
   34       printf("wait failed!\\n");
   35       break;
   36     
   37   
   38 

以上是关于Linux进程等待的主要内容,如果未能解决你的问题,请参考以下文章

Linux进程等待

Linux进程等待

Linux进程等待

Linux进程控制:创建 终止 等待

Linux进程控制

Linux进程控制