1GETPID(2) Linux Programmer's Manual GETPID(2)
2
3
4
6 getpid, getppid - get process identification
7
9 #include <unistd.h>
10
11 pid_t getpid(void);
12 pid_t getppid(void);
13
15 getpid() returns the process ID (PID) of the calling process. (This is
16 often used by routines that generate unique temporary filenames.)
17
18 getppid() returns the process ID of the parent of the calling process.
19 This will be either the ID of the process that created this process us‐
20 ing fork(), or, if that process has already terminated, the ID of the
21 process to which this process has been reparented (either init(1) or a
22 "subreaper" process defined via the prctl(2) PR_SET_CHILD_SUBREAPER op‐
23 eration).
24
26 These functions are always successful.
27
29 POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
30
32 If the caller's parent is in a different PID namespace (see pid_name‐
33 spaces(7)), getppid() returns 0.
34
35 From a kernel perspective, the PID (which is shared by all of the
36 threads in a multithreaded process) is sometimes also known as the
37 thread group ID (TGID). This contrasts with the kernel thread ID
38 (TID), which is unique for each thread. For further details, see get‐
39 tid(2) and the discussion of the CLONE_THREAD flag in clone(2).
40
41 C library/kernel differences
42 From glibc version 2.3.4 up to and including version 2.24, the glibc
43 wrapper function for getpid() cached PIDs, with the goal of avoiding
44 additional system calls when a process calls getpid() repeatedly. Nor‐
45 mally this caching was invisible, but its correct operation relied on
46 support in the wrapper functions for fork(2), vfork(2), and clone(2):
47 if an application bypassed the glibc wrappers for these system calls by
48 using syscall(2), then a call to getpid() in the child would return the
49 wrong value (to be precise: it would return the PID of the parent
50 process). In addition, there were cases where getpid() could return
51 the wrong value even when invoking clone(2) via the glibc wrapper func‐
52 tion. (For a discussion of one such case, see BUGS in clone(2).) Fur‐
53 thermore, the complexity of the caching code had been the source of a
54 few bugs within glibc over the years.
55
56 Because of the aforementioned problems, since glibc version 2.25, the
57 PID cache is removed: calls to getpid() always invoke the actual system
58 call, rather than returning a cached value.
59
60 On Alpha, instead of a pair of getpid() and getppid() system calls, a
61 single getxpid() system call is provided, which returns a pair of PID
62 and parent PID. The glibc getpid() and getppid() wrapper functions
63 transparently deal with this. See syscall(2) for details regarding
64 register mapping.
65
67 clone(2), fork(2), gettid(2), kill(2), exec(3), mkstemp(3), tempnam(3),
68 tmpfile(3), tmpnam(3), credentials(7), pid_namespaces(7)
69
71 This page is part of release 5.13 of the Linux man-pages project. A
72 description of the project, information about reporting bugs, and the
73 latest version of this page, can be found at
74 https://www.kernel.org/doc/man-pages/.
75
76
77
78Linux 2021-03-22 GETPID(2)