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