1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH getpid 2 (date) "Linux man-pages (unreleased)"
7 getpid, getppid \- get process identification
10 .RI ( libc ", " \-lc )
13 .B #include <unistd.h>
15 .B pid_t getpid(void);
16 .B pid_t getppid(void);
20 returns the process ID (PID) of the calling process.
21 (This is often used by
22 routines that generate unique temporary filenames.)
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
28 or, if that process has already terminated,
29 the ID of the process to which this process has been reparented (either
31 or a "subreaper" process defined via the
33 .B PR_SET_CHILD_SUBREAPER
36 These functions are always successful.
38 POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
40 If the caller's parent is in a different PID namespace (see
41 .BR pid_namespaces (7)),
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
52 and the discussion of the
57 .SS C library/kernel differences
58 From glibc version 2.3.4 up to and including version 2.24,
59 the glibc wrapper function for
62 with the goal of avoiding additional system calls when a process calls
65 Normally this caching was invisible,
66 but its correct operation relied on support in the wrapper functions for
71 if an application bypassed the glibc wrappers for these system calls by using
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":
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>
88 .\" main(int argc, char *argv[])
90 .\" /* The following statement fills the getpid() cache */
92 .\" printf("parent PID = %ld\n", (intmax_t) getpid());
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);
103 In addition, there were cases where
105 could return the wrong value even when invoking
107 via the glibc wrapper function.
108 (For a discussion of one such case, see BUGS in
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 version 2.25, the PID cache is removed:
115 .\" commit c579f48edba88380635ab98cb612030e3ed8691e
116 .\" https://sourceware.org/glibc/wiki/Release/2.25#pid_cache_removal
119 always invoke the actual system call, rather than returning a cached value.
121 .\" Review progress of https://bugzilla.redhat.com/show_bug.cgi?id=1469757
123 On Alpha, instead of a pair of
127 system calls, a single
129 system call is provided, which returns a pair of PID and parent PID.
134 wrapper functions transparently deal with this.
137 for details regarding register mapping.
149 .BR pid_namespaces (7)