Rookie essay (3)---Three process learning. Orphan process. Zombie process. Daemon

Source: Internet
Author: User
Tags exit in

A rookie turned out, the yard of the world to break into a daily progress more than lost.

  

                        Three kinds of process learning. Orphan process. Zombie process. Daemon

Transfer from 77040494

1. Orphan process

If the parent process exits first and the child process does not exit then the child process is tuogu to the Init process, where the parent process of the child process is the init process (process 1th). In fact, it is very well understood.

Parent process first child process exitsThe child process will be taken over by the grandfather process and run in the background, executing the internal codeint main () {pid_t pid = fork ();Switch (PID) {Case-1:perror ("Fork");BreakCase0:Child process Close (1);Create a file to hold the output textint fd = open ( "child", o_rdwr| O_creat, 0777); printf ( "I am a child process, my ID is%d\n", getpid ()); while (1) {printf (" find dad \ "); Fflush (stdout); Sleep (2);} break; default: //parent process printf (printf ( "I'm gone \ n"); while (1); break;} return 0;}         


Here we run the program, we can see in the other terminal there are 2 a.out running, we will terminate the parent process, the child process does not exit, but continue to run in the background, and output text to the children file.

2. Zombie Process

If we know about the Linux process state and the transition relationship, we should know that there is a state in the process so many states is a zombie state, that is, the process terminates into a zombie state (zombie), waiting to tell the parent process to terminate itself before it completely disappears. But if a process has been terminated, But its parent process has not yet acquired its state, so this process is called a zombie process. The zombie process also consumes some system resources and retains some profile information for the parent process to query the state of the child process to provide the information the parent process wants. Once the parent process gets the information it wants, the zombie process ends.

Child processes exit first than parent processint main () {int count =5;while (count--) {Signal (sigchld,sig_ign); pid_t pid = fork ();Switch (PID) {case-1:perror ( "fork"); break; case  0: //child process printf (" I am the child process, my ID is%d\n ", getpid ()); printf ( "I'm gone \ n"); exit (0); default: //parent process printf ( "I am the parent process, my id is%d\n", getpid ()); //while (1); break;} } while (1); return 0;}         

Signal (sigchld,sig_ign); After adding this line of code, the zombie process disappears.

by Ps-ef | grep a.out We know the process information and the process PID, we can see that the child process is in the defunct state. How do we want to avoid the zombie process at this point? Look at the program I commented on the sentence signal (sigchld,sig_ign), Plus there's no zombie progression. So why don't we just add some space to explain why we can avoid the zombie process?
This is the declaration of the Signal () function sighandler_t signal (int signum, sighandler_t handler), we can conclude that the first function of the signal function is the Linux supported signal, the second parameter is the operation of the signal Whether the system is default or ignored or captured. We're just going to know. Signal (sigchld,sig_ign) is the option to opt-out of the termination signal of a child program, which is a zombie process that is processed by the kernel itself and does not spawn a zombie process.

3. Guardian Process
Also, we need to know what a daemon is, daemons are running in the background, processes that are not associated with any terminal, and typically daemons are running at system startup, and they run as root users or other special users (Apache and postfix). and can handle some system-level tasks. The customary daemon name usually ends with a D (sshd), but these are not required.

Here are the steps to create a daemon:

· Call fork () to create a new process, which will be the future daemon.
· Call exit in the parent process to ensure that the child process is not the process leader
· Call Setsid () to create a new session area
· Changes the current directory to a directory (if the current directory is used as the daemon directory, the current directory cannot be uninstalled as the working directory of the Daemon)

Daemon processint Daemonize (int Nochdir,int noclose) {Create child process, close parent process pid_t pid = fork ();if (PID >0)Parent Process {Exit(0); }Elseif (PID <0) {Return-1; }2, set the file mask, mode & ~umask Umask(0);3. Set new session: Out of control of current session and terminalif (Setsid () <0) {Return-1; }When Nochdir is 0 o'clock, daemon will change the root directory of the process to rootIf(0 = = Nochdir) {Change the current working directoryif (ChDir ("/") <0) {Return-1; } }Standard input, off standard output, standard errorClose (Stdin_fileno);close (Stdout_fileno); close (Stderr_fileno); if  (0 = = noclose) {//redirect standard input, Turn off standard output, standard error open ( "Dev/null", o_rdonly); //0 open ( "Dev/null", O_RDWR); //1 open ( "Dev/null", O_RDWR); //2} return 0;} int Main () {daemonize  (0, 0); //Daemon (0,0); System comes with daemon while  (1); return 0;         

 

  

Rookie essay (3)---Three process learning. Orphan process. Zombie process. Daemon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.