2 Relationship Between processes: Process Group, session, daemon, and session daemon
Zookeeper
1. Process Group
A collection of one or more processes. The process group ID is a positive integer. Function used to obtain the ID of the current process group.
Pid_t getpgid (pid_t pid)
Pid_t getpgrp (void)
Obtain the parent-child process group
Running result:
Process ID of the LEADER: Process Group ID = process ID
The leader process can create a process group, create a process in the process group, and terminate the process. As long as there is a process in the process group, the process group exists, regardless of whether the leader process is terminated.
Process Group Survival: process group created to the last process to exit (terminate or transfer to another process group)
A process can set a process group ID for itself or its sub-processes.
Int setpgid (pid_tpid, pid_t pgid );
If the sub-process is changed to a new group, use
Non-root processes can only change the sub-processes created by themselves or processes with Operation Permissions
Setpgid () is added to an existing process group or a new process group is created. For example, the Parent and Child processes are changed to a new group.
Running result:
2 sessions
Pid_t setsid (void );
A: The Calling process cannot be the process group leader. The process becomes the first process (sessionheader) of the new session)
B: The process becomes the leader process of a new process group.
C: root permission required (not required for ubuntu)
D: the new session discards the original control terminal, which has no control terminal.
E: If the calling process is a leader process, an error is returned.
F: when a new session is established, fork is called first, the parent process is terminated, and the child process is called.
Pid_t getsid (pid_t pid );
If the pid is 0, the current process sessionID is viewed.
Run the ps ajx command to view the processes in the system. Parameter a indicates not only listing the processes of the current user, but also listing all other processes
User process. parameter x indicates that not only processes with control terminals are listed, but also processes with no control terminals are listed. parameter j indicates that information related to job control is listed.
The leader process cannot be the first process of a new session. The first process of a new session must be the leader process.
Run:
Open another terminal. Enter ps ajx at the beginning. The running result is as follows:
Wait a moment and enter ps ajx again. The running result is as follows:
The process automatically disappears after 20 seconds of sleep.
3 daemon
Concept
A Daemon process is a background service process in Linux that has a long life. It is usually independent of the control terminal and periodically executes a task or waits for processing certain events.
B Model
Daemon programming steps
B1 creates a sub-process and the parent process exits.
All work is performed in the sub-process
The form is separated from the control terminal
B2 create a session in the sub-process
Setsid () function
Independent sub-processes from control
B3 change the current directory to the root directory
Chdir () function
Prevents the use of detachable file systems
You can also switch to another path.
B4 resetting the File Permission mask
Umask () function
Prevents the creation of blocked characters in an inherited file from rejecting certain permissions.
Increase the flexibility of the daemon process
B5 disable file descriptor
The inherited open file is not used, which wastes system resources and cannot be uninstalled.
B6 started to execute the core work of the daemon process
B7 daemon exit handling
Case study:
Run this program. It becomes a daemon and is no longer associated with the current terminal. You cannot see it using the ps command. You must run the ps command with the x parameter to see it. You can also see that closing the terminal window or logging out does not affect the running of the daemon process.
Fork and setsid functions in the Linux daemon
After the first child process is generated, call fork () again to generate another child process. Of course, it also follows the "return twice" feature of fork.
First, consider that processes are independent of each other, while child processes only inherit the code segment and Data Segment of the parent process and the execution environment.
The second fork will not call setsid. The second sub-process that calls fork only executes the code after umask (0. That is to say, it is not a session leader. Therefore, the second fork is an independent process and is out of control from the terminal.
Question about the role of the second fork () of the linux daemon
First, the control terminal controlling terminal refers to/dev/tty, which is written in the first line of man 4 tty. For the shell you log on to,/dev/tty is the terminal you are using, and the device number is (5, 0 ). Run the "tty" command to check which actual terminal device it corresponds. /Dev/tty is similar to a connection to the actually used terminal device.
Second, the key after the first fork step is setsid.
You can find the setsid,
1. If the first process in the Process Group cannot call setsid, The EPERM error is returned. This is the reason for the first fork. Execute the first fork and let the parent process exit. The child process continues, at this time, because the child process will inherit the GID of its parent process and have its own process PID, This PID and GID must be different. The sub-process cannot be the first process in the process group and meets the conditions for calling setsid.
2. After setsid is called, all connections to the control terminal will be disconnected.
Finally, execute fork again to let the sub-process exit, and the sun process continues. In this way, the sun process pid is not equal to the session sid where it is located, so that it will not become the first session process itself, so that it cannot open the control terminal. In this way, the daemon process gets a clean environment, which will not be disturbed by the signal generated by the terminal.