Process Group ID operation, process group id

Source: Internet
Author: User

Process Group ID operation, process group id
Zookeeper

Process relationship

Call format
# 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 (psid_t pid);/* BSD version */

Int setpgrp (void);/* System V version */
Int setpgrp (pid_t pid, pid_t pgid);/* BSD version */
Description: gets and sets the id of a process group. All these interfaces are applicable to linux, but getpgrp is generally used to obtain the group id of a process. setpgid () is used to set the group id of a process.


Setpgid (pid_t pid, pid_t pgid) Function
Pid = 0 set the group id of the calling Process
Pid> 0 set the group id of the specified pid process to pgid
Pgid = 0 set the pid Process Group id to pid, create a process group, pid process as the leader
If you want to move A process from process group A to process group B, A and B must be in the same session.


Getpgrp (void) Function
Returns the id of the process group that calls the process.


Getpgid (pid_t pid) Function
Pid = 0 return the id of the process group that calls the process
Pid> 0 returns the id of the Process Group of the specified process


Setpgrp (void) Function
Equivalent to setpgid (0, 0)

Setpgrp (pid_t pid, pid_t pgid) Function
Equivalent to setpgid (pid, pgid)


Getpgrp (psid_t pid) Function
Equivalent to getpgrp (void)


Return Value
If setpgid () and setpgrp () are successful, 0 is returned,-1 is returned, and errno is set.
Getpgrp () returns the group id of the calling Process
Getpgid () and getpgrp (psid_t pid) are returned successfully. If the process group id fails,-1 is returned. errno is set.

ERRNO error Definition
EACCES attempts to modify the process group id (setpgid (), setpgrp () of sub-processes generated through execve ())
EINVAL pgid <0 (setpgid (), setpgrp ())
EPERM 1. attempted to move the process to another process group not in the same session
2. attempt to modify the Process Group of the sub-process that calls the process, but the sub-process is in another session
3. attempt to modify the process group id of the first PROCESS OF THE SESSION
(Setpgid (), setpgrp ())
ESRCH 1. getpgid () the specified pid process does not exist
2. setpgid () specifies that the pid process is neither a calling process nor a sub-process that calls the process.

Note:
1. The fork sub-process inherits the group id from the parent process. The sub-process created by execve does not inherit the group id.
2. A session can have a control terminal. At any time, in particular, only one process group in the session is the foreground process group, and other process groups in the session are the background process groups. If the control terminal receives a signal, the signal is sent to the foreground process group. Only the front-end

The process group can obtain this signal. If the background process wants to obtain the signal from the control terminal, the background process group sends the SIGTSTP signal and suspends the foreground process to call the tcsetpgrp function to control the background process and form the foreground process group.
3. If a session has a control terminal, the control terminal flag CLOCAL is not set, and the terminal hangs up, the first process of the session sends a SIGHUP signal. If the first process ends, a SIGHUP signal is sent to the frontend process group.

All processes.
4. setpgid () and getpgrp () can also be used for Job Control
5. If a process in the Process Group unexpectedly exits and the whole process is composed of an orphan process, and one of all orphan processes is finished, the signal SIGCONT and SIGHUP will be sent to each Orphan process.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.