Orphan process, zombie process, and daemon

Source: Internet
Author: User
Tags readable system log

In the Wikipedia explanation:

In the operating system realm, the orphan process refers to a class of processes that continue to run after the parent process finishes or is terminated.

In a Unix-like system, a zombie process is the completion of execution (via the exit system call, or a fatal error at run time or a termination signal), but there is still one table entry in the operating system's process table ( process control block PCB), The process of "terminating state".

In a multi-worker computer system, the daemon (English: Daemon, English pronunciation:/?di?m?n/or English pronunciation:/?de?m?n/) is a PC program that executes in the background. Such programs are initialized in the form of a process . The name of the daemon program usually ends with the letter "D": for example, syslogd refers to the daemon that manages the system log.

Personal Understanding:

1, in general, the child process is created by the parent process, and the child process and the parent process exit is not sequential, neither knows who exits first. Normally the parent process ends up calling wait or the Waitpid function waits for the child process to complete before exiting, and once the parent process does not wait for a direct exit, the remaining child processes are received by the Init (pid=1) process and orphaned. (The process tree will have a parent process in addition to init).

2. If the child process exits first, the parent process is not finished and the wait is not called or the Waitpid function gets the state information of the child process, the remaining state information (task_struct structure and small amount of resource information) of the child process becomes a zombie process.

3, daemon (daemon) refers to running in the background, there is no control terminal connected with the process. It is independent of the control terminal and usually performs some sort of task periodically. The daemon is detached from the terminal to prevent the information in the process from being displayed on any terminal and the process will not be interrupted by terminal information generated by any terminal.

Harm:

The orphan process will be destroyed after the INIT process, and there is no harm, and the zombie process will always occupy the process number, the operating system is limited the number of processes will be affected.

Solve:

The general zombie process is generated because of the parent process, can be resolved by the kill parent process, when the zombie process becomes an orphan process, received by the INIT process

The daemon is written:

In different UNIX environments, the specific programming details of the daemon are not consistent. Fortunately, the daemon's programming principles are all the same, and the difference lies in the specifics of the implementation details, which is to satisfy the nature of the daemon. The following are the programming rules:

1. Running in the background

To avoid suspending the control terminal, the daemon is placed in the background by calling Fork in the process to terminate the parent process and let Daemon execute in the child process in the background. Specifically, call fork and then exit the parent process. Doing so achieves the following points:
First, if the sprite process is started by a simple S-H-l command, then the parent process is terminated so that the S-H L-L thinks the command has been completed.
Second, the child process inherits the process group I d of the parent process, but has a new process I d, which guarantees that the child process is not the first process of a process group. This is necessary for the following to be done for the S e T s i d call.

2. Out of control terminal, login session and process group

A logon session can contain multiple process groups that share a control terminal,This control terminal is usually the login terminal for the creation process, the control terminal, the logon session and the process group are usually inherited from the parent process. Our aim is to get rid of them so that they are not affected by them.

The method is based on the 1th, call Setsid () to make the process a conversation leader:

It should be explained that the SETSID () call will fail when the process is the session leader, but the 1th has ensured that the process is not the conversation leader. After the Setsid () call succeeds, the process becomes the new session leader and the new process leader, detached from the original login session and the process group, and the process is detached from the control terminal due to the exclusive nature of the session process to the control terminal.
Specifically, the operation is:
(a) to become the first process in the new dialogue period
(b) The first process to become a new process group
(c) No control terminals.

3, prohibit the process to reopen the control terminal (fork the principle of the second time)

now, the process has become a session leader without terminal, but it can re-apply to open a control terminal. You can prevent a process from reopening the control terminal by making the process no longer a session leader:

4. Close Open File descriptor

The process inherits the open file descriptor from the parent process that created it. Without shutting down, system resources will be wasted, causing the file system in the process location to fail to unload and unexpected errors. In general, it is necessary to close the 0, 1, 23 file descriptors, standard input, standard output, standard error. Because we generally want the daemon to have a system of information output and input, rather than sending everything to the terminal screen. Call Fclose ();

5. Change the current working directory

Change the current working directory to the root directory. The current working directory inherited from the parent process may be in an assembled file system. Because the sprite process is always present before the system reboots, the file system cannot be disassembled if the current working directory of the sprite process is in an assembly file system.

In addition, some sprite processes may change the current working directory to a specified location, where they do their work. For example, the line printer spool daemon often changes its working directory to their s P o l directory.
Can call ChDir ("directory");

6. Resetting the file creation mask

Set the file mode to create the mask word to 0. Creating a masked word from an inherited file may deny setting certain permissions. For example, if the sprite process to create a group of readable, written files, and inherited file mode to create a mask, blocking the two permissions, the required group readable, write will not work.

7, processing SIGCHLD signal

Processing of SIGCHLD signals is not required. However, for some processes, especially server processes, it is often necessary to produce a child process request when the request arrives. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) and still consumes system resources. If the parent process waits for the child process to end, it increases the burden on the parent process and affects the concurrency performance of the server process. The operation of the SIGCHLD signal can be simply set to sig-ign under System V:

Signal (sigchld,sig_ign);

In this way, the kernel does not spawn a zombie process at the end of the subprocess, unlike BSD4, which must show waiting for the end of the subprocess to release the zombie process under BSD4.

Python version Code

def initdaemon (Stdoutfd, Stderrfd=none, basepath=None):"""Initialize into daemon process"""BasePath ='/' ifBasePath is NoneElseBasePath curtimestr= Time. Strftime ("%y-%m-%d%h:%m:%s") try:stdoutfd.Write("Start on%s,"%curtimestr) Stdoutfd.flush () Except:raise try:ifOs.fork () >0: Os._exit (0) except Oserror:raise OSError ("Fork Error") Os.chdir (basepath) os.setsid () Sys.stdout=stdoutfd Sys.stderr= StdoutfdifSTDERRFD is NoneElseSTDERRFD Sys.stdin= Open ("/dev/null",'R') Try:ifOs.fork () >0: Os._exit (0) except Oserror:raise OSError ("Fork 2 Error") PID=int(Os.getpid ()) stdoutfd.Write("pid=%s\n"%pid) Stdoutfd.flush () return

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.