Jobs, process groups, sessions, and daemons

Source: Internet
Author: User
Tags terminates

1. Process Group

Each process belongs to a process group in addition to a process ID. A process group is a collection of one or more processes. Typically, they are associated with the same job and can receive various signals from the same terminal. Each process group has a unique process group ID. Each process group can have a leader process. The identification of the leader process is that its process group ID equals its process ID. The leader process can create a group, create a process in the group, and then terminate. As long as a process exists in a process group, the process group exists, regardless of whether its leader process terminates.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/85/3A/wKioL1edn0by5SsYAAA5aAvLmr8197.png-wh_500x0-wm_3 -wmp_4-s_3969162313.png "title=" process group. png "alt=" wkiol1edn0by5ssyaaa5aavlmr8197.png-wh_50 "/>

2. Job

The shell is not a process but a job or a process group that controls the front and back of the table. A foreground job can consist of multiple processes, one background can be composed of multiple processes, and the shell can run a foreground job and any number of background jobs, which is called job control.

The difference between a job and a process group: If a process in the job has created a child process, the child process does not belong to the job. Once the job runs, the shell mentions itself to the foreground, and if the original foreground process still exists (if the child process has not yet terminated), it automatically becomes the background process group.

Foreground jobs: Jobs that can be interacted and controlled by the user we call the foreground job.

Background jobs: Jobs that can be self-running in memory, users cannot participate in interactions, and use [Ctrl]+c to terminate, only invoke the job through BG or FG.

3. Session

A session is a collection of one or more process groups. A session can have a control terminal. This is usually the terminal device (in the case of terminal landing) or the pseudo terminal device (in the case of network Landing). The first process of establishing a session connected to a control terminal is called the control process. Several process groups in a session can be divided into a foreground process group and one or more background process groups. Therefore, a session should include the control process (session first process), a foreground process group, and any background process group.

4. Control Terminal

A session has only one control terminal, and the control terminal is connected to the lead process of the process group in session.

5. Daemon Process

。 The reason for the existence of the daemon function is because the control terminal sends some signals for some reason (such as disconnecting the terminal link). The default action of receiving the incoming city processing these signals causes the process to exit. These signals are generated by tapping some special keys on the terminal.

A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. Daemons are a very useful process. Most Linux servers are implemented with daemons.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/85/3B/wKioL1edrJzS8e0lAABK8Eq-iLY652.png-wh_500x0-wm_3 -wmp_4-s_1746793961.png "title=" qq picture 20160731154749.png "alt=" Wkiol1edrjzs8e0laabk8eq-ily652.png-wh_50 "/>

The Tpgid is-1, which means the terminal is irrelevant, TTY is?

Creating daemons

1. Call Umask to set the file mode creation shield Word to 0.

2. Call fork, the parent process exits (exit). Reason: 1) If the daemon is started as a simple shell command, the parent process terminates so that the shell considers that the command has been executed. 2) Ensure that the child process is not a process group leader process.

3. Call Setsid to create a new session. Setsid causes: 1) The calling process becomes the first process of a new session. 2) The calling process becomes the leader process of a process group. 3) The calling process does not control the terminal. (Fork once again to ensure that the daemon process does not open the TTY device afterwards)

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

5. Close the file descriptor that is not required.

6. Other: Ignore the SIGCHLD signal.

Note: Some also fork two times

1, the role of the first fork is to let the shell think this command has been terminated, do not hang on the terminal input. There is also a function for the back SETSID service. The caller of the SETSID cannot be the process group leader (group leader). At this point the parent process is the leader of the process group.

2, Setsid () is the most important call of this function. It completes most of the things that the daemon function wants to do. The entire function is called out. Child process is the conversation leader (Sid==pid), is also the process group leader (Pgid = = pid), and out of the original control terminal. In this step, basically no matter how the control terminal. The new process will not receive those signals.

3, after the first 2 steps, basically want to do all done. The 2nd time Fork is not required. The second main purpose of fork is. Prevents the process from opening a control terminal again. Because the foreground condition for opening a control terminal is that the process must be the conversation leader. Fork again, child process id! = SID (Sid is the SID of the process parent process). Therefore, it is not possible to open a new control terminal.


650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/85/3B/wKioL1edr2HyOcN9AAB_G6iitlU980.png-wh_500x0-wm_3 -wmp_4-s_1132051594.png "title=" qq picture 20160731155917.png "alt=" Wkiol1edr2hyocn9aab_g6iitlu980.png-wh_50 "/>

Jobs, process groups, sessions, and daemons

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.