SETPGID(3P)
π SETPGID(3P) β POSIX Programmer's Manual, bestie edition
okay so real talk: this is a page from the POSIX Programmer's Manual β¨
π«Ά PROLOG
Heads up, bestie π this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux at all. So likeβ¦ check before you commit, fr fr.
π NAME
setpgid β set process group ID for job control. that's it, that's the whole job. iconic behavior honestly.
πΈ SYNOPSIS
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
β¨ DESCRIPTION
So the setpgid() function shall either join an existing process group or create a new process group within the session of the calling process. pick your adventure π¦
The process group ID of a session leader shall not change. nope. not happening. main character stays in their own group π
Upon successful completion, the process group ID of the process with a process ID that matches pid shall be set to pgid. slay.
And as a special case β because there's always a plot twist π β if pid is 0, the process ID of the calling process shall be used. Also, if pgid is 0, the process ID of the indicated process shall be used. cute little shortcut, iykyk.
π RETURN VALUE
Upon successful completion, setpgid() shall return 0 β yes bestie, zero is the good one here π β otherwise, -1 shall be returned and errno shall be set to indicate the error. not the vibe, but okay.
π ERRORS
The setpgid() function shall fail if:
EACCESβ the value of thepidargument matches the process ID of a child process of the calling process, and that child process has successfully executed one of theexecfunctions. so like, once they've done the exec glow-up, you can't retroactively put them in a group. that's a no πEINVALβ the value of thepgidargument is less than0, or is not a value supported by the implementation. negative group IDs are not it.EPERMβ the process indicated by thepidargument is a session leader. session leaders don't move groups, periodt πEPERMβ the value of thepidargument matches the process ID of a child process of the calling process, and the child process is not in the same session as the calling process. different session, different energy, can't reach π§EPERMβ the value of thepgidargument is valid but does not match the process ID of the process indicated by thepidargument, and there is no process with a process group ID that matches the value of thepgidargument in the same session as the calling process. basically: you're trying to join a group that doesn't exist in your session. ghosted πESRCHβ the value of thepidargument does not match the process ID of the calling process or of a child process of the calling process. wrong person, wrong number, nobody home π΅
The following sections are informative. (so like, they're the fun bonus content, not the binding rules β¨)
π EXAMPLES
None. literally nothing. the void. π
π¦ APPLICATION USAGE
None. also nothing. we're just vibing. π
π₯ RATIONALE
okay THIS is the good part, sit down bestie π
The setpgid() function shall group processes together for the purpose of signaling, placement in foreground or background, and other job control actions.
The setpgid() function is similar to the setpgrp() function of 4.2 BSD, except that 4.2 BSD allowed the specified new process group to assume any value. That presents certain security problems and is more flexible than necessary to support job control. so like, 4.2 BSD was doing too much π
To provide tighter security, setpgid() only allows the calling process to:
- join a process group already in use inside its session, or
- create a new process group whose process group ID was equal to its process ID.
That's the boundary. respect it π
When a job control shell spawns a new job, the processes in the job must be placed into a new process group via setpgid(). There are two timing constraints involved in this action:
- The new process must be placed in the new process group before the appropriate program is launched via one of the
execfunctions. - The new process must be placed in the new process group before the shell can correctly send signals to the new process group.
To address these constraints, the following actions are performed. The new processes call setpgid() to alter their own process groups after fork() but before exec. This satisfies the first constraint. Under 4.3 BSD, the second constraint is satisfied by the synchronization property of vfork(); that is, the shell is suspended until the child has completed the exec, thus ensuring that the child has completed the setpgid(). A new version of fork() with this same synchronization property was considered, but it was decided instead to merely allow the parent shell process to adjust the process group of its child processes via setpgid(). Both timing constraints are now satisfied by having both the parent shell and the child attempt to adjust the process group of the child process; it does not matter which succeeds first. teamwork makes the dream work π«Ά
Since it would be confusing to an application to have its process group change after it began executing (that is, after exec), and because the child process would already have adjusted its process group before this, the [EACCES] error was added to disallow this. protection era β¨
One non-obvious use of setpgid() is to allow a job control shell to return itself to its original process group (the one in effect when the job control shell was executed). A job control shell does this before returning control back to its parent when it is terminating or suspending itself, as a way of restoring its job control state back to what its parent would expect. (Note that the original process group of the job control shell typically matches the process group of its parent, but this is not necessarily always the case.) no cap, this is a real trick π
π FUTURE DIRECTIONS
None. the future is empty. manifesting nothing. β
π SEE ALSO
execgetpgrp()setsid()tcsetpgrp()
The Base Definitions volume of POSIX.1β2017, <sys_types.h>, <unistd.h> π
π COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π₯° this absolute banger of a manpage was brought to you by IEEE/The Open Group, dated 2017 β mwah, thank you for your service, take care of yourselves, drink water, and go set some process groups π β¨π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms