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