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