1SETPGID(2)                 Linux Programmer's Manual                SETPGID(2)
2
3
4

NAME

6       setpgid, getpgid, setpgrp, getpgrp - set/get process group
7

SYNOPSIS

9       #include <unistd.h>
10
11       int setpgid(pid_t pid, pid_t pgid);
12       pid_t getpgid(pid_t pid);
13
14       pid_t getpgrp(void);                 /* POSIX.1 version */
15       pid_t getpgrp(pid_t pid);            /* BSD version */
16
17       int setpgrp(void);                   /* System V version */
18       int setpgrp(pid_t pid, pid_t pgid);  /* BSD version */
19
20   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22       getpgid():
23           _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
24           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
25
26       setpgrp() (POSIX.1):
27           _SVID_SOURCE || _XOPEN_SOURCE >= 500 ||
28           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
29
30       setpgrp() (BSD), getpgrp() (BSD):
31           _BSD_SOURCE &&
32               ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
33                  _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE)
34

DESCRIPTION

36       All  of  these interfaces are available on Linux, and are used for get‐
37       ting and setting the process group ID (PGID) of a  process.   The  pre‐
38       ferred,  POSIX.1-specified  ways  of doing this are: getpgrp(void), for
39       retrieving the calling process's PGID; and  setpgid(),  for  setting  a
40       process's PGID.
41
42       setpgid()  sets  the  PGID of the process specified by pid to pgid.  If
43       pid is zero, then the process ID of the calling process  is  used.   If
44       pgid is zero, then the PGID of the process specified by pid is made the
45       same as its process ID.  If setpgid() is used to move  a  process  from
46       one  process  group to another (as is done by some shells when creating
47       pipelines), both process groups must be part of the same  session  (see
48       setsid(2)  and  credentials(7)).   In  this case, the pgid specifies an
49       existing process group to be joined and the session ID  of  that  group
50       must match the session ID of the joining process.
51
52       The POSIX.1 version of getpgrp(), which takes no arguments, returns the
53       PGID of the calling process.
54
55       getpgid() returns the PGID of the process specified by pid.  If pid  is
56       zero,  the  process ID of the calling process is used.  (Retrieving the
57       PGID of a process other than the caller is rarely  necessary,  and  the
58       POSIX.1 getpgrp() is preferred for that task.)
59
60       The  System  V-style setpgrp(), which takes no arguments, is equivalent
61       to setpgid(0, 0).
62
63       The BSD-specific setpgrp() call, which takes arguments pid and pgid, is
64       equivalent to setpgid(pid, pgid).
65
66       The  BSD-specific getpgrp() call, which takes a single pid argument, is
67       equivalent to getpgid(pid).
68

RETURN VALUE

70       On success, setpgid() and setpgrp()  return  zero.   On  error,  -1  is
71       returned, and errno is set appropriately.
72
73       The POSIX.1 getpgrp() always returns the PGID of the caller.
74
75       getpgid(),  and  the  BSD-specific  getpgrp() return a process group on
76       success.  On error, -1 is returned, and errno is set appropriately.
77

ERRORS

79       EACCES An attempt was made to change the process group ID of one of the
80              children  of  the calling process and the child had already per‐
81              formed an execve(2) (setpgid(), setpgrp()).
82
83       EINVAL pgid is less than 0 (setpgid(), setpgrp()).
84
85       EPERM  An attempt was made to move a process into a process group in  a
86              different  session,  or to change the process group ID of one of
87              the children of the calling process and the child was in a  dif‐
88              ferent  session,  or to change the process group ID of a session
89              leader (setpgid(), setpgrp()).
90
91       ESRCH  For getpgid(): pid does not match any process.   For  setpgid():
92              pid  is  not  the calling process and not a child of the calling
93              process.
94

CONFORMING TO

96       setpgid() and the version of getpgrp() with  no  arguments  conform  to
97       POSIX.1-2001.
98
99       POSIX.1-2001 also specifies getpgid() and the version of setpgrp() that
100       takes no arguments.  (POSIX.1-2008 marks this  setpgrp()  specification
101       as obsolete.)
102
103       The version of getpgrp() with one argument and the version of setpgrp()
104       that takes two arguments derive from 4.2BSD, and are not  specified  by
105       POSIX.1.
106

NOTES

108       A  child  created  via  fork(2) inherits its parent's process group ID.
109       The PGID is preserved across an execve(2).
110
111       Each process group is a member of a session and each process is a  mem‐
112       ber of the session of which its process group is a member.
113
114       A  session can have a controlling terminal.  At any time, one (and only
115       one) of the process groups in the session can be the foreground process
116       group  for  the terminal; the remaining process groups are in the back‐
117       ground.  If a signal is generated from the terminal (e.g.,  typing  the
118       interrupt  key  to  generate  SIGINT), that signal is sent to the fore‐
119       ground process group.  (See termios(3) for a description of the charac‐
120       ters  that  generate  signals.)   Only the foreground process group may
121       read(2) from the terminal; if  a  background  process  group  tries  to
122       read(2)  from  the  terminal,  then the group is sent a SIGTTIN signal,
123       which suspends it.  The tcgetpgrp(3)  and  tcsetpgrp(3)  functions  are
124       used  to get/set the foreground process group of the controlling termi‐
125       nal.
126
127       The setpgid() and getpgrp() calls are used by programs such as  bash(1)
128       to create process groups in order to implement shell job control.
129
130       If  a  session has a controlling terminal, and the CLOCAL flag for that
131       terminal is not set, and a terminal hangup  occurs,  then  the  session
132       leader  is  sent  a SIGHUP.  If the session leader exits, then a SIGHUP
133       signal will also be sent to each  process  in  the  foreground  process
134       group of the controlling terminal.
135
136       If  the  exit of the process causes a process group to become orphaned,
137       and if any member of the newly orphaned process group is stopped,  then
138       a  SIGHUP  signal  followed  by  a  SIGCONT signal will be sent to each
139       process in the newly orphaned process group.  An orphaned process group
140       is  one  in which the parent of every member of process group is either
141       itself also a member of the process group or is a member of  a  process
142       group in a different session (see also credentials(7)).
143

SEE ALSO

145       getuid(2),  setsid(2),  tcgetpgrp(3), tcsetpgrp(3), termios(3), creden‐
146       tials(7)
147

COLOPHON

149       This page is part of release 3.53 of the Linux  man-pages  project.   A
150       description  of  the project, and information about reporting bugs, can
151       be found at http://www.kernel.org/doc/man-pages/.
152
153
154
155Linux                             2013-07-31                        SETPGID(2)
Impressum