2. Relationship between processes: Process Groups, sessions, and Daemon Processes

Source: Internet
Author: User
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.

2. Relationship between processes: Process Groups, sessions, and Daemon Processes

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.