readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man2 / getpid.2
blobfa252b7641ecf6825d5d31e86376242f50c17488
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH GETPID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 getpid, getppid \- get process identification
28 .SH SYNOPSIS
29 .nf
30 .B #include <unistd.h>
31 .PP
32 .B pid_t getpid(void);
33 .B pid_t getppid(void);
34 .fi
35 .SH DESCRIPTION
36 .BR getpid ()
37 returns the process ID (PID) of the calling process.
38 (This is often used by
39 routines that generate unique temporary filenames.)
40 .PP
41 .BR getppid ()
42 returns the process ID of the parent of the calling process.
43 This will be either the ID of the process that created this process using
44 .BR fork (),
45 or, if that process has already terminated,
46 the ID of the process to which this process has been reparented (either
47 .BR init (1)
48 or a "subreaper" process defined via the
49 .BR prctl (2)
50 .BR PR_SET_CHILD_SUBREAPER
51 operation).
52 .SH ERRORS
53 These functions are always successful.
54 .SH CONFORMING TO
55 POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
56 .SH NOTES
57 If the caller's parent is in a different PID namespace (see
58 .BR pid_namespaces (7)),
59 .BR getppid ()
60 returns 0.
61 .PP
62 From a kernel perspective,
63 the PID (which is shared by all of the threads in a multithreaded process)
64 is sometimes also known as the thread group ID (TGID).
65 This contrasts with the kernel thread ID (TID),
66 which is unique for each thread.
67 For further details, see
68 .BR gettid (2)
69 and the discussion of the
70 .BR CLONE_THREAD
71 flag in
72 .BR clone (2).
73 .\"
74 .SS C library/kernel differences
75 From glibc version 2.3.4 up to and including version 2.24,
76 the glibc wrapper function for
77 .BR getpid ()
78 cached PIDs,
79 with the goal of avoiding additional system calls when a process calls
80 .BR getpid ()
81 repeatedly.
82 Normally this caching was invisible,
83 but its correct operation relied on support in the wrapper functions for
84 .BR fork (2),
85 .BR vfork (2),
86 and
87 .BR clone (2):
88 if an application bypassed the glibc wrappers for these system calls by using
89 .BR syscall (2),
90 then a call to
91 .BR getpid ()
92 in the child would return the wrong value
93 (to be precise: it would return the PID of the parent process).
94 .\" The following program demonstrates this "feature":
95 .\"
96 .\" #define _GNU_SOURCE
97 .\" #include <sys/syscall.h>
98 .\" #include <sys/wait.h>
99 .\" #include <stdint.h>
100 .\" #include <stdio.h>
101 .\" #include <stdlib.h>
102 .\" #include <unistd.h>
104 .\" int
105 .\" main(int argc, char *argv[])
106 .\" {
107 .\"    /* The following statement fills the getpid() cache */
109 .\"    printf("parent PID = %ld\n", (intmax_t) getpid());
111 .\"    if (syscall(SYS_fork) == 0) {
112 .\"        if (getpid() != syscall(SYS_getpid))
113 .\"            printf("child getpid() mismatch: getpid()=%jd; "
114 .\"                    "syscall(SYS_getpid)=%ld\n",
115 .\"                    (intmax_t) getpid(), (long) syscall(SYS_getpid));
116 .\"        exit(EXIT_SUCCESS);
117 .\"    }
118 .\"    wait(NULL);
119 .\"}
120 In addition, there were cases where
121 .BR getpid ()
122 could return the wrong value even when invoking
123 .BR clone (2)
124 via the glibc wrapper function.
125 (For a discussion of one such case, see BUGS in
126 .BR clone (2).)
127 Furthermore, the complexity of the caching code had been
128 the source of a few bugs within glibc over the years.
130 Because of the aforementioned problems,
131 since glibc version 2.25, the PID cache is removed:
132 .\" commit c579f48edba88380635ab98cb612030e3ed8691e
133 .\" https://sourceware.org/glibc/wiki/Release/2.25#pid_cache_removal
134 calls to
135 .BR getpid ()
136 always invoke the actual system call, rather than returning a cached value.
137 .\" FIXME .
138 .\" Review progress of https://bugzilla.redhat.com/show_bug.cgi?id=1469757
140 On Alpha, instead of a pair of
141 .BR getpid ()
143 .BR getppid ()
144 system calls, a single
145 .BR getxpid ()
146 system call is provided, which returns a pair of PID and parent PID.
147 The glibc
148 .BR getpid ()
150 .BR getppid ()
151 wrapper functions transparently deal with this.
153 .BR syscall (2)
154 for details regarding register mapping.
155 .SH SEE ALSO
156 .BR clone (2),
157 .BR fork (2),
158 .BR gettid (2),
159 .BR kill (2),
160 .BR exec (3),
161 .BR mkstemp (3),
162 .BR tempnam (3),
163 .BR tmpfile (3),
164 .BR tmpnam (3),
165 .BR credentials (7),
166 .BR pid_namespaces (7)