OSDN Git Service

ef298a59c5090bccda1e434dcc1f440c263794bb
[linuxjm/LDP_man-pages.git] / draft / man2 / getpid.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
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 .\"
24 .\" Japanese Version Copyright (c) 1997 SUTO, Mitsuaki
25 .\"     and Copyright(c) 2008 Akihiro MOTOKI
26 .\"         all rights reserved.
27 .\" Translated Thu Jun 26 20:33:01 JST 1997
28 .\"         by SUTO, Mitsuaki <suto@av.crl.sony.co.jp>
29 .\" Updated 2008-10-03, Akihiro MOTOKI, LDP v3.10
30 .\"
31 .TH GETPID 2 2008-09-23 "Linux" "Linux Programmer's Manual"
32 .\"O .SH NAME
33 .SH 名前
34 .\"O getpid, getppid \- get process identification
35 getpid, getppid \- プロセス ID を得る
36 .\"O .SH SYNOPSIS
37 .SH 書式
38 .B #include <sys/types.h>
39 .br
40 .B #include <unistd.h>
41 .sp
42 .B pid_t getpid(void);
43 .br
44 .B pid_t getppid(void);
45 .\"O .SH DESCRIPTION
46 .SH 説明
47 .\"O .BR getpid ()
48 .\"O returns the process ID of the calling process.
49 .\"O (This is often used by
50 .\"O routines that generate unique temporary filenames.)
51 .BR getpid ()
52 は呼び出し元のプロセスのプロセス ID を返す。(テンポラリ用のファイル名として
53 他と重ならない名前を生成するルーチンでしばしば使用される。)
54
55 .\"O .BR getppid ()
56 .\"O returns the process ID of the parent of the calling process.
57 .BR getppid ()
58 は呼び出し元のプロセスの親プロセスのプロセス ID を返す。
59 .\"O .SH ERRORS
60 .SH エラー
61 .\"O These functions are always successful.
62 これらの関数は常に成功する。
63 .\"O .SH "CONFORMING TO"
64 .SH 準拠
65 POSIX.1-2001, 4.3BSD, SVr4.
66 .\"O .SH NOTES
67 .SH 注意
68 .\"O Since glibc version 2.3.4,
69 .\"O the glibc wrapper function for
70 .\"O .BR getpid ()
71 .\"O caches PIDs,
72 .\"O so as to avoid additional system calls when a process calls
73 .\"O .BR getpid ()
74 .\"O repeatedly.
75 glibc バージョン 2.3.4 以降では、
76 glibc の
77 .BR getpid ()
78 のラッパー関数は PID をキャッシュする。
79 これは、プロセスが繰り返し
80 .BR getpid ()
81 を呼び出した場合にその都度システムコールを呼ぶのを避けるためである。
82 .\"O Normally this caching is invisible,
83 .\"O but its correct operation relies on support in the wrapper functions for
84 .\"O .BR fork (2),
85 .\"O .BR vfork (2),
86 .\"O and
87 .\"O .BR clone (2):
88 .\"O if an application bypasses the glibc wrappers for these system calls by using
89 .\"O .BR syscall (2),
90 .\"O then a call to
91 .\"O .BR getpid ()
92 .\"O in the child will return the wrong value
93 .\"O (to be precise: it will return the PID of the parent process).
94 通常は、このキャッシュ処理が見えることはないが、
95 キャッシュ処理が正しく働くためには
96 .BR fork (2),
97 .BR vfork (2),
98 .BR clone (2)
99 のラッパー関数でのサポートが必要である。
100 アプリケーションがこれらのシステムコールを呼び出す際に、
101 glibc のラッパー関数を経由せずに
102 .BR syscall (2)
103 を使った場合には、子プロセスで
104 .BR getpid ()
105 を呼び出すと間違った値が返ることだろう
106 (正確にいうと、親プロセスの PID が返される)。
107 .\" The following program demonstrates this "feature":
108 .\"
109 .\" #define _GNU_SOURCE
110 .\" #include <sys/syscall.h>
111 .\" #include <sys/wait.h>
112 .\" #include <stdio.h>
113 .\" #include <stdlib.h>
114 .\" #include <unistd.h>
115 .\"
116 .\" int
117 .\" main(int argc, char *argv[])
118 .\" {
119 .\"    /* The following statement fills the getpid() cache */
120 .\"
121 .\"    printf("parent PID = %ld\n", (long) getpid());
122 .\"
123 .\"    if (syscall(SYS_fork) == 0) {
124 .\"        if (getpid() != syscall(SYS_getpid))
125 .\"            printf("child getpid() mismatch: getpid()=%ld; "
126 .\"                    "syscall(SYS_getpid)=%ld\n",
127 .\"                    (long) getpid(), (long) syscall(SYS_getpid));
128 .\"        exit(EXIT_SUCCESS);
129 .\"    }
130 .\"    wait(NULL);
131 .\"}
132 .\"O See also
133 .\"O .BR clone (2)
134 .\"O for discussion of a case where
135 .\"O .BR getpid ()
136 .\"O may return the wrong value even when invoking
137 .\"O .BR clone (2)
138 .\"O via the glibc wrapper function.
139 .BR clone (2)
140 を glibc のラッパー関数経由で起動した際にも
141 .BR getpid ()
142 が間違った値を返す場合があり、これに関する議論は
143 .BR clone (2)
144 も参照してほしい。
145 .\"O .SH "SEE ALSO"
146 .SH 関連項目
147 .BR clone (2),
148 .BR fork (2),
149 .BR kill (2),
150 .BR exec (3),
151 .BR mkstemp (3),
152 .BR tempnam (3),
153 .BR tmpfile (3),
154 .BR tmpnam (3),
155 .BR credentials (7)