Process Group ID Action

Source: Internet
Author: User



Process relationships

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 or sets the process group ID of a process. All of these interfaces are suitable for Linux, but generally use GETPGRP to get the group ID of the process, using Setpgid () to set the group ID of the process


Setpgid (pid_t pid, pid_t pgid) function
PID = 0 Sets the group ID of the calling process
PID > 0 Setting the group ID for the specified PID process is Pgid
Pgid = 0 Sets the group ID of the specified PID process to PID, creating a process group, the PID process as the leader
If you want to move a process from process Group A to the process group B,a and B must be in the same session.


Getpgrp (void) function
Returns the process group ID of the calling process


Getpgid (pid_t pid) function
PID = 0 Returns the process group ID of the calling process
PID > 0 Returns the process group ID 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
Success Setpgid () and SETPGRP () return 0, Failure returns-1, setting errno
GETPGRP () returns the group ID of the calling process
Getpgid () and Getpgrp (psid_t PID) successfully returned the process group ID, failed return-1, set errno

errno Error Definitions
Eacces attempts to modify the process group ID (setpgid (), SETPGRP ()) of the child process generated through EXECVE
EINVAL pgid<0 (Setpgid (), SETPGRP ())
Eperm 1. Attempt to move a process to another process group that is not in the same session
2. Attempt to modify the process group of the child process of the calling process, but the child process is in another session
3. Attempt to modify the process group ID of the session's first process
(Setpgid (), SETPGRP ())
Esrch 1.getpgid () specifies that the PID process does not exist
2.setpgid () specifies that the PID process is neither the calling process nor the child process of the calling process

Attention
1.fork child processes inherit groups from the parent process id,execve the child processes that are created do not inherit
2. A session can have a control terminal. At any time, in particular, only one session in a process group is the foreground process group, and the other process groups in the session are background process groups. If the control terminal receives a signal, the signal is sent to the foreground process group. Only front desk

The process group can get to that signal. If the background process wants to get a signal from the control terminal, then the background process group sends the SIGTSTP signal bar to suspend the foreground process in the call function Tcsetpgrp to control the background process to form the foreground process group
3. If a session has a control terminal, the control terminal flag bit clocal is not set, and the terminal hangs up, then the session first process will emit a sighup signal. If the end of the session first process terminates, a SIGHUP signal is issued to the foreground process group

of all processes.
4.setpgid () and GETPGRP () can also be used for job control
5. If a process in a process group exits unexpectedly causing the entire process to become an orphan process, and there is a process in all the orphan processes that ends, then signals Sigcont and Sighup will be sent to each orphan process

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Process Group ID Action

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.