代码示例_进程

进程


 1. fork ,  waitpid

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <sys/wait.h>
 5 
 6 int main(void)
 7 {
 8     pid_t pid = fork(); // 创建一个进程
 9 
10     if(pid<0){
11         perror("fork");
12         exit(1);
13     }
14 
15     else if(pid==0){
16         SLEEP ( . 3 );
 . 17          the printf ( " Child proc \ n- " );
 18 is      }
 . 19  
20 is      the else  IF (PID> 0 ) {
 21 is          SLEEP ( . 1 );
 22 is          the printf ( " parent proc \ n- " );
 23 is          waitpid (PID, NULL, 0 );   // block waiting for the recovery process does not recover orphaned child processes,
 24-          // waitpid (pid, NULL, WNOHANG); // non-blocking sub-process recycling 
25  
26          printf ( " sub-process recycling success \ the n-! ")
27  
28      }
 29  
30      return  0 ;
31 }

 

test:


 

 

 


2. 

 

Guess you like

Origin www.cnblogs.com/panda-w/p/11059799.html