1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" %%%LICENSE_START(verbatim)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH GETPID 2 2008-09-23 "Linux" "Linux Programmer's Manual"
25 getpid, getppid \- get process identification
27 .B #include <sys/types.h>
29 .B #include <unistd.h>
31 .B pid_t getpid(void);
33 .B pid_t getppid(void);
36 returns the process ID of the calling process.
37 (This is often used by
38 routines that generate unique temporary filenames.)
41 returns the process ID of the parent of the calling process.
43 These functions are always successful.
45 POSIX.1-2001, 4.3BSD, SVr4.
47 Since glibc version 2.3.4,
48 the glibc wrapper function for
51 so as to avoid additional system calls when a process calls
54 Normally this caching is invisible,
55 but its correct operation relies on support in the wrapper functions for
60 if an application bypasses the glibc wrappers for these system calls by using
64 in the child will return the wrong value
65 (to be precise: it will return the PID of the parent process).
66 .\" The following program demonstrates this "feature":
68 .\" #define _GNU_SOURCE
69 .\" #include <sys/syscall.h>
70 .\" #include <sys/wait.h>
71 .\" #include <stdio.h>
72 .\" #include <stdlib.h>
73 .\" #include <unistd.h>
76 .\" main(int argc, char *argv[])
78 .\" /* The following statement fills the getpid() cache */
80 .\" printf("parent PID = %ld\n", (long) getpid());
82 .\" if (syscall(SYS_fork) == 0) {
83 .\" if (getpid() != syscall(SYS_getpid))
84 .\" printf("child getpid() mismatch: getpid()=%ld; "
85 .\" "syscall(SYS_getpid)=%ld\n",
86 .\" (long) getpid(), (long) syscall(SYS_getpid));
87 .\" exit(EXIT_SUCCESS);
93 for discussion of a case where
95 may return the wrong value even when invoking
97 via the glibc wrapper function.