]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getpid.2
getpid.2: Note that PID caching is removed as of glibc 2.25
[thirdparty/man-pages.git] / man2 / getpid.2
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 2017-05-03 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 getpid, getppid \- get process identification
28 .SH SYNOPSIS
29 .B #include <sys/types.h>
30 .br
31 .B #include <unistd.h>
32 .sp
33 .B pid_t getpid(void);
34 .br
35 .B pid_t getppid(void);
36 .SH DESCRIPTION
37 .BR getpid ()
38 returns the process ID of the calling process.
39 (This is often used by
40 routines that generate unique temporary filenames.)
41
42 .BR getppid ()
43 returns the process ID of the parent of the calling process.
44 This will be either the ID of the process that created this process using
45 .BR fork (),
46 or, if that process has already terminated,
47 the ID of the process to which this process has been reparented (either
48 .BR init (1)
49 or a "subreaper" process defined via the
50 .BR prctl (2)
51 .BR PR_SET_CHILD_SUBREAPER
52 operation).
53 .SH ERRORS
54 These functions are always successful.
55 .SH CONFORMING TO
56 POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
57 .SH NOTES
58 If the caller's parent is in a different PID namespace (see
59 .BR pid_namespaces (7)),
60 .BR getppid ()
61 returns 0.
62 .\"
63 .SS C library/kernel differences
64 Since glibc version 2.3.4,
65 the glibc wrapper function for
66 .BR getpid ()
67 caches PIDs,
68 so as to avoid additional system calls when a process calls
69 .BR getpid ()
70 repeatedly.
71 Normally this caching is invisible,
72 but its correct operation relies on support in the wrapper functions for
73 .BR fork (2),
74 .BR vfork (2),
75 and
76 .BR clone (2):
77 if an application bypasses the glibc wrappers for these system calls by using
78 .BR syscall (2),
79 then a call to
80 .BR getpid ()
81 in the child will return the wrong value
82 (to be precise: it will return the PID of the parent process).
83 .\" The following program demonstrates this "feature":
84 .\"
85 .\" #define _GNU_SOURCE
86 .\" #include <sys/syscall.h>
87 .\" #include <sys/wait.h>
88 .\" #include <stdio.h>
89 .\" #include <stdlib.h>
90 .\" #include <unistd.h>
91 .\"
92 .\" int
93 .\" main(int argc, char *argv[])
94 .\" {
95 .\" /* The following statement fills the getpid() cache */
96 .\"
97 .\" printf("parent PID = %ld\n", (long) getpid());
98 .\"
99 .\" if (syscall(SYS_fork) == 0) {
100 .\" if (getpid() != syscall(SYS_getpid))
101 .\" printf("child getpid() mismatch: getpid()=%ld; "
102 .\" "syscall(SYS_getpid)=%ld\n",
103 .\" (long) getpid(), (long) syscall(SYS_getpid));
104 .\" exit(EXIT_SUCCESS);
105 .\" }
106 .\" wait(NULL);
107 .\"}
108 See also
109 .BR clone (2)
110 for discussion of a case where
111 .BR getpid ()
112 may return the wrong value even when invoking
113 .BR clone (2)
114 via the glibc wrapper function.
115
116 Since glibc version 2.25, the PID cache is removed, and calls to
117 .BR getpid ()
118 will not be cached.
119
120 .SH SEE ALSO
121 .BR clone (2),
122 .BR fork (2),
123 .BR kill (2),
124 .BR exec (3),
125 .BR mkstemp (3),
126 .BR tempnam (3),
127 .BR tmpfile (3),
128 .BR tmpnam (3),
129 .BR credentials (7),
130 .BR pid_namespaces (7)