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