Daemons and sessions, process groups under Linux

Source: Internet
Author: User

Daemons, sessions, and process groups have a lot of good information on the web. I also collected a bunch of online, plus my own understanding. Dare not say original, just write in this fear oneself forget it. Caishuxueqian, inevitably have the mistake, welcome everybody to correct. The following writing is very good, we can go to see: http://www.cnblogs.com/forstudy/archive/2012/04/03/2427683.html. The following section refers to his description.

Process Group: A process group is a group of one or more processes, assigning a group ID (pgid) to the group, which is usually the PID of the process group leader (the first process in the group). If the process group leader exits, the pgid of its group is not changed. Before this group is dissolved, the system will not assign a PID with the same value as this pgid to other programs. Process groups are designed to facilitate the management of a set of processes, such as terminating a set of processes at the same time. There are also designs on the bottom of the system, such as pipelines. On an application, it is often used for multi-process programs to determine the state of other processes.

Sessions: Multiple process groups form a single session. Usually when we log on to a user, we generate a session that ends when we exit. Processes that belong to the session (such as our browser, music playback) will be terminated at the end of the session.

Terminal Shell: In the past, the computer had no interface, and all operations were command lines. As a result, the program at the time is often related to the shell, which requires the shell to display and enter information. For example, the top program, if you exit the shell directly, will also terminate it. But now the interface is much, many do not rely on the shell. But many server programs are still running on the terminal, and if you want to be a daemon, you need to get out of the terminal.

Many operating systems are multi-user. After we turn on the user, then open the program, start the job (Internet, watch video, etc.). However, many programs do not start the job until the user logs on, which is generally a daemon, such as a network service. Daemons are not user-dependent. If a program on a server is turned on after the user logs on and terminates after the user exits, how is this possible. So the characteristics of a daemon:

Daemon Process Programming steps 1. Create child process, parent process exitsall work is done in child processesformally separated from the control terminal (cannot be terminated by the shell via CTRL + C) 2. Create a new session in a child processSetsid () functionLeave the child process completely independent, out of control (the service does not stop when the user exits the terminating session) 3. Change the current directory to the root directorychdir () functionprevents the use of an unmounted file systemcan also be replaced by other paths 4. Resetting the file permission maskumask () functionprevent inherited file creation masks from denying certain permissionsIncreased daemon Flexibility 5. Close the file descriptorinherited open files are not used, waste system resources, cannot be uninstalledgetdtablesize ()returns the number of entries in the file descriptor for the process, that is, the text that the process opens Number of pieces

Among them, 1th 2nd is necessary, others can decide whether to deal with their own procedures.

Daemons and sessions, process groups under Linux

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.