Orphan process, zombie process, and daemon process, orphan daemon process

Source: Internet
Author: User

Orphan process, zombie process, and daemon process, orphan daemon process

Wikipedia:

In the operating system field,Orphan ProcessRefers to the parentProcessThe first type of operation that continues after execution is completed or terminated.Process.

In UNIX-like systems,BotnetsIt refers to the execution completed (called by the exit system, or a fatal error occurs during running or a termination signal is received)ProcessThe table still has a table item (ProcessControl Block PCB), in the "terminated" stateProcess.

In a multi-engineering E-commerce system,Daemon(English: daemon, English pronunciation:/di too m then n/or English pronunciation:/ˈ de too m then n/) is a computer program executed in the background. Such programs areProcess.DaemonThe program name usually ends with the letter "d". For example, syslogd refers toDaemon.

 

Personal Understanding:

1. Generally, the child process is created by the parent process, and the exit of the child process and the parent process is unordered. No one knows who exits first. Normally, when the parent process ends, the wait or waitpid function is called to wait for the child process to complete and then exit. Once the parent process does not wait for the child process to exit, the remaining sub-processes will be received by the init (pid = 1) process and become orphan processes. (All processes except init in the process tree have parent processes ).

2. If the child process exits first, the parent process has not ended and the wait or waitpid function is not called to obtain the status information of the child process, then the residual State Information (task_struct structure and a small amount of resource information) of the sub-process will become a zombie process.

3. daemon refers to a process that runs in the background and does not control the connection of the terminal. It is independent of the control terminal and usually periodically executes a task. The daemon is separated from the terminal to prevent the information in the execution process from being displayed on any terminal and the process is not interrupted by any terminal information generated by any terminal.

 

Hazards:

After the orphan process ends, it will be taken care of by the init process and will not cause any harm. The zombie process will always occupy the process number, and the limited number of operating system processes will be affected.

Solution:

Generally, a zombie process is generated because of the parent process. You can solve the problem by killing the parent process. At this time, the zombie process becomes an orphan process and is received by the init process.

 

Write the daemon process:

In different Unix environments, the detailed programming details of the daemon are inconsistent. Fortunately, the programming principles of the daemon are the same. The difference is that the specific implementation details are different. This principle is to satisfy the characteristics of the daemon. The programming rules are as follows:

1. Run in the background

To avoid suspending the control terminal, you need to put the daemon into the background for execution by calling fork in the process to terminate the parent process and let the daemon be executed in the background of the child process. Specifically, fork is called and the parent process is exited. In this way, the following points are implemented:
First, if the wizard process is started by a simple s h e l command, terminate the parent process so that s h e l considers that the command has been executed completely.
Second, the child process inherits the process group I d of the parent process, but has a new process I D, which ensures that the child process is not the first process of a process group. This is a necessary precondition for the next s e t s I d call.

2. log on to the session and process group from the control terminal

A logon session can contain multiple process groups that share a control terminal. This control terminal is usually used to create a logon terminal and control terminal for a process, logon sessions and process groups are generally inherited from the parent process. Our goal isIs to get rid of them, so that they are not affected.

The method is to call setsid () on the basis of the first point to make the process a session leader:

It should be noted that when the process is the session leader, the setsid () call will fail, but the first point is that the process is not the session leader. After the setsid () call is successful, the process becomes a new session leader and a new process leader, and disconnects from the original logon session and process group. Because the session process is dedicated to the control terminal, the process is separated from the control terminal at the same time.
The specific operation is:
(A) become the first process in the new dialog Period
(B) The first process to become a new process group
(C) There is no control terminal.

3. Prohibit the process from re-opening the control terminal (the second fork principle)

Now, the process has become the no-end session leader, but it can re-apply to open a control terminal. You can disable the process from re-opening the control terminal by making the process no longer the session leader:

4. Disable the opened file descriptor.

A process inherits the opened file descriptor from the parent process that created it. If you do not close it, system resources will be wasted, causing undetachable and unexpected errors to the file system where the process is located. Generally, it is necessary to disable file descriptors 0, 1, and 2, namely, standard input, standard output, and standard error. Because we generally want the daemon to have a set of information output and input systems, 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 usually exists until the system is started again, if the current working directory of the sprite process is in an assembly file system, the file system cannot be detached.

In addition, some genie processes may change the current working directory to a specified location to do their work. For example, the fake offline wizard process of a row printer often changes its working directory to their s p o l directory.
You can call chdir ("directory ");

6. Resetting the file creation mask

Set the blocking word created in file mode to 0. Creating a blocked word by using an inherited file may deny some permission settings. For example, if a Sprite process creates a group of readable and written files, and the inherited file creates a blocked word, the two permissions are blocked, then, the Group read and write operations are not allowed.

7. Process SIGCHLD Signal

Processing SIGCHLD signals is not necessary. However, some processes, especially server processes, often generate requests from child processes when requests arrive. If the parent process does not wait for the child process to end, the child process will become a zombie process (zombie) and still occupy system resources. If the parent process waits for the child process to end, it will increase the burden on the parent process and affect the concurrent performance of the server process. Under System V, the operation of SIGCHLD signal can be set as SIG-IGN simply:

Signal (SIGCHLD, SIG_IGN );

In this way, the kernel will not generate a zombie process when the child process ends. This is different from BSD4. In BSD4, the zombie process must be released only after the child process ends.

 

Python version code

Def initDaemon (stdoutfd, stderrfd = None, basePath = None): "initialized to daemon process" basePath = '/' if basePath is None else basePath curTimeStr = time. strftime ("% Y-% m-% d % H: % M: % S") try: stdoutfd. write ("Start on % s," % curTimeStr) stdoutfd. flush () handle T: raise try: if OS. fork ()> 0: OS. _ exit (0) handle T OSError: raise OSError ("fork error") OS. chdir (basePath) OS. setsid () sys. stdout = stdoutfd sys. stderr = stdoutfd if stderrfd is None else stderrfd sys. stdin = open ("/dev/null", 'R') try: if OS. fork ()> 0: OS. _ exit (0) handle T OSError: raise OSError ("fork 2 error") pid = int (OS. getpid () stdoutfd. write ("pid = % s \ n" % pid) stdoutfd. flush () return

 

Related Article

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.