Debian Codegraph @

upstream/debian-unstable/man2/getpid.2

raw
1 .\" Copyright 1993, Rickard E. Faith <[email protected]>
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH getpid 2 2026-02-08 "Linux man-pages 6.18"
7 .SH NAME
8 getpid, getppid \- get process identification
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <unistd.h>
15 .P
16 .B pid_t getpid(void);
17 .B pid_t getppid(void);
18 .fi
19 .SH DESCRIPTION
20 .BR getpid ()
21 returns the process ID (PID) of the calling process.
22 (This is often used by
23 routines that generate unique temporary filenames.)
24 .P
25 .BR getppid ()
26 returns the process ID of the parent of the calling process.
27 This will be either the ID of the process that created this process using
28 .BR fork (),
29 or, if that process has already terminated,
30 the ID of the process to which this process has been reparented (either
31 .BR init (1)
32 or a "subreaper" process defined via the
33 .BR prctl (2)
34 .B PR_SET_CHILD_SUBREAPER
35 operation).
36 .SH ERRORS
37 These functions are always successful.
38 .SH VERSIONS
39 On Alpha, instead of a pair of
40 .BR getpid ()
41 and
42 .BR getppid ()
43 system calls, a single
44 .BR getxpid ()
45 system call is provided, which returns a pair of PID and parent PID.
46 The glibc
47 .BR getpid ()
48 and
49 .BR getppid ()
50 wrapper functions transparently deal with this.
51 See
52 .BR syscall (2)
53 for details regarding register mapping.
54 .SH STANDARDS
55 POSIX.1-2024.
56 .SH HISTORY
57 4.3BSD,
58 SVr4,
59 POSIX.1-1988.
60 .SS C library/kernel differences
61 From glibc 2.3.4 up to and including glibc 2.24,
62 the glibc wrapper function for
63 .BR getpid ()
64 cached PIDs,
65 with the goal of avoiding additional system calls when a process calls
66 .BR getpid ()
67 repeatedly.
68 Normally this caching was invisible,
69 but its correct operation relied on support in the wrapper functions for
70 .BR fork (2),
71 .BR vfork (2),
72 and
73 .BR clone (2):
74 if an application bypassed the glibc wrappers for these system calls by using
75 .BR syscall (2),
76 then a call to
77 .BR getpid ()
78 in the child would return the wrong value
79 (to be precise: it would return the PID of the parent process).
80 .\" The following program demonstrates this "feature":
81 .\"
82 .\" #define _GNU_SOURCE
83 .\" #include <sys/syscall.h>
84 .\" #include <sys/wait.h>
85 .\" #include <stdint.h>
86 .\" #include <stdio.h>
87 .\" #include <stdlib.h>
88 .\" #include <unistd.h>
89 .\"
90 .\" int
91 .\" main(int argc, char *argv[])
92 .\" {
93 .\" /* The following statement fills the getpid() cache */
94 .\"
95 .\" printf("parent PID = %ld\n", (intmax_t) getpid());
96 .\"
97 .\" if (syscall(SYS_fork) == 0) {
98 .\" if (getpid() != syscall(SYS_getpid))
99 .\" printf("child getpid() mismatch: getpid()=%jd; "
100 .\" "syscall(SYS_getpid)=%ld\n",
101 .\" (intmax_t) getpid(), (long) syscall(SYS_getpid));
102 .\" exit(EXIT_SUCCESS);
103 .\" }
104 .\" wait(NULL);
105 .\"}
106 In addition, there were cases where
107 .BR getpid ()
108 could return the wrong value even when invoking
109 .BR clone (2)
110 via the glibc wrapper function.
111 (For a discussion of one such case, see BUGS in
112 .BR clone (2).)
113 Furthermore, the complexity of the caching code had been
114 the source of a few bugs within glibc over the years.
115 .P
116 Because of the aforementioned problems,
117 since glibc 2.25, the PID cache is removed:
118 .\" commit c579f48edba88380635ab98cb612030e3ed8691e
119 .\" https://sourceware.org/glibc/wiki/Release/2.25#pid_cache_removal
120 calls to
121 .BR getpid ()
122 always invoke the actual system call, rather than returning a cached value.
123 .\" FIXME .
124 .\" Review progress of https://bugzilla.redhat.com/show_bug.cgi?id=1469757
125 .SH NOTES
126 If the caller's parent is in a different PID namespace (see
127 .BR pid_namespaces (7)),
128 .BR getppid ()
129 returns 0.
130 .P
131 From a kernel perspective,
132 the PID (which is shared by all of the threads in a multithreaded process)
133 is sometimes also known as the thread group ID (TGID).
134 This contrasts with the kernel thread ID (TID),
135 which is unique for each thread.
136 For further details, see
137 .BR gettid (2)
138 and the discussion of the
139 .B CLONE_THREAD
140 flag in
141 .BR clone (2).
142 .SH SEE ALSO
143 .BR clone (2),
144 .BR fork (2),
145 .BR gettid (2),
146 .BR kill (2),
147 .BR exec (3),
148 .BR mkstemp (3),
149 .BR tempnam (3),
150 .BR tmpfile (3),
151 .BR tmpnam (3),
152 .BR credentials (7),
153 .BR pid_namespaces (7)