SETPGID(2)

πŸ’– SETPGID β€” ok bestie, let's talk process groups πŸ’–

Hiii welcome to setpgid and setpgrp, living their best lives in section 2 of the System Calls Manual (that's the syscall manual, the one where all the real ones hang out πŸ’…). Two names, one job: setting the process group. Let's get into it ✨


🌸 LIBRARY 🌸

Standard C Library β€” that's libc, link it with -lc. Yes, -lc, exactly like that, no notes 🫢


πŸŽ€ SYNOPSIS πŸŽ€

#include <unistd.h>

int
setpgid(pid_t pid, pid_t pgrp);

int
setpgrp(pid_t pid, pid_t pgrp);

Look at them. Twins. Iconic behavior, honestly πŸ‘‘


✨ DESCRIPTION ✨

Okay so here's the tea πŸ“. The setpgid() system call sets the process group of the process you point at with pid to the process group you point at with pgrp.

Now the little ✨ special cases ✨, and these are important so don't skip:

  • If pid is zero, then the call applies to the current process. It's you. You're the main character πŸ’β™€οΈ
  • If pgrp is zero, then the process id of the process specified by pid is used instead.

And now the rules, because there ARE rules, bestie, and they will vibe check you:

If the affected process is not the invoking process, then a whole checklist kicks in:

  • it must be a child of the invoking process,
  • it must not have performed an exec(3) operation,
  • and both processes must be in the same session.

Also: the requested process group ID must already exist in the session of the caller, or it must be equal to the target process ID. No third option, sorry babe 😭


πŸ”₯ RETURN VALUES πŸ”₯

  • setpgid() returns the value 0 if successful β€” slay βœ…
  • otherwise the value -1 is returned, and the global variable errno is set to indicate the error πŸ’€

🌈 COMPATIBILITY 🌈

The setpgrp() system call is identical to setpgid(), and it's kept around for calling convention compatibility with historical versions of BSD. So it's not extra credit, it's literally the same thing wearing a different outfit 🧚


😭 ERRORS 😭

The setpgid() system call will fail and the process group will not be altered if:

  • [EINVAL] β€” The requested process group ID is not legal. Not legal!! 🚨
  • [ESRCH] β€” The requested process does not exist. Ghosted. Literally πŸ«₯
  • [ESRCH] β€” The target process is not the calling process or a child of the calling process. It's not in your family, so no πŸ’…
  • [EACCES] β€” The requested process is a child of the calling process, but it has performed an exec(3) operation. It grew up and moved out, you can't do this anymore πŸ˜”
  • [EPERM] β€” The target process is a session leader. Crown stays on, no touching πŸ‘‘
  • [EPERM] β€” The requested process group ID is not in the session of the caller, and it is not equal to the process ID of the target process. Wrong session, wrong group, hard no πŸ™ˆ
Heads up bestie: on failure the process group is left completely untouched. Nothing half-happens here, it's all or nothing, periodt.

πŸ’œ SEE ALSO πŸ’œ

  • getpgrp(2) β€” the other half of the duo, go read it too πŸ¦‹

⭐ STANDARDS ⭐

The setpgid() system call is expected to conform to ISO/IEC 9945-1:1990 (β€œPOSIX.1”). Certified. Official. Standards girlie πŸ“œβœ¨


okay that's the whole page, you ate that, go set some process groups responsibly πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«ΆπŸ‘‘πŸŒˆβ­

This one came from the Debian folks, dated February 8, 2004 β€” old money, still relevant, we love a classic πŸ’œ

πŸ“¦ where this page comes from