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 exits•
all work is done in child processes•
formally separated from the control terminal (cannot be terminated by the shell via CTRL + C)
2. Create a new session in a child process•
Setsid () function•
Leave 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 directory•
chdir () function•
prevents the use of an unmounted file system•
can also be replaced by other paths
4. Resetting the file permission mask•
umask () function•
prevent inherited file creation masks from denying certain permissions•
Increased daemon Flexibility
5. Close the file descriptor•
inherited open files are not used, waste system resources, cannot be uninstalled•
getdtablesize ()•
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