1. daemons, orphans, and zombie processes
(1) Daemon process
The ① daemon (daemon) is a long-lived process. They often start when the system boots and terminate when the system shuts down.
② All daemons run with the priority of the Superuser (user ID 0) .
③ Daemon no control terminal
The parent process of the ④ daemon is the init process (process number 1th).
(2) Orphan process : The parent process ends First , the child process becomes an orphan process and is adopted by process 1th (init process) .
"Programming Experiment" creates orphan process
Process_orphen.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>//orphan process. To view the status of a child process, start another terminal login during presentation//because the parent process ends first, the child process is adopted by the INIT process. intMainvoid) {pid_t pid=Fork (); if(PID <0) {perror ("Fork Error"); Exit (1); }Else if(PID >0){//Parent Processprintf"%d deaded\n", Getpid ()); Exit (0);//Parent Process Termination}Else{sleep (4);//let the parent process terminate first, the child process becomes the orphan process, will be adopted by the No. 1th processprintf"pid:%d, Ppid:%d\n", Getpid (), Getppid ());//ppid=1 } return 0;}
(3) Zombie process
① If the child process ends first , the parent process also exists . At this point the child process is not completely free of memory (task_struct in the kernel is not released), the process becomes a zombie process .
② when the parent process of a zombie process is finished, it is adopted by the INIT process and is eventually recycled.
③ ways to avoid zombie processes:
A. Let the parent process of the zombie process recycle, and the parent process queries the child process every once in a while to see if it ends and recycles. Call wait or waitpid to notify the kernel to release the zombie process.
B. Use signal SIGCHLD notification processing and call the wait function in the signal handler to notify the release of the zombie process.
C. Make the zombie process an orphan process, which is recycled by the init process.
The production of "programming experiments" zombie processes
Process_zombie.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>intMainvoid) {pid_t pid=Fork (); if(PID) <0) {perror ("Fork Error"); Exit (1); }Else if(PID = =0){//Child Processprintf"pid:%d, Ppid:%d\n", Getpid (), Getppid ()); Exit (0); } //wait for the child process to end and notify release task_struct//Wait (0); //Plus this will notify the kernel to release the task_struct of the child process, not//Create a zombie process that can be compared by canceling comments. //trying to spawn a zombie process while(1){//The parent process continues to loop without exiting. At this point the child process is not fully//release and become a zombie processSleep1); } return 0;}
7th Process Relationship (1) _ Daemon, orphan, and zombie process