Inter-process relationships

Source: Internet
Author: User

  1. Process Group

    A process group is a collection of one or more processes. Each process belongs to a process group in addition to a process ID. Each process group has a unique process group ID. Each process group can have a leader process. (Generally, the first process is the leader process) the identity of the leader process is that its process group ID equals the ID of its process.

    as long as a process in a process still exists, the process group exists, regardless of whether its leader process terminates.

  2. Homework

    The shell does not control the process but the job or the process group. When you run a binary program on the command line, a job is turned on. A foreground job can consist of multiple processes, and a background job can consist of many processes.

    Job Control: The shell can run a foreground job and any number of background jobs

    the difference between a job and a process group : If a process in the job creates a child process, the child process is not part of the job but belongs to a process group. Once the job runs, the shell mentions itself to the foreground, and if the original foreground process still exists, it automatically becomes the background process.

  3. Session

    A session is a collection of one or more process groups. A session can have a control terminal.

    Opening a terminal is equivalent to opening a session.

    Control process: Establish session first process with control terminal connection

    One session should include: Control process (Session first process), a foreground process group, and any background process group


4. Daemon Process

Daemons are also known as daemon processes. 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 process certain occurring processes.

The most critical step in creating a daemon is to call the SETSID function to create a new session.

pid_t setsid (void);

Return value: When successful, returns the ID of the newly created session (which is actually the ID of the current process)

Error returns-1.

Note: The current process does not allow the leader of the process group until this function is called, otherwise the function returns-1.

The result of a successful call to this function is:

<1> create a new session, the current process is called session leader, the ID of the current process is the ID of the session

<2> Create a new process group, the current process becomes the leader of the process group, and the ID of the current process is the ID of the process group

<3> if the current process originally had a control terminal, it would lose the control terminal and become a process without the control terminal. The so-called loss of control terminal is that the original control terminal is still open, can still read and write, but just an ordinary open file instead of the control terminal.


To create a daemon:

<1> call Umask to set the file mode creation screen Word to 0

<2> Call Fork, parent process exit (exit).

Cause: In general, the process group's first process is the process leader, fork out the child process, and the parent process exits, guaranteeing 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 to be the first process of a new session (2) The call process becomes a process group leader process (3) The calling process does not control the terminal

<4> change the current working directory to the root directory

<5> close file descriptors that are not needed

<6> ignoring SIGCHLD signals

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/3E/wKiom1eeALeCY1zKAAA-hEOcJc0427.png "title=" 1.PNG " alt= "Wkiom1eealecy1zkaaa-heocjc0427.png"/>


To create a daemon:

int daemon (int nochdir,int noclose)

When Nochdir is zero, the current directory becomes the root directory, otherwise it is unchanged;

When the Noclose is zero, the standard input, standard output, and error output are redirected to/dev/null, which means no information is output, otherwise output.

return value:

Deamon () calls fork (), and if the fork succeeds, the parent process calls _exit (2) to exit, so the error message that you see is all generated by the child process. If the success function returns 0, return 1 and set errno.

If the run succeeds, the parent process commits suicide after the daemon function has finished running, and later hibernation and printing are all child processes to run.


In some materials, when creating a daemon, first fork the child process, let the parent process terminate, and then the child process fork out grandson process, let grandson process Setsid. why fork two times ?


Analysis: The second fork is not necessary first. The primary purpose of the second fork is to prevent the process from opening a terminal again. because opening a terminal is the condition that the process must be the session leader, in the fork once, grandson process ID! = SID (Sid is the SID of the child process). Therefore, the new control terminal cannot be opened.


This article from "Together to see the Stars" blog, reproduced please contact the author!

Inter-process relationships

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.