Blame man2/getpid.2

Packit 7cfc04
.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETPID 2 2017-11-26 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getpid, getppid \- get process identification
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.br
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.B pid_t getpid(void);
Packit 7cfc04
.br
Packit 7cfc04
.B pid_t getppid(void);
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
returns the process ID (PID) of the calling process.
Packit 7cfc04
(This is often used by
Packit 7cfc04
routines that generate unique temporary filenames.)
Packit 7cfc04
.PP
Packit 7cfc04
.BR getppid ()
Packit 7cfc04
returns the process ID of the parent of the calling process.
Packit 7cfc04
This will be either the ID of the process that created this process using
Packit 7cfc04
.BR fork (),
Packit 7cfc04
or, if that process has already terminated,
Packit 7cfc04
the ID of the process to which this process has been reparented (either
Packit 7cfc04
.BR init (1)
Packit 7cfc04
or a "subreaper" process defined via the
Packit 7cfc04
.BR prctl (2)
Packit 7cfc04
.BR PR_SET_CHILD_SUBREAPER
Packit 7cfc04
operation).
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
These functions are always successful.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, 4.3BSD, SVr4.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
If the caller's parent is in a different PID namespace (see
Packit 7cfc04
.BR pid_namespaces (7)),
Packit 7cfc04
.BR getppid ()
Packit 7cfc04
returns 0.
Packit 7cfc04
.PP
Packit 7cfc04
From a kernel perspective,
Packit 7cfc04
the PID (which is shared by all of the threads in a multithreaded process)
Packit 7cfc04
is sometimes also known as the thread group ID (TGID).
Packit 7cfc04
This contrasts with the kernel thread ID (TID),
Packit 7cfc04
which is unique for each thread.
Packit 7cfc04
For further details, see
Packit 7cfc04
.BR gettid (2)
Packit 7cfc04
and the discussion of the
Packit 7cfc04
.BR CLONE_THREAD
Packit 7cfc04
flag in
Packit 7cfc04
.BR clone (2).
Packit 7cfc04
.\"
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
From glibc version 2.3.4 up to and including version 2.24,
Packit 7cfc04
the glibc wrapper function for
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
cached PIDs,
Packit 7cfc04
with the goal of avoiding additional system calls when a process calls
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
repeatedly.
Packit 7cfc04
Normally this caching was invisible,
Packit 7cfc04
but its correct operation relied on support in the wrapper functions for
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR vfork (2),
Packit 7cfc04
and
Packit 7cfc04
.BR clone (2):
Packit 7cfc04
if an application bypassed the glibc wrappers for these system calls by using
Packit 7cfc04
.BR syscall (2),
Packit 7cfc04
then a call to
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
in the child would return the wrong value
Packit 7cfc04
(to be precise: it would return the PID of the parent process).
Packit 7cfc04
.\" The following program demonstrates this "feature":
Packit 7cfc04
.\"
Packit 7cfc04
.\" #define _GNU_SOURCE
Packit 7cfc04
.\" #include <sys/syscall.h>
Packit 7cfc04
.\" #include <sys/wait.h>
Packit 7cfc04
.\" #include <stdio.h>
Packit 7cfc04
.\" #include <stdlib.h>
Packit 7cfc04
.\" #include <unistd.h>
Packit 7cfc04
.\"
Packit 7cfc04
.\" int
Packit 7cfc04
.\" main(int argc, char *argv[])
Packit 7cfc04
.\" {
Packit 7cfc04
.\"    /* The following statement fills the getpid() cache */
Packit 7cfc04
.\"
Packit 7cfc04
.\"    printf("parent PID = %ld\n", (long) getpid());
Packit 7cfc04
.\"
Packit 7cfc04
.\"    if (syscall(SYS_fork) == 0) {
Packit 7cfc04
.\"        if (getpid() != syscall(SYS_getpid))
Packit 7cfc04
.\"            printf("child getpid() mismatch: getpid()=%ld; "
Packit 7cfc04
.\"                    "syscall(SYS_getpid)=%ld\n",
Packit 7cfc04
.\"                    (long) getpid(), (long) syscall(SYS_getpid));
Packit 7cfc04
.\"        exit(EXIT_SUCCESS);
Packit 7cfc04
.\"    }
Packit 7cfc04
.\"    wait(NULL);
Packit 7cfc04
.\"}
Packit 7cfc04
In addition, there were cases where
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
could return the wrong value even when invoking
Packit 7cfc04
.BR clone (2)
Packit 7cfc04
via the glibc wrapper function.
Packit 7cfc04
(For a discussion of one such case, see BUGS in
Packit 7cfc04
.BR clone (2).)
Packit 7cfc04
Furthermore, the complexity of the caching code had been
Packit 7cfc04
the source of a few bugs within glibc over the years.
Packit 7cfc04
.PP
Packit 7cfc04
Because of the aforementioned problems,
Packit 7cfc04
since glibc version 2.25, the PID cache is removed:
Packit 7cfc04
.\" commit c579f48edba88380635ab98cb612030e3ed8691e
Packit 7cfc04
.\" https://sourceware.org/glibc/wiki/Release/2.25#pid_cache_removal
Packit 7cfc04
calls to
Packit 7cfc04
.BR getpid ()
Packit 7cfc04
always invoke the actual system call, rather than returning a cached value.
Packit 7cfc04
.\" FIXME .
Packit 7cfc04
.\" Review progress of https://bugzilla.redhat.com/show_bug.cgi?id=1469757
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR clone (2),
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR gettid (2),
Packit 7cfc04
.BR kill (2),
Packit 7cfc04
.BR exec (3),
Packit 7cfc04
.BR mkstemp (3),
Packit 7cfc04
.BR tempnam (3),
Packit 7cfc04
.BR tmpfile (3),
Packit 7cfc04
.BR tmpnam (3),
Packit 7cfc04
.BR credentials (7),
Packit 7cfc04
.BR pid_namespaces (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.