1GETPID(2)                  Linux Programmer's Manual                 GETPID(2)
2
3
4

NAME

6       getpid, getppid - get process identification
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <unistd.h>
11
12       pid_t getpid(void);
13       pid_t getppid(void);
14

DESCRIPTION

16       getpid() returns the process ID (PID) of the calling process.  (This is
17       often used by routines that generate unique temporary filenames.)
18
19       getppid() returns the process ID of the parent of the calling  process.
20       This  will  be  either  the ID of the process that created this process
21       using fork(), or, if that process has already terminated, the ID of the
22       process  to which this process has been reparented (either init(1) or a
23       "subreaper" process defined  via  the  prctl(2)  PR_SET_CHILD_SUBREAPER
24       operation).
25

ERRORS

27       These functions are always successful.
28

CONFORMING TO

30       POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
31

NOTES

33       If  the caller's parent is in a different PID namespace (see pid_names‐
34       paces(7)), getppid() returns 0.
35
36       From a kernel perspective, the PID (which  is  shared  by  all  of  the
37       threads  in  a  multithreaded  process)  is sometimes also known as the
38       thread group ID (TGID).  This  contrasts  with  the  kernel  thread  ID
39       (TID),  which is unique for each thread.  For further details, see get‐
40       tid(2) and the discussion of the CLONE_THREAD flag in clone(2).
41
42   C library/kernel differences
43       From glibc version 2.3.4 up to and including version  2.24,  the  glibc
44       wrapper  function  for  getpid() cached PIDs, with the goal of avoiding
45       additional system calls when a process calls getpid() repeatedly.  Nor‐
46       mally  this  caching was invisible, but its correct operation relied on
47       support in the wrapper functions for fork(2), vfork(2),  and  clone(2):
48       if an application bypassed the glibc wrappers for these system calls by
49       using syscall(2), then a call to getpid() in the child would return the
50       wrong  value  (to  be  precise:  it  would return the PID of the parent
51       process).  In addition, there were cases where  getpid()  could  return
52       the wrong value even when invoking clone(2) via the glibc wrapper func‐
53       tion.  (For a discussion of one such case, see BUGS in clone(2).)  Fur‐
54       thermore,  the  complexity of the caching code had been the source of a
55       few bugs within glibc over the years.
56
57       Because of the aforementioned problems, since glibc version  2.25,  the
58       PID cache is removed: calls to getpid() always invoke the actual system
59       call, rather than returning a cached value.
60

SEE ALSO

62       clone(2), fork(2), gettid(2), kill(2), exec(3), mkstemp(3), tempnam(3),
63       tmpfile(3), tmpnam(3), credentials(7), pid_namespaces(7)
64

COLOPHON

66       This  page  is  part of release 4.15 of the Linux man-pages project.  A
67       description of the project, information about reporting bugs,  and  the
68       latest     version     of     this    page,    can    be    found    at
69       https://www.kernel.org/doc/man-pages/.
70
71
72
73Linux                             2017-11-26                         GETPID(2)
Impressum