]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getpid.2
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[thirdparty/man-pages.git] / man2 / getpid.2
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" %%%LICENSE_END
23 .TH GETPID 2 2008-09-23 "Linux" "Linux Programmer's Manual"
24 .SH NAME
25 getpid, getppid \- get process identification
26 .SH SYNOPSIS
27 .B #include <sys/types.h>
28 .br
29 .B #include <unistd.h>
30 .sp
31 .B pid_t getpid(void);
32 .br
33 .B pid_t getppid(void);
34 .SH DESCRIPTION
35 .BR getpid ()
36 returns the process ID of the calling process.
37 (This is often used by
38 routines that generate unique temporary filenames.)
39
40 .BR getppid ()
41 returns the process ID of the parent of the calling process.
42 .SH ERRORS
43 These functions are always successful.
44 .SH CONFORMING TO
45 POSIX.1-2001, 4.3BSD, SVr4.
46 .SH NOTES
47 Since glibc version 2.3.4,
48 the glibc wrapper function for
49 .BR getpid ()
50 caches PIDs,
51 so as to avoid additional system calls when a process calls
52 .BR getpid ()
53 repeatedly.
54 Normally this caching is invisible,
55 but its correct operation relies on support in the wrapper functions for
56 .BR fork (2),
57 .BR vfork (2),
58 and
59 .BR clone (2):
60 if an application bypasses the glibc wrappers for these system calls by using
61 .BR syscall (2),
62 then a call to
63 .BR getpid ()
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":
67 .\"
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>
74 .\"
75 .\" int
76 .\" main(int argc, char *argv[])
77 .\" {
78 .\" /* The following statement fills the getpid() cache */
79 .\"
80 .\" printf("parent PID = %ld\n", (long) getpid());
81 .\"
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);
88 .\" }
89 .\" wait(NULL);
90 .\"}
91 See also
92 .BR clone (2)
93 for discussion of a case where
94 .BR getpid ()
95 may return the wrong value even when invoking
96 .BR clone (2)
97 via the glibc wrapper function.
98 .SH SEE ALSO
99 .BR clone (2),
100 .BR fork (2),
101 .BR kill (2),
102 .BR exec (3),
103 .BR mkstemp (3),
104 .BR tempnam (3),
105 .BR tmpfile (3),
106 .BR tmpnam (3),
107 .BR credentials (7)