Zombie Process: The parent process does not wait for the child process, and the wait () child process becomes a zombie process.
int main (int arg, char *args[])
{
pid_t pid = fork ();//call fork to produce a sub-
int status;
if (PID = =-1)
{
printf ("fork failed\n");
return 0;
}
if (PID = = 0)//child process call Execve, execute ls-l command
{
Exit (0);//the child process exits and becomes a zombie process.
}
Else
{
printf ("Father exit");
Wait (&status);//block the call until the child process exits, and wait does not return
Sleep (10);//Not waiting for child process state.
Return 0;//Parent Process exit
}
}
Orphan process: The parent process exits a child process that has not exited and will be taken over by the INIT process into an orphan process.
int main (int arg, char *args[])
{
Close (Stdout_fileno);//Turn off standard output
Open ("/dev/pts/2", o_wronly);//Turn on "/DEV/PTS/2" as standard output
pid_t pid = fork ();//call fork to produce a sub-
int status;
if (PID = =-1)
{
printf ("fork failed\n");
return 0;
}
if (PID = = 0)//child process call Execve, execute ls-l command
{
Char *args[] = {"/bin/ls", "-L", NULL};
Execve ("/bin/ls", args, NULL);
}
Else
{
Return 0;//Parent Process exit
}
}
Code that the parent process waits for the child process to exit
int main (int arg, char *args[])
{
pid_t pid = fork ();//There are two processes after the fork is called
int status;
if (PID = = 0)
{
printf ("Child begin\n");
Sleep (5);
printf ("Child end\n");
return-1;
}
if (PID > 0)
{
printf ("Parent begin\n");
Wait (&status);//block the call until the child process exits, and wait does not return
printf ("Child return =%d\n", Wexitstatus (status));
printf ("Parent end\n");
}
return 0;
}
Zombie Process
When the parent process does not call wait, the child process exits, and the child process becomes a zombie process.
5 ways to exit a process:
Return is the same as exit in main function main, only in the sub-function, there is a difference, the child function return just exit the child function, exit is the exit process.
Exit is the same as return in the main function, except that it is not the same in a child function.
_exit
Abort exception exits. Generate a Core.xxx file
Signal
Fork ();