OSDN Git Service

dea04c82e6378715e584e4294d414a7530941ba2
[linuxjm/LDP_man-pages.git] / original / man2 / getpid.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH GETPID 2 2008-09-23 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 getpid, getppid \- get process identification
28 .SH SYNOPSIS
29 .B #include <sys/types.h>
30 .br
31 .B #include <unistd.h>
32 .sp
33 .B pid_t getpid(void);
34 .br
35 .B pid_t getppid(void);
36 .SH DESCRIPTION
37 .BR getpid ()
38 returns the process ID of the calling process.
39 (This is often used by
40 routines that generate unique temporary filenames.)
41
42 .BR getppid ()
43 returns the process ID of the parent of the calling process.
44 .SH ERRORS
45 These functions are always successful.
46 .SH CONFORMING TO
47 POSIX.1-2001, 4.3BSD, SVr4.
48 .SH NOTES
49 Since glibc version 2.3.4,
50 the glibc wrapper function for
51 .BR getpid ()
52 caches PIDs,
53 so as to avoid additional system calls when a process calls
54 .BR getpid ()
55 repeatedly.
56 Normally this caching is invisible,
57 but its correct operation relies on support in the wrapper functions for
58 .BR fork (2),
59 .BR vfork (2),
60 and
61 .BR clone (2):
62 if an application bypasses the glibc wrappers for these system calls by using
63 .BR syscall (2),
64 then a call to
65 .BR getpid ()
66 in the child will return the wrong value
67 (to be precise: it will return the PID of the parent process).
68 .\" The following program demonstrates this "feature":
69 .\"
70 .\" #define _GNU_SOURCE
71 .\" #include <sys/syscall.h>
72 .\" #include <sys/wait.h>
73 .\" #include <stdio.h>
74 .\" #include <stdlib.h>
75 .\" #include <unistd.h>
76 .\"
77 .\" int
78 .\" main(int argc, char *argv[])
79 .\" {
80 .\"    /* The following statement fills the getpid() cache */
81 .\"
82 .\"    printf("parent PID = %ld\n", (long) getpid());
83 .\"
84 .\"    if (syscall(SYS_fork) == 0) {
85 .\"        if (getpid() != syscall(SYS_getpid))
86 .\"            printf("child getpid() mismatch: getpid()=%ld; "
87 .\"                    "syscall(SYS_getpid)=%ld\n",
88 .\"                    (long) getpid(), (long) syscall(SYS_getpid));
89 .\"        exit(EXIT_SUCCESS);
90 .\"    }
91 .\"    wait(NULL);
92 .\"}
93 See also
94 .BR clone (2)
95 for discussion of a case where
96 .BR getpid ()
97 may return the wrong value even when invoking
98 .BR clone (2)
99 via the glibc wrapper function.
100 .SH SEE ALSO
101 .BR clone (2),
102 .BR fork (2),
103 .BR kill (2),
104 .BR exec (3),
105 .BR mkstemp (3),
106 .BR tempnam (3),
107 .BR tmpfile (3),
108 .BR tmpnam (3),
109 .BR credentials (7)
110 .SH COLOPHON
111 This page is part of release 3.67 of the Linux
112 .I man-pages
113 project.
114 A description of the project,
115 information about reporting bugs,
116 and the latest version of this page,
117 can be found at
118 \%http://www.kernel.org/doc/man\-pages/.