SETPGID(3POSIX)
π SETPGID β bestie, let's talk process groups π
okay so real talk: this page is part of the POSIX Programmer's Manual, and I need you to know something before we go any further π
The Linux implementation of this interface may differ β so pls consult the corresponding Linux manual page for the deets on Linux behavior. Also possible: the interface just isn't implemented on Linux at all. π
β¨ NAME β¨
setpgid β set process group ID for job control π
π¦ SYNOPSIS π¦
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
πΈ DESCRIPTION πΈ
So setpgid() is the function that shall either join an existing process group or create a brand new process group β and it does all this within the session of the calling process. It's giving main character energy in the session, fr fr.
The process group ID of a session leader shall not change. No cap. Session leaders stay put, that's just the rules. π
Upon successful completion, the process group ID of the process with a process ID that matches pid shall be set to pgid. Slay.
And here's the special-case tea π:
- If
pidis0, the process ID of the calling process shall be used. - If
pgidis0, the process ID of the indicated process shall be used.
β RETURN VALUE β
- On success:
setpgid()shall return0. Periodt. π - Otherwise:
-1shall be returned, anderrnoshall be set to indicate the error. π
π₯ 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. (Sneaky child, you already moved on.)EINVALβ The value of thepgidargument is less than0, or it's not a value supported by the implementation. π«EPERMβ The process indicated by thepidargument is a session leader. π (Main character can't be recast.)EPERMβ The value of thepidargument matches the process ID of a child process of the calling process, and that child process is not in the same session as the calling process.EPERMβ The value of thepgidargument is valid, but it 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.ESRCHβ The value of thepidargument does not match the process ID of the calling process, or of a child process of the calling process. Like, who even is that? π
The following sections are informative.
π EXAMPLES π
None. Yes bestie, literally none. Nothing to see here. π«Ά
π APPLICATION USAGE π
None. Also empty. We're just vibing. β¨
π RATIONALE π
Okay so why does this exist:
The setpgid() function shall group processes together for the purpose of signaling, placement in foreground or background, and other job control actions. That's the whole vibe check. π
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, yeah, too much freedom, we fixed it. π
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.
π the timing constraints era
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. π€ iconic teamwork honestly.
π€ why the EACCES exists
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. Boundaries! π§
π§ one non-obvious use
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.) So it's like... a little glow-up back to your roots. π¦
π FUTURE DIRECTIONS π
None. The future is just... more of the same. π
π 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 .
okay that's the whole page bestie, mwah π this one came to you courtesy of IEEE/The Open Group, dated 2017 β go be a session leader, but like, legally β¨ππ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms