Today, someone asked about the zombie process and the orphan process, which has been encountered before, but not much attention, here mark
Zombie Process: The process fork out the subprocess, but the parent process does not call wait or waitpid to get the status information of the child process, the process description of the child process Fuzhengan saved in the system
Find Zombie Processes
Ps-a-ostat,ppid,pid,cmd | Grep-e ' ^[zz] '
Orphan process: The parent process exits, the child process continues, the orphan process will be adopted by Init (1) and the information collected by Init is completed.
Zombie Process Damage: When the process exits, the kernel releases all resources, including open files, memory, but still retains some information
, knowing that the parent process is released by Wait/waitpid, if the parent process does not fish wait waitpid, the reserved information will not be released, its process number will be occupied, the system process number is limited, if a large number of zombie processes, the system will not be able to generate new processes.
Orphan process is a process without a parent process, INIT will be responsible for the aftermath of the work, no harm
Child process (except Init) does not disappear immediately after exit, but instead becomes zombie, waiting for the parent process to handle
Eg: Zombie process
1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4#include <errno.h>5 6 7 intMain ()8 {9pid_t PID =fork ();Ten if(PID <0 ) One { Aprintf"fork_error\n"); -Exit1); - } the Else if(PID = =0 ) - { -printf"Child \ n"); -Exit0); + } - + Aprintf"father....\n"); atSleep2); -System"Ps-o Pid,ppid,state,tty,command"); -printf"Father exit ...."); - return 0; -}
The result of its operation
Orphan process
#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<errno.h>intMain () {pid_t PID=Fork (); if(PID <0) {printf ("fork_error\n"); Exit (1); } Else if(PID = =0) {printf ("Child \ n"); printf ("PID:%d\tppid:%d\n", Getpid (), Getppid ()); printf ("sleep ..."); Sleep (4); printf ("PID:%d\tppid:%d\n", Getpid (), Getppid ()); printf ("Child Exit ..."); Exit (0); } printf ("father....\n"); printf ("Father exit ...."); return 0;}
Operation Result: