Basic attributes of a process: process ID, parent process ID, process group ID, session, and control terminal.

Source: Internet
Author: User

Basic attributes of a process: process ID, parent process ID, process group ID, session, and control terminal.
Abstract:This document describes the basic attributes of a process, including process ID, parent process ID, process group ID, session, and control terminal.

Basic Process attributes

1. process ID (PID) Function Definition:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t getpid (void );
Function Description:
Each process has a unique process ID (PID) represented by a non-negative integer ). well, like our ID card, each person's ID card number is unique. because the process ID Identifier is always unique and is often used as a part of another identifier to ensure its uniqueness, the process ID (PID) cannot be modified at the user layer.
In Linux, a process with a PID of 0 is usually a scheduling process, which is often called an exchange process and the first system process. The first user process is the init process, and its PID is 1.
In application programming, call the getpid () function to obtain the PID of the current process. This function has no parameters. If the execution is successful, return the PID of the current process. If the execution fails, return-1, error causes are stored in errno.
Example 1: print your own process ID (PID ).
# Include <unistd. h> # include <stdio. h> int main () {pid_t pid; // pid_t is actually int pid = getpid (); printf ("the current program's pid is % d \ n ", pid); while (1); return 0 ;}

Run the "ps u" command to check the comparison.

2. Parent process ID (PPID) Function Definition:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t getppid (void );
Function Description:
Any process (except the init process) is created by another process. This process is called the parent process of the created process, and the created process is called a child process. The parent process ID cannot be modified at the user layer. the parent process ID is the parent process ID (PPID) of the child process ).
You can call the getppid () function to obtain the parent process ID (PPID) of the current process ). this function has no parameters. If the execution is successful, the system returns the parent process ID (PPID) of the current process and-1 if the execution fails. The error cause is stored in errno.
Example 1: print your parent process PPID.
# Include <unistd. h> # include <stdio. h> int main () {pid_t ppid; // pid_t is actually int ppid = getppid (); printf ("the current program's ppid is % d \ n ", ppid); while (1); return 0 ;}

Run the "ps u" command to check the comparison.

3. process group ID (process group id pgid) Function Definition:
# Include <unistd. h>
Int setpgid (pid_t pid, pid_t pgid );
Pid_t getpgid (pid_t pid );
Pid_t getpgrp (void);/* POSIX.1 version */
Pid_t getpgrp (pid_t pid);/* BSD version */
Int setpgrp (void);/* System V version */
Int setpgrp (pid_t pid, pid_t pgid);/* BSD version */
Function Description:
In Linux, each user has a user ID (UID) and a user group ID (GUID ). the process also has its own PID and PGID ). A process group is a collection of one or more processes. They are associated with the same job. each process group has a unique PGID, which can be modified at the user layer. for example, to add a process to another process group, you can use the setpgid () function to modify the process group ID.
You can call the getpgid () function to obtain the progress group ID (PGID) of the current process ). if this parameter is set to 0, the ID of the Process Group of the current process is obtained. If the execution is successful, the ID of the Process Group (PGID) of the current process is returned. If the execution fails, the value-1 is returned. The error cause is stored in errno. we recommend that you replace the getpgid (pid) function with the non-parameter getprgp () function in POSIX.1.
The process group ID (PGID) can also be obtained through the getpgrp () function. The child process generated by the fork () function will inherit the process group ID (PGID) of its parent process ).
Each process group can have a leader process. The process group ID of the leader process is equal to the process ID. however, the leader process can exit first, that is, as long as a process exists in a process group, the process group exists and has nothing to do with the existence of the leader process. the final process of the Process Group can be exited or transferred to another group.
You can add a process to a process group and call the system function setpgid (). the first parameter is the process ID (PID) of the Process Group ID (PGID) to be modified, and the second parameter is the new process group ID (PGID). If the two parameters are equal, the process specified by the pid is changed to the Process Group Leader. If the pid is 0, the caller's process ID (that is, the ID of the current process's process group (PGID is the specified pgid) is used. If pgid is 0, the pid specifies the process ID (PID ), use the process group ID (PGID) (that is, the process referred to by the pid as the process leader process of the process group ).
A process can only set a process group ID (PGID) for itself or its sub-process. If a series of functions such as exec () are called in its sub-process, the process group ID (PGID) of the sub-process can no longer be changed ).
#include <unistd.h>#include <stdio.h>int main(){        int i;        printf("\t pid \tppid \t pgid\n");        printf("parent:\t%d\t%d\t%d\n",getpid(),getppid(),getpgid(0));        for(i=0; i<2; i++)        {                if(fork()==0)                {                        printf("child:\t%d\t%d\t%d\n",getpid(),getppid(),getpgid(0));                }        }        sleep(500);        return 0;}

Output:

4. session) Function Definition:
# Include <unistd. h>
Pid_t getsid (pid_t pid );
Pid_t setsid (void );
Function Description:
A session is a collection of one or more process groups. The getsid () function is called by the system to obtain the session ID (SID) of a process ).
If the pid is 0, the session SID of the calling process is returned. In general, the transformation is equal to the Process Group ID (PGID). If the pid does not belong to the session where the caller is located, the caller cannot obtain the SID.
The session ID of a process can also be modified. The setsid () function is called to create a new session.
If the called process is already the leader of a process group, this function returns an error. If not, a new session is returned.
(1) The process becomes the first process of the new session. The first process is the process that creates the session. At this time, the process is the only process of the new session.
(2) The process becomes the leader process of a new process group. The new process group ID (PGID) is the pid of the called process.
(3) The process has no control terminal. If the process has a control terminal before setsid () is called, this connection will also be interrupted.
 

Figure 1 process arrangement of a process combination session

5. controlling terminal) Function Definition:
# Include <unistd. h>
Pid_t tcgetpgrp (int fd );
Int tcsetpgrp (int fd, pid_t pgrp );
Function Description:
Relationship between sessions and Process Groups:
(1) A session can have a control terminal. The first session process established on the control terminal is called a control process.
(2) Several process groups in a session can be divided into one foreground process group and several background process groups. If a session has a control terminal, it has a foreground process group.
(3) Whenever you type the terminal interrupt key, the interrupt Message ID is sent to all sessions in the foreground process group, all sessions in the foreground Process Group will be sent with the exit ID.
If the terminal detects that the modem (or Network) is disconnected, it will send the hang-up ID to the control process (the first process of the session ).
Call the tcgetgrpt () function to obtain the ID of the foreground process group associated with the opened terminal.
Call the tcsetgrpt () function to set whether a process group is a foreground process or a background process group.
If a process has a control terminal, set the foreground Process Group ID to pgrp. The pgrp value should be the ID of a process group in the same session, and fd is the file descriptor of the control terminal.
If the tcsetpgrp () function is called for a process in the background process group in the session, the process will not be blocked, or the SIGTTOU signal will be ignored. The signal SIGTTOU will be sent to all processes in the process group.
When fd is a control terminal pointing to a process, the function tcgetpgrp () returns the process group ID of the foreground Process Group of the terminal. The ID value is greater than 1 and is not used. if fd points to a terminal that is not a process, the function returns-1 and sets errno to indicate an error.
 
Figure 2 process groups and sessions of the control terminal

Author: my personal abilities are limited, just for reference. If the reader finds any errors in this article, please submit them.
-- Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Do not build a high station in the sand float, calm down and slowly accumulate -----------------------------------------Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

What is the parent process ID?

The parent process is of course relative to the child process generated by it. The parent process ID, which is the ID of the process. It is the unique process identifier of the system!
 
What are the Parent and Child processes?

It should be a tree row Structure
Attributes of the parent process inherited by the child process:
Actual UID, GID and valid UID, GID.
. Environment variable.
. Add GID.
. The flag to close when exec () is called.
. UID Setting Mode bit.
. GID sets the bitwise of the mode.
. Process Group number.
. Session ID.
. Control terminal.
. Current working directory.
. Root directory.
. File Creation mask UMASK.
. File length limit ULIMIT.

These are different based on different calls.

Common multi-process programming system calls
1. fork ()
Function: Creates a new process.
2. system ()
Function: generate a new process that executes the specified command.
4. popen ()
Function: Initialize the pipeline from/to a process.
5. pclose ()
Function: Close the pipeline to a process.
6. wait ()
Function: waits for a sub-process to return and modify the status.
7. waitpid ()
Function: waits for the return and modification status of the sub-process of the specified process number.
8. setpgrp ()
Function: sets the process group number and session number.
9. exit ()
Function: terminate a process.
10. signal ()
Function: Signal Management
11. kill ()
Function: sends a signal to one or more processes.
12. alarm ()
Function: sets the timeout clock for a process.
13. msgsnd ()
Function: Send a message to a specified message queue.
14. msgrcv ()
Function: gets a specified type of message from the message queue.
15. msgctl ()
Function: Message control operation
16. msgget ()
Function: Get a message queue.

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.