setpgid(2)

๐Ÿซถ setpgid โ€” set/get process group ๐Ÿซถ

okay so bestie, this is a page from the System Calls Manual, section 2 (the syscall section, very main character energy). It's literally about how processes group up together. Grab a snack, we're going in ๐Ÿ’–


๐Ÿ“š LIBRARY

Standard C library (libc, -lc). The classic. The blueprint. ๐Ÿ‘‘


โœจ SYNOPSIS โœจ

#include <unistd.h>

int setpgid(pid_t pid, pid_t pgid);
pid_t getpgid(pid_t pid);

pid_t getpgrp(void);                            /* POSIX.1 version */
[[deprecated]] pid_t getpgrp(pid_t pid);        /* BSD version */

int setpgrp(void);                              /* System V version */
[[deprecated]] int setpgrp(pid_t pid, pid_t pgid);  /* BSD version */

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

getpgid():
    _XOPEN_SOURCE >= 500
        || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L

setpgrp() (POSIX.1):
    _XOPEN_SOURCE >= 500
        || /* Since glibc 2.19: */ _DEFAULT_SOURCE
        || /* glibc <= 2.19: */ _SVID_SOURCE

setpgrp() (BSD), getpgrp() (BSD):
    [These are available only before glibc 2.19]
    _BSD_SOURCE &&
        ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE
            || _GNU_SOURCE || _SVID_SOURCE)
yes, the feature test macros are giving โœจbureaucracyโœจ but you HAVE to define them before you include anything, okay? iykyk.

๐Ÿ’– DESCRIPTION ๐Ÿ’–

So like, all of these interfaces are available on Linux, and they're all about getting and setting the process group ID (PGID) of a process. But listen โ€” the preferred, POSIX.1-specified ways to do the thing are:

  • getpgrp(void) โ€” for retrieving the calling process's PGID
  • setpgid() โ€” for setting a process's PGID

setpgid() sets the PGID of the process specified by pid to pgid. And here's the little detail checklist, bestie:

  • If pid is zero, then the process ID of the calling process is used. ๐Ÿซถ
  • If pgid is zero, then the PGID of the process specified by pid is made the same as its process ID.
  • If setpgid() is used to move a process from one process group to another (as is done by some shells when creating pipelines), both process groups must be part of the same session โ€” see setsid(2) and credentials(7). In this case, the pgid specifies an existing process group to be joined, and the session ID of that group must match the session ID of the joining process. no cross-session drama allowed, it's giving strictly-enforced-bubble ๐Ÿ’…

The POSIX.1 version of getpgrp(), which takes no arguments, returns the PGID of the calling process.

getpgid() returns the PGID of the process specified by pid. If pid is zero, the process ID of the calling process is used. (Retrieving the PGID of a process other than the caller is rarely necessary, and the POSIX.1 getpgrp() is preferred for that task โ€” like, why are you being nosy about other processes' groups, just use the no-arg one bestie.)

The System V-style setpgrp(), which takes no arguments, is equivalent to setpgid(0, 0).

The BSD-specific setpgrp() call, which takes arguments pid and pgid, is a wrapper function that calls

setpgid(pid, pgid)

Since glibc 2.19, the BSD-specific setpgrp() function is no longer exposed by <unistd.h>; calls should be replaced with the setpgid() call shown above. it's retired, it's retired ๐Ÿ’€

The BSD-specific getpgrp() call, which takes a single pid argument, is a wrapper function that calls

getpgid(pid)

Since glibc 2.19, the BSD-specific getpgrp() function is no longer exposed by <unistd.h>; calls should be replaced with calls to the POSIX.1 getpgrp() which takes no arguments (if the intent is to obtain the caller's PGID), or with the getpgid() call shown above. don't be texting the old number, it doesn't answer anymore ๐Ÿ™ˆ


๐ŸŒŸ RETURN VALUE ๐ŸŒŸ

On success, setpgid() and setpgrp() return zero. Slay. โœจ

On error, -1 is returned, and errno is set to indicate the error. ๐Ÿ˜ญ

The POSIX.1 getpgrp() always returns the PGID of the caller. Always. Like clockwork. Iconic behavior honestly.

getpgid(), and the BSD-specific getpgrp() return a process group on success. On error, -1 is returned, and errno is set to indicate the error.


๐Ÿ’€ ERRORS ๐Ÿ’€

  • EACCES โ€” An attempt was made to change the process group ID of one of the children of the calling process and the child had already performed an execve(2) (setpgid(), setpgrp()). like no, that kid has moved on with their life, you can't just move them back into your group.
  • EINVAL โ€” pgid is less than 0 (setpgid(), setpgrp()). negative group id? absolutely not. a crime.
  • EPERM โ€” An attempt was made to move a process into a process group in a different session, or to change the process group ID of one of the children of the calling process and the child was in a different session, or to change the process group ID of a session leader (setpgid(), setpgrp()). boundaries, bestie. the session leader cannot be yanked around.
  • EPERM โ€” The target process group does not exist. (setpgid(), setpgrp()). you can't join a group chat that isn't real ๐Ÿ’…
  • ESRCH โ€” For getpgid(): pid does not match any process. For setpgid(): pid is not the calling process and not a child of the calling process. who?? never heard of her.

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

  • getpgid()
  • setpgid()
  • getpgrp() (no args)
these are POSIX.1-2024. certified, official, canon. ๐Ÿ’œ
  • setpgrp()
  • getpgrp() (1 arg)
None. not standardized. free agents. no rules, no label. ๐Ÿฆ‹

๐ŸŒธ HISTORY ๐ŸŒธ

  • getpgid()
  • setpgid()
  • getpgrp() (no args)
POSIX.1-2001. the origin story. ๐ŸŽ€
  • setpgrp() (no args)
POSIX.1-2001. Obsoleted in POSIX.1-2008. Removed in POSIX.1-2024. it had a whole arc. gone but not forgotten ๐Ÿ˜ญ
  • setpgrp() (2 args)
  • getpgrp() (1 arg)
4.2BSD. the BSD girls, real ones from back in the day. ๐Ÿ’…

๐Ÿงš NOTES ๐Ÿงš

A child created via fork(2) inherits its parent's process group ID. The PGID is preserved across an execve(2). so even when you exec into a whole new program, your group comes with you. consistent queen. ๐ŸŒˆ

Each process group is a member of a session and each process is a member of the session of which its process group is a member. (See credentials(7).) it's groups within groups within groups, very nesting-doll, very serene. ๐Ÿช†

A session can have a controlling terminal. At any time, one (and only one) of the process groups in the session can be the foreground process group for the terminal; the remaining process groups are in the background. If a signal is generated from the terminal (e.g., typing the interrupt key to generate SIGINT), that signal is sent to the foreground process group. (See termios(3type) for a description of the characters that generate signals.) Only the foreground process group may read(2) from the terminal; if a background process group tries to read(2) from the terminal, then the group is sent a SIGTTIN signal, which suspends it. The tcgetpgrp(3) and tcsetpgrp(3) functions are used to get/set the foreground process group of the controlling terminal. one group gets the aux, the rest are on mute. that's just how the terminal works ๐Ÿ’…

The setpgid() and getpgrp() calls are used by programs such as bash(1) to create process groups in order to implement shell job control. so like, every time you hit Ctrl+Z and suspend something, this is the machinery. you're welcome ๐Ÿซถ

If the termination of a process causes a process group to become orphaned, and if any member of the newly orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process in the newly orphaned process group. An orphaned process group is one in which the parent of every member of process group is either itself also a member of the process group or is a member of a process group in a different session (see also credentials(7)). tragic backstory but make it POSIX. ๐Ÿ’”


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ


and that's the whole page bestie!! this one came out of Linux man-pages 6.20, dated 2026-07-02 โ€” go forth and group your processes responsibly, mwah ๐Ÿ’‹๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms