]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/getpid.2
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / 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.\"
4c1c5274 5.TH getpid 2 (date) "Linux man-pages (unreleased)"
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>
c6d039a3 14.P
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.)
c6d039a3 23.P
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.
4131356c
AC
37.SH VERSIONS
38On Alpha, instead of a pair of
39.BR getpid ()
40and
da83e8a2 41.BR getppid ()
4131356c
AC
42system calls, a single
43.BR getxpid ()
44system call is provided, which returns a pair of PID and parent PID.
45The glibc
46.BR getpid ()
47and
48.BR getppid ()
49wrapper functions transparently deal with this.
50See
51.BR syscall (2)
52for details regarding register mapping.
53.SH STANDARDS
54POSIX.1-2008.
55.SH HISTORY
56POSIX.1-2001, 4.3BSD, SVr4.
0722a578 57.SS C library/kernel differences
3ea21610 58From glibc 2.3.4 up to and including glibc 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.
c6d039a3 112.P
9338d37b 113Because of the aforementioned problems,
b324e17d 114since glibc 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
4131356c
AC
122.SH NOTES
123If the caller's parent is in a different PID namespace (see
124.BR pid_namespaces (7)),
70ea1968 125.BR getppid ()
4131356c 126returns 0.
c6d039a3 127.P
4131356c
AC
128From a kernel perspective,
129the PID (which is shared by all of the threads in a multithreaded process)
130is sometimes also known as the thread group ID (TGID).
131This contrasts with the kernel thread ID (TID),
132which is unique for each thread.
133For further details, see
134.BR gettid (2)
135and the discussion of the
136.B CLONE_THREAD
137flag in
138.BR clone (2).
47297adb 139.SH SEE ALSO
67f0f50d 140.BR clone (2),
fea681da 141.BR fork (2),
12fe9b17 142.BR gettid (2),
fea681da
MK
143.BR kill (2),
144.BR exec (3),
145.BR mkstemp (3),
146.BR tempnam (3),
147.BR tmpfile (3),
53a1443c 148.BR tmpnam (3),
4effb5be 149.BR credentials (7),
7e0e902b 150.BR pid_namespaces (7)