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:
fdmust be the controlling terminal of the calling process,- that terminal must still be associated with its session,
- and
pgrpmust 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โfdis not a valid file descriptor. bestie that's not a file ๐ญEINVALโpgrphas an unsupported value.ENOTTYโ the calling process does not have a controlling terminal, or it has one but it is not described byfd, or, fortcsetpgrp(), this controlling terminal is no longer associated with the session of the calling process.EPERMโpgrphas 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
- Copyright 2002, Andries E. Brouwer <aeb@cwi.nl>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms