The parent process of the zombie process to recycle, the parent process every time to query whether the child process is finished and recycled, call wait () or Waitpid (), notify the kernel to release the zombie process
/* Let the parent process of the zombie process recycle, and the parent process queries the child process to see if it ends and recycles at intervals,
calls wait () or waitpid (), notifies the kernel to release the zombie process
*/
#include <stdio.h >
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
int main (void)
{
pid_t pid=fork ();
int n=0;
char *s=null;
if (pid<0) {
perror ("fork creat");
} else if (pid>0) {//parent process
n=1;
S= "This is parent process";
} else{//pid=0//Sub-process
n=10;
S= "This was child process";
}
for (int i=0;i<n;++i) {
printf ("%s\n", s);
if (pid>0) {
printf ("I am waiting for you, my child\n");
Wait (NULL);
}
Sleep (1);
}
printf ("%s bye\n", s);
return 0;
}
Run results
Xfliu@ubuntu: Avoid the zombie process $./BIN/1 This
is the parent process I am waiting for your, my child this is the child process
t He is a child process, this is a child process, which is a child process, this is a child process, this is a child
proce The SS this is the child process, which is the child process, which is the child process.
Child process bye The is
parent process bye
Two
Uses signal SIGCHLD to notify processing and calls the wait function in the signal handler
/*
use signal sigchld to notify processing and call the wait function in the signal handler
*/
#include <stdio.h>
#include <unistd.h>
# include<sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include < signal.h>
void signal_wait (int isig)
{
wait (NULL);
printf ("Child is cleaned\n");
}
int main (void)
{
pid_t pid=fork ();
char *s=null;
if (signal (sigchld,signal_wait) ==sig_err) {
perror ("Install signal_wait");
}
if (pid<0) {
perror ("fork creat");
} else if (pid>0) {//Parent process
sleep (1);
S= "This is parent process";
} else{//pid=0//subprocess s= "This is a child process
";
}
printf ("%s bye\n", s);
return 0;
}
Results
This is the child process bye child is cleaned the is
parent
processes bye Xfliu@ubuntu: Avoid zombie progress $
Third, let the zombie process into the orphan process, from the init recovery, is to let the father died first
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h >
#include <sys/wait.h>
int main (void)
{
pid_t pid=fork ();
int n=0;
char *s=null;
if (pid<0) {
perror ("fork creat");
} else if (pid>0) {//parent process
n=1;
S= "This is parent process";
} else{//pid=0//Sub-process
n=10;
S= "This was child process";
}
for (int i=0;i<n;++i) {
printf ("%s\n", s);
/*
if (pid>0) {
printf ("I am waiting for you, my child\n");
Wait (NULL);
}
Sleep (1);
*
/} printf ("%s bye\n", s);
return 0;
}
Operation Result:
Xfliu@ubuntu: Avoid zombie processes $./BIN/2 This is the parent process, which is the
parent process Bye This
is the child process, this
IS-child process, which is a child process that is a child process that is a child process, which is a child
process< C8/>this is a child process, this is a child process, which is a child process, this is a child process, this is
Chi LD Process bye
About wait and waitpid after the study