实例内容:一个父进程创建两个子进程,其中一个子进程运行“ls -l”的指令,另一个子进程在等待5s之后异常退 出,父进程先用阻塞方式等待第一个子进程的结束,然后用非阻塞方式等待另一个子进程的退出, 待收集到第二个子进程结束的信息,父进程就返回。
先了解下waitpid函数的应用:
表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t waitpid(pid_t pid,int * status,int options); 函数说明 waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status可以设成NULL。参数pid为欲等待的子进程识别码,其他数值意义如下: pid<-1 等待进程组识别码为pid绝对值的任何子进程。 pid=-1 等待任何子进程,相当于wait()。 pid=0 等待进程组识别码与目前进程相同的任何子进程。 pid>0 等待任何子进程识别码为pid的子进程。 参数option可以为0 或下面的OR 组合 WNOHANG 如果没有任何已经结束的子进程则马上返回,不予以等待。 WUNTRACED 如果子进程进入暂停执行情况则马上返回,但结束状态不予以理会。 子进程的结束状态返回后存于status,底下有几个宏可判别结束情况 WIFEXITED(status)如果子进程正常结束则为非0值。 WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。 WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真 WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。 WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。 WSTOPSIG(status)取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。 返回值 如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。失败原因存于errno中。
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char **argv) { pid_t child1,child2; child1=fork(); if(child1==-1) printf("creat fork 1 failed!\n"); else if(child1==0) { printf("In child1 : ready to excute ls -l\n"); if(execlp("ls","ls","-l",NULL)<0) printf("execlp failed!\n"); } else { pid_t child; child2=fork(); if(child2==-1) printf("creat fork 2 failed!\n"); else if(child2==0) { printf("In child2: sleep 5 s and exit"); sleep(5); exit(0); } printf("In father process:\n"); child=waitpid(child1,NULL,0); if(child==child1) { printf("wait child1\n"); } else printf("error occured!\n"); child=0; while(child==0) { child=waitpid(child2,NULL,WNOHANG); if(child==0) { printf("the child 2 has not exited!\n"); sleep(1); } } if(child==child2) printf("wait child2\n"); } return 0; } 执行结果如下:
ivan@ivan-virtual-machine:~/123$ gcc multi_fork.c -o multi_fork ivan@ivan-virtual-machine:~/123$ ./multi_fork In father process: In child1 : ready to excute ls -l 总用量 240 -rwxrwxr-x 1 ivan ivan 7384 7月 11 11:14 fifo_read -rwxrwxr-x 1 ivan ivan 7471 7月 11 16:22 fifo_read1 -rwxrw-rw- 1 ivan ivan 1006 7月 11 16:22 fifo_read1.c -rw-rw-r-- 1 ivan ivan 796 7月 11 11:14 fifo_read.c -rwxrwxr-x 1 ivan ivan 7482 7月 11 12:04 fifo_write -rwxrw-rw- 1 ivan ivan 659 7月 11 12:04 fifo_write.c -rw-rw-r-- 1 ivan ivan 14925 6月 26 11:20 file1 -rw-rw-r-- 1 ivan ivan 517 6月 25 17:49 file1.c -rwxrwxr-x 1 ivan ivan 7420 6月 25 21:03 file_copy -rwxrwxr-x 1 ivan ivan 7425 6月 26 11:20 file_copy1 -rw-rw-r-- 1 ivan ivan 1107 6月 26 11:19 file_copy1.c -rw-rw-r-- 1 ivan ivan 1058 6月 25 21:03 file_copy.c -rw-rw-r-- 1 ivan ivan 550 6月 25 14:58 file_create.c -rwxrwxr-x 1 ivan ivan 7385 6月 25 15:47 file_open -rw-rw-r-- 1 ivan ivan 530 6月 25 20:38 file_open.c -rwxrwxr-x 1 ivan ivan 7159 6月 25 14:32 hello -rwxr-xr-x 1 ivan ivan 0 6月 25 17:49 hello1 -rw-rw-r-- 1 ivan ivan 14925 6月 26 11:19 hello.c -rwxrwxr-x 1 ivan ivan 7388 7月 11 17:53 multi_fork -rw-rw-r-- 1 ivan ivan 1244 7月 11 17:52 multi_fork.c -rwxrwxr-x 1 ivan ivan 7353 7月 11 16:33 mysignal -rwxrw-rw- 1 ivan ivan 475 7月 11 16:33 mysignal.c -rwxrwxr-x 1 ivan ivan 7305 6月 27 18:51 pid -rwxrwxr-x 1 ivan ivan 7271 6月 27 17:59 pid1 -rw-rw-r-- 1 ivan ivan 372 6月 27 17:58 pid1.c -rwxrwxr-x 1 ivan ivan 7344 6月 27 19:21 pid2 -rw-rw-r-- 1 ivan ivan 984 6月 27 19:21 pid2.c -rwxrwxr-x 1 ivan ivan 7380 6月 27 19:33 pid3 -rw-rw-r-- 1 ivan ivan 646 6月 27 19:33 pid3.c -rwxrwxr-x 1 ivan ivan 7159 6月 27 19:57 pid4 -rw-rw-r-- 1 ivan ivan 145 6月 27 19:56 pid4.c -rw-rw-r-- 1 ivan ivan 383 6月 27 18:51 pid.c -rwxrwxr-x 1 ivan ivan 7473 7月 11 16:50 sigaction -rwxrw-rw- 1 ivan ivan 997 7月 11 09:59 sigaction.c -rwxrwxr-x 1 ivan ivan 7311 6月 25 21:52 time -rwxrwxr-x 1 ivan ivan 7388 6月 25 21:54 time1 -rw-rw-r-- 1 ivan ivan 408 6月 25 21:54 time1.c -rw-rw-r-- 1 ivan ivan 362 6月 25 21:50 time.c wait child1 the child 2 has not exited! the child 2 has not exited! the child 2 has not exited! the child 2 has not exited! the child 2 has not exited! In child2: sleep 5 s and exitwait child2