CINXE.COM
getppid
<html> <head> <title>getppid</title> <META NAME="KEYWORDS" CONTENT="getppid"> </head> <body BGCOLOR="#ffffff" LINK="#0000ff" VLINK="#0000ff" ALINK="#0000ff" TEXT="#000000"> <center> <h1><b>getppid</b></h1></center> <PRE> <STRONG><A HREF="/man2/GETPID">GETPID(2)</A></STRONG> Linux Programmer's Manual <STRONG><A HREF="/man2/GETPID">GETPID(2)</A></STRONG> NAME getpid, getppid - get process identification SYNOPSIS #include <sys/types.h> #include <unistd.h> pid_t getpid(void); pid_t getppid(void); DESCRIPTION getpid() returns the process ID (PID) of the calling process. (This is often used by routines that generate unique temporary filenames.) getppid() returns the process ID of the parent of the calling process. This will be either the ID of the process that created this process us- ing fork(), or, if that process has already terminated, the ID of the process to which this process has been reparented (either <STRONG><A HREF="/man1/init">init(1)</A></STRONG> or a "subreaper" process defined via the <STRONG><A HREF="/man2/prctl">prctl(2)</A></STRONG> PR_SET_CHILD_SUBREAPER op- eration). ERRORS These functions are always successful. CONFORMING TO POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4. NOTES If the caller's parent is in a different PID namespace (see pid_name- <STRONG><A HREF="/man7/spaces">spaces(7)</A></STRONG>), getppid() returns 0. 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 get- <STRONG><A HREF="/man2/tid">tid(2)</A></STRONG> and the discussion of the CLONE_THREAD flag in <STRONG><A HREF="/man2/clone">clone(2)</A></STRONG>. C library/kernel differences From glibc version 2.3.4 up to and including version 2.24, the glibc wrapper function for getpid() cached PIDs, with the goal of avoiding additional system calls when a process calls getpid() repeatedly. Nor- mally this caching was invisible, but its correct operation relied on support in the wrapper functions for <STRONG><A HREF="/man2/fork">fork(2)</A></STRONG>, <STRONG><A HREF="/man2/vfork">vfork(2)</A></STRONG>, and <STRONG><A HREF="/man2/clone">clone(2)</A></STRONG>: if an application bypassed the glibc wrappers for these system calls by using <STRONG><A HREF="/man2/syscall">syscall(2)</A></STRONG>, 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). In addition, there were cases where getpid() could return the wrong value even when invoking <STRONG><A HREF="/man2/clone">clone(2)</A></STRONG> via the glibc wrapper func- tion. (For a discussion of one such case, see BUGS in <STRONG><A HREF="/man2/clone">clone(2)</A></STRONG>.) Fur- thermore, the complexity of the caching code had been the source of a few bugs within glibc over the years. Because of the aforementioned problems, since glibc version 2.25, the PID cache is removed: calls to getpid() always invoke the actual system call, rather than returning a cached value. On Alpha, instead of a pair of getpid() and getppid() system calls, a single getxpid() system call is provided, which returns a pair of PID and parent PID. The glibc getpid() and getppid() wrapper functions transparently deal with this. See <STRONG><A HREF="/man2/syscall">syscall(2)</A></STRONG> for details regarding register mapping. SEE ALSO <STRONG><A HREF="/man2/clone">clone(2)</A></STRONG>, <STRONG><A HREF="/man2/fork">fork(2)</A></STRONG>, <STRONG><A HREF="/man2/gettid">gettid(2)</A></STRONG>, <STRONG><A HREF="/man2/kill">kill(2)</A></STRONG>, <STRONG><A HREF="/man3/exec">exec(3)</A></STRONG>, <STRONG><A HREF="/man3/mkstemp">mkstemp(3)</A></STRONG>, <STRONG><A HREF="/man3/tempnam">tempnam(3)</A></STRONG>, <STRONG><A HREF="/man3/tmpfile">tmpfile(3)</A></STRONG>, <STRONG><A HREF="/man3/tmpnam">tmpnam(3)</A></STRONG>, <STRONG><A HREF="/man7/credentials">credentials(7)</A></STRONG>, <STRONG><A HREF="/man7/pid_namespaces">pid_namespaces(7)</A></STRONG> COLOPHON This page is part of release 5.05 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2019-03-06 <STRONG><A HREF="/man2/GETPID">GETPID(2)</A></STRONG></PRE> <center> <h6>Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2025 <a href="http://www.he.net">Hurricane Electric</a>. All Rights Reserved.</h6></center> </body> </html>