1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH GETPID 2 2017-05-03 "Linux" "Linux Programmer's Manual"
27 getpid, getppid \- get process identification
29 .B #include <sys/types.h>
31 .B #include <unistd.h>
33 .B pid_t getpid(void);
35 .B pid_t getppid(void);
38 returns the process ID of the calling process.
39 (This is often used by
40 routines that generate unique temporary filenames.)
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
46 or, if that process has already terminated,
47 the ID of the process to which this process has been reparented (either
49 or a "subreaper" process defined via the
51 .BR PR_SET_CHILD_SUBREAPER
54 These functions are always successful.
56 POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
58 If the caller's parent is in a different PID namespace (see
59 .BR pid_namespaces (7)),
63 .SS C library/kernel differences
64 From glibc version 2.3.4 up to and including version 2.24,
65 the glibc wrapper function for
68 with the goal of avoiding additional system calls when a process calls
71 Normally this caching was invisible,
72 but its correct operation relied on support in the wrapper functions for
77 if an application bypassed the glibc wrappers for these system calls by using
81 in the child would return the wrong value
82 (to be precise: it would return the PID of the parent process).
83 .\" The following program demonstrates this "feature":
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>
93 .\" main(int argc, char *argv[])
95 .\" /* The following statement fills the getpid() cache */
97 .\" printf("parent PID = %ld\n", (long) getpid());
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);
108 In addition, there were cases where
110 could return the wrong value even when invoking
112 via the glibc wrapper function.
113 (For a discussion of one such case, see BUGS in
115 Furthermore, the complexity of the caching code had been
116 the source of a few bugs within glibc over the years.
118 Because of the aforementioned problems,
119 since glibc version 2.25, the PID cache is removed:
120 .\" commit c579f48edba88380635ab98cb612030e3ed8691e
121 .\" https://sourceware.org/glibc/wiki/Release/2.25#pid_cache_removal
124 always invoke the actual system call, rather than returning a cached value.
135 .BR pid_namespaces (7)