(1)fork()创建子进程

 

 
  
  1. #include <unistd.h> 
  2. #include <sys/types.h> 
  3. #include <sys/wait.h> 
  4. #include <stdio.h> 
  5. #include <stdlib.h> 
  6. #include <errno.h> 
  7. #include <math.h> 
  8.  
  9. /* 进程创建 */ 
  10. void main(void
  11.     pid_t child; 
  12.     int status; 
  13.  
  14.     printf("This will demostrate how to get child status\n"); 
  15.  
  16.     /* 创建子进程 */ 
  17.     if((child=fork())==-1) 
  18.     { 
  19.         printf("Fork Error : %s\n", strerror(errno)); 
  20.         exit(1); 
  21.     } 
  22.     else if(child==0) // 子进程 
  23.     { 
  24.         int i; 
  25.         printf("I am the child: %s\n", getpid()); 
  26.         for(i=0;i<1000000;i++) sin(i); 
  27.         i=5; 
  28.         printf("I exit with %d\n", i); 
  29.         exit(i); 
  30.     } 
  31.  
  32.     while(((child=wait(&status))==-1)&(errno==EINTR));  //子进程未结束 
  33.  
  34.     if(child==-1) 
  35.         printf("Wait Error: %s\n", strerror(errno)); 
  36.     else if(!status)             // 子进程退出值为0 
  37.         printf("Child %ld terminated normally return status is zero\n", child); 
  38.     else if(WIFEXITED(status))   // 子进程退出值0 
  39.         printf("Child %ld terminated normally return status is %d\n", child, WEXITSTATUS(status)); 
  40.     else if(WIFSIGNALED(status)) // 子进程未获信号而退出 
  41.         printf("Chlid %ld terminated due to signal %d not caught\n", child, WTERMSIG(status)); 
  42. }    

(2)进程等待waitpid()

 

 
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <sys/types.h> 
  4. #include <unistd.h> 
  5. #include <sys/wait.h> 
  6.  
  7. void die(const char *msg) 
  8.         perror(msg); 
  9.         exit(1); 
  10.  
  11. void child2_do() 
  12.         printf("In child2: execute 'date'\n"); 
  13.  
  14.         sleep(5); 
  15.         if (execlp("date""date", NULL) < 0) { 
  16.                 perror("child2 execlp"); 
  17.         } 
  18.  
  19. void child1_do(pid_t child2, char *argv) 
  20.         pid_t pw; 
  21.  
  22.         do { 
  23.                 if (*argv == '1') { 
  24.                         pw = waitpid(child2, NULL, 0); 
  25.                 } 
  26.                 else { 
  27.                         pw = waitpid(child2, NULL, WNOHANG); 
  28.                 } 
  29.                 if (pw == 0) { 
  30.                         printf("In child1 process:\nThe child2 process has not exited!\n"); 
  31.                         sleep(1); 
  32.                 } 
  33.         }while (pw == 0); 
  34.  
  35.         if (pw == child2) { 
  36.                 printf("Get child2 %d.\n", pw); 
  37.                 sleep(5); 
  38.                 if (execlp("pwd""pwd", NULL) < 0) { 
  39.                         perror("child1 execlp"); 
  40.                 } 
  41.         } 
  42.         else { 
  43.                 printf("error occured!\n"); 
  44.         } 
  45.  
  46. void father_do(pid_t child1, char *argv) 
  47.         pid_t pw; 
  48.  
  49.         do { 
  50.                 if (*argv == '1') { 
  51.                         pw = waitpid(child1, NULL, 0); 
  52.                 } 
  53.                 else { 
  54.                         pw = waitpid(child1, NULL, WNOHANG); 
  55.                 } 
  56.  
  57.                 if (pw == 0) { 
  58.                         printf("In father process:\nThe child1 process has not exited.\n"); 
  59.                         sleep(1); 
  60.                 } 
  61.         }while (pw == 0); 
  62.  
  63.         if (pw == child1) { 
  64.                 printf("Get child1 %d.\n", pw); 
  65.                 if (execlp("ls""ls""-l", NULL) < 0) { 
  66.                         perror("father execlp"); 
  67.                 } 
  68.         } 
  69.         else { 
  70.                 printf("error occured!\n"); 
  71.         } 
  72.  
  73. int main(int argc, char *argv[]) 
  74.         pid_t child1, child2; 
  75.  
  76.         if (argc < 3) { 
  77.                 printf("Usage: waitpid [0 1] [0 1]\n"); 
  78.                 exit(1); 
  79.         } 
  80.  
  81.         child1 = fork(); 
  82.  
  83.         if (child1 < 0) { 
  84.                 die("child1 fork"); 
  85.         } 
  86.         else if (child1 == 0) { 
  87.                 child2 = fork(); 
  88.  
  89.                 if (child2 < 0) { 
  90.                         die("child2 fork"); 
  91.                 } 
  92.                 else if (child2 == 0) { 
  93.                         child2_do(); 
  94.                 } 
  95.                 else { 
  96.                         child1_do(child2, argv[1]); 
  97.                 } 
  98.         } 
  99.         else { 
  100.                 father_do(child1, argv[2]); 
  101.         } 
  102.  
  103.         return 0; 

 

(3)结束进程函数 kill()

 

 
  
  1. #include <sys/wait.h> 
  2. #include <sys/types.h> 
  3. #include <stdio.h> 
  4. #include <stdlib.h> 
  5. #include <signal.h> 
  6.  
  7. int main( void ) 
  8.     pid_t childpid; 
  9.     int status; 
  10.     int retval; 
  11.      
  12.     childpid = fork(); 
  13.     if ( -1 == childpid ) 
  14.     { 
  15.         perror( "fork()" ); 
  16.         exit( EXIT_FAILURE ); 
  17.     } 
  18.     else if ( 0 == childpid ) 
  19.     { 
  20.         puts( "In child process" ); 
  21.         sleep( 100 );//让子进程睡眠,看看父进程的行为 
  22.         exit(EXIT_SUCCESS); 
  23.     } 
  24.     else  
  25.     { 
  26.         if ( 0 == (waitpid( childpid, &status, WNOHANG ))) 
  27.         { 
  28.             retval = kill( childpid,SIGKILL ); 
  29.              
  30.             if ( retval ) 
  31.             { 
  32.                 puts( "kill failed." ); 
  33.                 perror( "kill" ); 
  34.                 waitpid( childpid, &status, 0 ); 
  35.             } 
  36.             else 
  37.             { 
  38.                 printf( "%d killed\n", childpid ); 
  39.             } 
  40.              
  41.         } 
  42.     } 
  43.      
  44.     exit(EXIT_SUCCESS); 
  45. }