Analysis of three special processes: Orphan process, zombie process and daemon.

Source: Internet
Author: User
Tags exit in

In fact, sometimes think of the Linux kernel design also contains a lot of philosophy of life, there are so many special processes in Linux, we start to see their names may also feel very surprised, but after the understanding of the principle, we think carefully, such a name is not unreasonable! Here I will give you a separate introduction to these three kinds of special process!

  1. The 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, which is the parent process of the child process, which is the init process (process 1th). In fact, it is very well understood.

  

#include <sys/types.h>#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<signal.h>#include<errno.h>#include<signal.h>intMainvoid) {pid_t pid;        Signal (sigchld,sig_ign); printf ("before fork pid:%d\n", Getpid ()); intABC =Ten; PID=Fork (); if(PID = =-1) {perror ("Tile"); return-1; }        if(PID >0)//parent process exits first{ABC++; printf ("parent:pid:%d \ n", Getpid ()); printf ("abc:%d \ n", ABC); Sleep (5); }        Else if(PID = =0){//value process exits and is entrusted to the INIT processAbc++; printf ("child:%d,parent:%d\n", Getpid (), Getppid ()); printf ("abc:%d", ABC); Sleep ( -); } printf ("Fork after...\n");}

Before Fork pid:27709
parent:pid:27709
Abc:11
child:27710,parent:27709
Fork after ...

DISDA 27710 1 0 10:47 pts/1 00:00:00./review
DISDA 27713 25948 10:47 pts/3 00:00:00 ps-ef

We execute the program because the child process enters sleep (100), and the parent process exits first. With the PS-EF command we can know that at this point the parent process of process No. 27710 programmed the 1th process. That's what we call the init process.

  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.

  

intMainvoid) {pid_t pid; //signal (sigchld,sig_ign);printf"before fork pid:%d\n", Getpid ()); intABC =Ten; PID=Fork (); if(PID = =-1) {perror ("Tile"); return-1; }        if(PID >0) {ABC++; printf ("parent:pid:%d \ n", Getpid ()); printf ("abc:%d \ n", ABC); Sleep ( -); }        Else if(PID = =0) {ABC++; printf ("child:%d,parent:%d\n", Getpid (), Getppid ()); printf ("abc:%d", ABC); Exit (0); } printf ("Fork after...\n");

DISDA 27881 23047 0 11:12 pts/1 00:00:00./fork01
DISDA 27882 27881 0 11:12 pts/1 00:00:00 [fork01] <defunct>

Also through ps-ef we can know the process information and process PID, you 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. Daemon 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)
    • REDIRECT standard input, callout output, standard error to/dev/null
#include <sys/types.h>#incldue<sys/stat.h>#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<signal.h>#include<errno.h>#include<signal.h>#include<fcntl.h>#incldue<unistd.h>#include<linux/fs.h>intMainvoid) {pid_t pid; inti; PID= Fork ();//Create a new process that will be a daemon in the future    if(PID = =-1)    {        return-1; }    Else if(PID! =0){//the parent process calls exit to ensure that the child process is not the process leaderexit (exit_success); }        if(Setsid () = =-1)//create a new session area    {        return-1; }        if(ChDir ("/") == -1)//change the current directory to the root directory    {        return-1; }         for(i =0; I < nr_open;i++) {close (i); } Open ("/dev/null", O_RDWR); REDIRECT DUP (0); DUP (0); return 0;}

DISDA 26217 1 0 06:59? 00:00:00./dm01_demon, there is a daemon!

  

Analysis of three special processes: Orphan process, zombie process and 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.