#include <unistd.h>#include<stdio.h>#include<stdlib.h>#include<signal.h>intMainvoid) { //signal (SIGCHLD, sig_ign); intI=0; printf ("i son/pa ppid pid fpid\n"); //Ppid refers to the parent process PID of the current process//PID refers to the PID of the current process,//fpid refers to the value that the fork returns to the current process while(1) {Sleep (1); //The reason to join this time is because, easy to see the changes in the program, or can not see the changes, even the mouse can not understand! pid_t fpid=Fork (); if(fpid==0) {printf ("%d child%4d%4d%4d\n", I,getppid (), Getpid (), fpid); //return 0;//These two effects are the same! Exit0); } } return 0; } [[email protected] desktop]# gcc b.c[[email protected] desktop]#./A. outI son/PA ppid pid fpid0Child16980 16981 00Child16980 16982 00Child16980 16983 00Child16980 16985 00Child16980 16988 00Child16980 16990 00Child16980 16991 00Child16980 16993 00Child16980 16995 00Child16980 16996 0^C[[email protected] desktop]# [[email protected] desktop]# PS aux| Grep-w'Z' //have spawned a lot of zombie processes Root16981 0.0 0.0 0 0pts/2z+ -: - 0:xx[A. out] <defunct>Root16982 0.0 0.0 0 0pts/2z+ -: - 0:xx[A. out] <defunct>Root16983 0.0 0.0 0 0pts/2z+ -: - 0:xx[A. out] <defunct>Root16985 0.0 0.0 0 0pts/2z+ -: - 0:xx[A. out] <defunct>Root16987 0.0 0.0 4340 804pts/3S+ -: - 0:xxgrep-w z[[email protected] desktop]# when putting signal (SIGCHLD, sig_ign); This line of code adds the following output:[[email protected] desktop]# gcc B.c[[email protected] desktop]#./A. outI son/PA ppid pid fpid0Child17135 17136 00Child17135 17137 00Child17135 17139 00Child17135 17140 00Child17135 17141 00Child17135 17142 00Child17135 17145 00Child17135 17147 00Child17135 17150 00Child17135 17151 00Child17135 17152 0^C[[email protected] desktop]# //No zombie process generated [[email protected] desktop]# PS aux| Grep-w'Z'Root17144 0.0 0.0 4336 796pts/3S+ -: the 0:xxgrep-w z[[email protected] desktop]# PS aux| Grep-w'Z'Root17149 0.0 0.0 4336 792pts/3S+ -: the 0:xxgrep-w z[[email protected] desktop]#
This can program primarily test how advanced concurrent server programs write to avoid zombie processes?