getpid(2)

๐Ÿ’– getpid(2) โ€” the system calls manual, but make it cute ๐Ÿ’–

okay bestie, welcome to the System Calls Manual page for getpid and getppid, aka the two tiny little functions that tell a process who it is and who made it. it's giving "find your birth certificate but for your own PID" ๐Ÿผโœจ


๐ŸŒธ NAME ๐ŸŒธ

getpid, getppid โ€” get process identification ๐Ÿ†”


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). the classic. the icon. the one your linker already links against, no cap ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

that's it. two lines. no arguments. no drama. literally the least demanding functions in the whole syscall universe ๐Ÿซถ


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so, getpid() returns the process ID (PID) of the calling process. that's it, that's the whole thing. and just so you know, this is often used by routines that generate unique temporary filenames โ€” so if you've ever written a temp file helper, this is basically your backbone, bestie. iykyk ๐ŸŽ€

and then getppid() returns the process ID of the parent of the calling process. and here's the tea: the "parent" here is either the ID of the process that created this process using fork(), or โ€” and this is the part where things get a lil spicy ๐ŸŒถ๏ธ โ€” if that process has already terminated, it returns the ID of the process to which this process has been reparented. and that reparenting target is either init(1) or a "subreaper" process defined via the prctl(2) PR_SET_CHILD_SUBREAPER operation. so like, your parent can change and that's valid, we support reparenting in this house ๐Ÿ‘‘โœจ


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

these functions are always successful. no errors. not even one. not even a little one. they simply cannot fail. the way that a syscall can just be perfect and unproblematic... obsessed ๐Ÿ’€๐Ÿ’–


๐ŸŒˆ VERSIONS ๐ŸŒˆ

so on Alpha, instead of having a whole pair of getpid() and getppid() system calls, there's a single getxpid() system call that returns a pair of PID and parent PID. it's giving two-for-one deal ๐ŸŽ‰

and the glibc getpid() and getppid() wrapper functions transparently deal with this โ€” so you never even notice. if you wanna know about register mapping details, go peek at syscall(2). yes, register mapping. yes it's deeply nerdy. yes we love it ๐Ÿงš


๐Ÿ“œ STANDARDS ๐Ÿ“œ

POSIX.1-2024. certified. ratified. slay โญ


๐Ÿ’œ HISTORY ๐Ÿ’œ

4.3BSD, SVr4, POSIX.1-1988. ancient. legendary. still standing. periodt ๐Ÿ’…

๐Ÿง  C library/kernel differences ๐Ÿง 

okay so buckle up bestie, this is the ancestral drama section and it is juicy ๐Ÿ“

from glibc 2.3.4 up to and including glibc 2.24, the glibc wrapper function for getpid() cached PIDs, with the goal of avoiding additional system calls when a process calls getpid() repeatedly. makes sense, right? efficiency girlie ๐Ÿ’ซ

normally this caching was invisible. normally. but its correct operation relied on support in the wrapper functions for fork(2), vfork(2), and clone(2). so if an application bypassed the glibc wrappers for these system calls by using syscall(2), then a call to getpid() in the child would return the wrong value โ€” to be precise: it would return the PID of the parent process ๐Ÿ˜ญ

and it gets worse. there were cases where getpid() could return the wrong value even when invoking clone(2) via the glibc wrapper function. (for a discussion of one such case, see BUGS in clone(2).) furthermore, the complexity of the caching code had been the source of a few bugs within glibc over the years. so like... the cache was lowkey a menace ๐Ÿ’€

and because of the aforementioned problems, since glibc 2.25, the PID cache is removed: calls to getpid() always invoke the actual system call, rather than returning a cached value. the cache got cancelled. we do not miss her. main character behavior honestly ๐Ÿ‘‘

bestie please internalise this one: if you're on old glibc (< 2.25) and you're raw-dogging syscall(2) for fork/vfork/clone, your child's getpid() might be lying to your face. that's not a vibe. that's a bug ๐Ÿ™ˆ

๐Ÿ“ NOTES ๐Ÿ“

if the caller's parent is in a different PID namespace (see pid_namespaces(7)), getppid() returns 0. zero. nada. no parent for you in this namespace, bestie, go touch grass ๐Ÿงš

also, from a kernel perspective, the PID (which is shared by all of the threads in a multithreaded process) is sometimes also known as the thread group ID (TGID). this contrasts with the kernel thread ID (TID), which is unique for each thread. for further details, see gettid(2) and the discussion of the CLONE_THREAD flag in clone(2). so like, PID = the whole group chat, TID = the individual member. TGID is literally the group chat name ๐ŸŒธ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

clone(2), fork(2), gettid(2), kill(2), exec(3), mkstemp(3), tempnam(3), tmpfile(3), tmpnam(3), credentials(7), pid_namespaces(7)

go read them. not me being a bad influence and giving you eleven more manpages to read ๐Ÿ“šโœจ


and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-02-08 ๐ŸŽ€ thanks for reading all the way to the bottom, you're literally the best, go get your PID and feel something ๐Ÿซถ๐Ÿ’–

๐Ÿ“ฆ 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