Interprocess relationships and daemons

Process group/job/session relationship

Process group : A process group is a collection of one or more processes, each process group has a unique process group ID, and each process group can have
a group leader process. The identity of the group leader process is that its process group ID is equal to its process ID. A group leader process can create a process group, create processes in that group
, and then terminate. As long as a process exists in a process group, the process group exists, which has nothing to do with whether the group leader process terminates.

close.

Jobs : Shell controls not processes but jobs or process groups by dividing the front and back. A foreground job can be composed of multiple processes, a background can be composed of multiple processes, and the shell can run a foreground job and multiple background jobs, which is called job control.

The difference between a job and a process group: If a process in a job creates a child process, the child process does not belong to the job. Once the job is finished, the Shell will bring itself to the foreground (the child process is still there, but the child process does not belong to the job), if the original foreground process still exists (if the child process has not terminated), it automatically becomes the background process group . Starting a new job in the foreground, the shell is unable to run because he is referred to the background. But if the foreground process exits, the shell is brought to the foreground again, so it can continue to accept user input. 
Session : A session is a collection of one or more process groups. A session can have one controlling terminal. This is usually the terminal
device (in the case of terminal login) or pseudo-terminal device (in the case of network login) to which it is logged. The session leader process that establishes the connection with the controlling terminal is called the
controlling process. Several process groups in a session can be divided into a foreground process group and one or more background process groups. so a session

, should include the controlling process (the session leader), a foreground process group and any background process groups.

The relationship between the three can be understood as: one or more process groups form jobs, and one or more jobs form sessions.

daemon

concept:

Daemon, also known as Daemon, is a special process that runs in the background . It is independent of the controlling terminal and periodically performs
some kind of task or waits for some event to occur. A daemon is a very useful process. Most servers in Linux are implemented as daemons
. For example, ftp server, ssh server, web server httpd, etc. At the same time, the daemon performs many system tasks. For example,
job planning process crond, etc.
When the Linux system starts, many system service processes are started. These system service processes have no controlling terminal and cannot directly interact with the user. Other processes
are created when the user logs in or runs the program, and terminates when the operation ends or the user logs out, but the system service process (daemon process) is not subject to user login.

The logout affects them running all the time. This process has a name called a daemon (Daemon).

Check the daemon process under the terminal: ps axj | more , any TPGID column with -1 written is a process that does not control the terminal, that is, a daemon process.



Create a daemon

The most critical step in creating a daemon is to call the setsid function to create a new Session and become the Session Leader.

#include <unistd.h>
pid_t setsid(void);

When the function is called successfully, it returns the id of the newly created Session (in fact, the id of the current process), and returns -1 on error.

Note that before calling this function, the current process is not allowed to be the leader of the process group, otherwise the function returns -1. It is also easy to ensure that the current process is not the leader of the process group, just fork and then call setsid. The child process created by fork and the parent process are in the same process group, the leader of the process group must be the first process of the group, so the child process cannot be the first process of the group, calling setsid in the child process will There will be no problem.

Steps to create a daemon:

1. Call umask to set the file mode creation mask to 0.

2. Call fork, and the parent process exits (exit). Reasons: 1) If the daemon was started as a simple shell command, the parent process terminates so that the shell thinks the command has finished executing. 2) Ensure that the child process is not the leader process of a process group.

3. Call setsid to create a new session. setsid will cause: 1) The calling process becomes the first process of the new session. 2) The calling process becomes the leader process of a process group. 3) The calling process has no controlling terminal. (Fork again to ensure that the daemon process will not open the tty device afterward)

4. Change the current working directory to the root directory.

5. Close file descriptors that are no longer needed.

6. Others: ignore the SIGCHLD signal.

Show results:




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326574229&siteId=291194637