tcgetpgrp(3)

๐Ÿ’… tcgetpgrp โ€” bestie edition (it's page 3 of the manual, section 3, iykyk)

okay so real talk, this is tcgetpgrp(3), living in the Library Functions Manual section of your friendly neighborhood man pages. two little functions about who's the main character on your terminal, and it's genuinely iconic. let's get into it ๐ŸŽ€


โœจ NAME โœจ

tcgetpgrp, tcsetpgrp โ€” get and set terminal foreground process group

like. that's the whole job description. get it, set it. slay.


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <unistd.h>

pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgrp);

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so first up, tcgetpgrp(). she returns the process group ID of the foreground process group on the terminal associated to fd. and bestie, fd must be the controlling terminal of the calling process. no controlling terminal, no vibes. periodt.

then we have tcsetpgrp(), the setter of the duo. she makes the process group with process group ID pgrp the foreground process group on the terminal associated to fd. requirements, and I'm not being dramatic, these are the requirements:

  • fd must be the controlling terminal of the calling process,
  • that terminal must still be associated with its session,
  • and pgrp must be a (nonempty) process group belonging to the same session as the calling process.

and then the little spicy bit ๐Ÿ’€ โ€” if tcsetpgrp() is called by a member of a background process group in its session, and the calling process is not blocking or ignoring SIGTTOU, then a SIGTTOU signal gets sent to all members of this background process group. so like, know that before you go poking around. not a drill.


๐Ÿซถ RETURN VALUE ๐Ÿซถ

so when fd refers to the controlling terminal of the calling process, tcgetpgrp() will return the foreground process group ID of that terminal if there is one, and otherwise it returns some value larger than 1 that is not presently a process group ID. yes, genuinely. that's what it does. that's not a typo, that's just the API being quirky.

and when fd does not refer to the controlling terminal of the calling process? -1 is returned, and errno is set to indicate the error. rude but fair.

for tcsetpgrp(): when successful, she returns 0. otherwise -1, and errno is set to indicate the error. standard queen behavior.


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • EBADF โ€” fd is not a valid file descriptor. bestie that's not a file ๐Ÿ˜ญ
  • EINVAL โ€” pgrp has an unsupported value.
  • ENOTTY โ€” the calling process does not have a controlling terminal, or it has one but it is not described by fd, or, for tcsetpgrp(), this controlling terminal is no longer associated with the session of the calling process.
  • EPERM โ€” pgrp has a supported value, but is not the process group ID of a process in the same session as the calling process. valid but not your session. rejected ๐Ÿ’…

๐Ÿ”ฅ ATTRIBUTES ๐Ÿ”ฅ

for an explanation of the terms used in this section, go see attributes(7). the receipts:

  • Interface: tcgetpgrp(), tcsetpgrp() โ€” Attribute: Thread safety โ€” Value: MT-Safe

so yeah. MT-Safe. she's safe to share with the group chat. ๐ŸŒˆ


โญ VERSIONS โญ

these functions are implemented via the TIOCGPGRP and TIOCSPGRP ioctls. that's the machinery under the hood, no cap.


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2008. she's certified, she's official, she's standards-compliant.


๐ŸŒธ HISTORY ๐ŸŒธ

POSIX.1-2001.

the ioctls appeared in 4.2BSD. the functions are POSIX inventions. so the vibes are old-school BSD, but the actual functions got invented by POSIX. a true glow-up origin story.


๐Ÿงš SEE ALSO ๐Ÿงš


okay bye bestie, go forth and manage your foreground process groups responsibly โ€” this has been Linux man-pages version 6.20, dated 2026-02-08, and honestly? she ate. 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: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms