1 |
|
2 |
=encoding euc-jp |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
=begin original |
7 |
|
8 |
perlfork - Perl's fork() emulation |
9 |
|
10 |
=end original |
11 |
|
12 |
perlfork - Perl の fork エミュレーション |
13 |
|
14 |
=head1 SYNOPSIS |
15 |
|
16 |
=begin original |
17 |
|
18 |
NOTE: As of the 5.8.0 release, fork() emulation has considerably |
19 |
matured. However, there are still a few known bugs and differences |
20 |
from real fork() that might affect you. See the "BUGS" and |
21 |
"CAVEATS AND LIMITATIONS" sections below. |
22 |
|
23 |
=end original |
24 |
|
25 |
注意: 5.8.0 のリリースと共に、fork() エミュレーションはかなり成熟し |
26 |
ています。しかしながら、またいくつかのバグや実際の fork() との差違 |
27 |
が知られています。後述の "バグ" 及び "警告と制限" の章も参照してく |
28 |
ださい。 |
29 |
|
30 |
=begin original |
31 |
|
32 |
Perl provides a fork() keyword that corresponds to the Unix system call |
33 |
of the same name. On most Unix-like platforms where the fork() system |
34 |
call is available, Perl's fork() simply calls it. |
35 |
|
36 |
=end original |
37 |
|
38 |
Perl は同名の Unix システムコールに対応するキーワード fork() を |
39 |
提供しています。 |
40 |
fork() システムコールが存在する大抵の Unix 風プラットフォームでは |
41 |
Perl の fork() は単純にそれを呼ぶだけです。 |
42 |
|
43 |
=begin original |
44 |
|
45 |
On some platforms such as Windows where the fork() system call is not |
46 |
available, Perl can be built to emulate fork() at the interpreter level. |
47 |
While the emulation is designed to be as compatible as possible with the |
48 |
real fork() at the level of the Perl program, there are certain |
49 |
important differences that stem from the fact that all the pseudo child |
50 |
"processes" created this way live in the same real process as far as the |
51 |
operating system is concerned. |
52 |
|
53 |
=end original |
54 |
|
55 |
Windows といった fork() システムコールを持っていないいくつかの |
56 |
プラットフォームでは、インタプリタレベルで fork() のエミュレーションを |
57 |
構築します。 |
58 |
エミュレーションは Perl プログラムのレベルに於いて |
59 |
本物の fork() とできる限り互換がとれるように設計されていますが, |
60 |
この方法で生成される全ての仮想的な子「プロセス」は |
61 |
オペレーティングシステムが関与する限りでは同じ実プロセスとして |
62 |
存在するためにいくらかの重要な違いが存在します。 |
63 |
|
64 |
=begin original |
65 |
|
66 |
This document provides a general overview of the capabilities and |
67 |
limitations of the fork() emulation. Note that the issues discussed here |
68 |
are not applicable to platforms where a real fork() is available and Perl |
69 |
has been configured to use it. |
70 |
|
71 |
=end original |
72 |
|
73 |
このドキュメントでは fork() エミュレーションの能力と限界の概要を |
74 |
提供します。 |
75 |
ここで述べられていることは本物の fork() が存在して |
76 |
Perl がそれを使うように設定されているプラットフォームには |
77 |
当てはまりません。 |
78 |
|
79 |
=head1 DESCRIPTION |
80 |
|
81 |
=begin original |
82 |
|
83 |
The fork() emulation is implemented at the level of the Perl interpreter. |
84 |
What this means in general is that running fork() will actually clone the |
85 |
running interpreter and all its state, and run the cloned interpreter in |
86 |
a separate thread, beginning execution in the new thread just after the |
87 |
point where the fork() was called in the parent. We will refer to the |
88 |
thread that implements this child "process" as the pseudo-process. |
89 |
|
90 |
=end original |
91 |
|
92 |
fork() エミュレーションは Perl インタプリタのレベルで実装されて |
93 |
います。 |
94 |
これの意味するところはおおざっぱに言うと fork() の実行は実際には |
95 |
実行しているインタプリタとその状態の全てを複製し、 |
96 |
複製されたインタプリタを別のスレッドで、親で fork() が呼び出された |
97 |
すぐ後から実行を始めることです。 |
98 |
仮想的なプロセスとしてこの子「プロセス」を実装しているスレッドに |
99 |
着目します。 |
100 |
|
101 |
=begin original |
102 |
|
103 |
To the Perl program that called fork(), all this is designed to be |
104 |
transparent. The parent returns from the fork() with a pseudo-process |
105 |
ID that can be subsequently used in any process-manipulation functions; |
106 |
the child returns from the fork() with a value of C<0> to signify that |
107 |
it is the child pseudo-process. |
108 |
|
109 |
=end original |
110 |
|
111 |
fork() を呼び出した Perl プログラムにとって、この全ては透過的であるように |
112 |
設計されています。 |
113 |
親プロセスは fork() からその後の |
114 |
プロセス操作関数で使うことのできる仮想プロセス ID を伴って戻り、 |
115 |
子プロセスでは子仮想プロセスであることを示す値 C<0> を伴って |
116 |
戻ります。 |
117 |
|
118 |
=head2 Behavior of other Perl features in forked pseudo-processes |
119 |
|
120 |
(fork された擬似プロセスの中でのその他の Perl の機能の振る舞い) |
121 |
|
122 |
=begin original |
123 |
|
124 |
Most Perl features behave in a natural way within pseudo-processes. |
125 |
|
126 |
=end original |
127 |
|
128 |
大抵の Perl の機能は仮想プロセスでも自然に振る舞います。 |
129 |
|
130 |
=over 8 |
131 |
|
132 |
=item $$ or $PROCESS_ID |
133 |
|
134 |
($$ 及び $PROCESS_ID) |
135 |
|
136 |
=begin original |
137 |
|
138 |
This special variable is correctly set to the pseudo-process ID. |
139 |
It can be used to identify pseudo-processes within a particular |
140 |
session. Note that this value is subject to recycling if any |
141 |
pseudo-processes are launched after others have been wait()-ed on. |
142 |
|
143 |
=end original |
144 |
|
145 |
この特殊変数は適切に仮想プロセス ID に設定されます。 |
146 |
これは特定のセッションに於いて仮想プロセスを識別するために |
147 |
使うことができます。 |
148 |
この値は wait() された後に起動された |
149 |
仮想プロセスでは再利用されることに注意して下さい。 |
150 |
|
151 |
=item %ENV |
152 |
|
153 |
=begin original |
154 |
|
155 |
Each pseudo-process maintains its own virtual environment. Modifications |
156 |
to %ENV affect the virtual environment, and are only visible within that |
157 |
pseudo-process, and in any processes (or pseudo-processes) launched from |
158 |
it. |
159 |
|
160 |
=end original |
161 |
|
162 |
各仮想プロセスはそれぞれの仮想環境を持っています。 |
163 |
%ENV の変更は仮想環境に作用し、その仮想プロセスと、そこから起動した |
164 |
全てのプロセス(及び仮想プロセス)でのみ見ることができます。 |
165 |
|
166 |
=item chdir() and all other builtins that accept filenames |
167 |
|
168 |
(chdir() 及びファイル名を受け取る他の全ての組み込み関数) |
169 |
|
170 |
=begin original |
171 |
|
172 |
Each pseudo-process maintains its own virtual idea of the current directory. |
173 |
Modifications to the current directory using chdir() are only visible within |
174 |
that pseudo-process, and in any processes (or pseudo-processes) launched from |
175 |
it. All file and directory accesses from the pseudo-process will correctly |
176 |
map the virtual working directory to the real working directory appropriately. |
177 |
|
178 |
=end original |
179 |
|
180 |
各仮想プロセスはそれぞれに仮想的なカレントディレクトリの主題(idea)を |
181 |
持っています。 |
182 |
chdir() を使ったカレントディレクトリの変更はその |
183 |
仮想プロセスと、そこから起動した全てのプロセス(及び仮想プロセス)でのみ |
184 |
見ることができます。 |
185 |
仮想プロセスからの全てのファイル及びディレクトリアクセスは |
186 |
仮想作業ディレクトリから実作業ディレクトリへと適切に |
187 |
正しく変換されます。 |
188 |
|
189 |
=item wait() and waitpid() |
190 |
|
191 |
(wait() 及び waitpid()) |
192 |
|
193 |
=begin original |
194 |
|
195 |
wait() and waitpid() can be passed a pseudo-process ID returned by fork(). |
196 |
These calls will properly wait for the termination of the pseudo-process |
197 |
and return its status. |
198 |
|
199 |
=end original |
200 |
|
201 |
wait() 及び waitpid() に fork() から返される仮想プロセス ID を |
202 |
渡すことができます。 |
203 |
これらの呼び出しは仮想プロセスの終了を適切に待ち、 |
204 |
その状態を返します。 |
205 |
|
206 |
=item kill() |
207 |
|
208 |
=begin original |
209 |
|
210 |
C<kill('KILL', ...)> can be used to terminate a pseudo-process by |
211 |
passing it the ID returned by fork(). The outcome of kill on a pseudo-process |
212 |
is unpredictable and it should not be used except |
213 |
under dire circumstances, because the operating system may not |
214 |
guarantee integrity of the process resources when a running thread is |
215 |
terminated. The process which implements the pseudo-processes can be blocked |
216 |
and the Perl interpreter hangs. Note that using C<kill('KILL', ...)> on a |
217 |
pseudo-process() may typically cause memory leaks, because the thread |
218 |
that implements the pseudo-process does not get a chance to clean up |
219 |
its resources. |
220 |
|
221 |
=end original |
222 |
|
223 |
C<kill('KILL', ...)> は fork() から返された ID を渡すことで仮想プロセスを |
224 |
停止することができます。 |
225 |
仮想プロセスに対する kill の結果は予測不能で、これは悲惨な状況下以外では |
226 |
使うべきではありません; |
227 |
なぜならオペレーティングシステムは実行しているスレッドが終了した |
228 |
時ではプロセスリソースの完全性を保証しないかもしれないからです。 |
229 |
仮想プロセスを実装しているプロセスがブロックされて、Perl が |
230 |
ハングするかもしれません。 |
231 |
仮想プロセスに対して C<kill('KILL', ...)> を使うと大抵メモリリークを |
232 |
引き起こします; これは仮想プロセスを実装しているスレッドにはそのリソースを |
233 |
解放するタイミングをとれないためです。 |
234 |
|
235 |
=begin original |
236 |
|
237 |
C<kill('TERM', ...)> can also be used on pseudo-processes, but the |
238 |
signal will not be delivered while the pseudo-process is blocked by a |
239 |
system call, e.g. waiting for a socket to connect, or trying to read |
240 |
from a socket with no data available. Starting in Perl 5.14 the |
241 |
parent process will not wait for children to exit once they have been |
242 |
signalled with C<kill('TERM', ...)> to avoid deadlock during process |
243 |
exit. You will have to explicitly call waitpid() to make sure the |
244 |
child has time to clean-up itself, but you are then also responsible |
245 |
that the child is not blocking on I/O either. |
246 |
|
247 |
=end original |
248 |
|
249 |
C<kill('TERM', ...)> は疑似プロセスに対しても使われますが、疑似プロセスが |
250 |
システムコールでブロックされている間 (例えば、ソケット接続を待っていたり、 |
251 |
データが無いときにソケットから読み込もうとしている間)はシグナルは |
252 |
配達されません。 |
253 |
Perl 5.14 から、プロセス終了時のデッドロックを避けるために、親プロセスは |
254 |
C<kill('TERM', ...)> のシグナルを受けた子プロセスの終了を |
255 |
待たなくなりました。 |
256 |
子プロセスが自身でクリーンナップする時間があるようにするためには |
257 |
明示的に waitpid() を呼び出す必要がありますが、子プロセスが I/O で |
258 |
ブロックされていないことにも責任を持つ必要があります。 |
259 |
|
260 |
=item exec() |
261 |
|
262 |
=begin original |
263 |
|
264 |
Calling exec() within a pseudo-process actually spawns the requested |
265 |
executable in a separate process and waits for it to complete before |
266 |
exiting with the same exit status as that process. This means that the |
267 |
process ID reported within the running executable will be different from |
268 |
what the earlier Perl fork() might have returned. Similarly, any process |
269 |
manipulation functions applied to the ID returned by fork() will affect the |
270 |
waiting pseudo-process that called exec(), not the real process it is |
271 |
waiting for after the exec(). |
272 |
|
273 |
=end original |
274 |
|
275 |
仮想プロセスでの exec() 呼び出しは実際には要求された実行形式を |
276 |
別のプロセス空間で呼び出し、そのプロセスの終了ステータスと |
277 |
同じステータスで終了するように待機します。 |
278 |
これは実行している実行形式が持っているプロセス ID がそれに先立つ |
279 |
Perl の fork() で返されたプロセス ID は異なることを意味します。 |
280 |
同じように、fork() によって返された ID を渡すプロセス操作関数は |
281 |
exec() の後で待っている実プロセスではなく、exec() を呼び出した |
282 |
仮想プロセスに対して作用します。 |
283 |
|
284 |
=begin original |
285 |
|
286 |
When exec() is called inside a pseudo-process then DESTROY methods and |
287 |
END blocks will still be called after the external process returns. |
288 |
|
289 |
=end original |
290 |
|
291 |
exec() が擬似プロセスの内側で呼び出されると、DESTROY メソッドと |
292 |
END ブロックは外部プロセスが返ってきた後に呼び出されるままです。 |
293 |
|
294 |
=item exit() |
295 |
|
296 |
=begin original |
297 |
|
298 |
exit() always exits just the executing pseudo-process, after automatically |
299 |
wait()-ing for any outstanding child pseudo-processes. Note that this means |
300 |
that the process as a whole will not exit unless all running pseudo-processes |
301 |
have exited. See below for some limitations with open filehandles. |
302 |
|
303 |
=end original |
304 |
|
305 |
exit() はいつでも、起動中の子仮想プロセスを自動的に wait() してから、 |
306 |
実行している仮想プロセスを終了させます。 |
307 |
これはそのプロセスは全ての |
308 |
実行中の仮想プロセスが終了するまでしばらくの間終了しないことを |
309 |
意味します。 |
310 |
オープンしたファイルハンドルに関する制限については以下を参照してください。 |
311 |
|
312 |
=item Open handles to files, directories and network sockets |
313 |
|
314 |
(ファイル、ディレクトリ、及びネットワークソケットに対する開いているハンドル) |
315 |
|
316 |
=begin original |
317 |
|
318 |
All open handles are dup()-ed in pseudo-processes, so that closing |
319 |
any handles in one process does not affect the others. See below for |
320 |
some limitations. |
321 |
|
322 |
=end original |
323 |
|
324 |
全ての開いているハンドルは子プロセスで dup() されるので、 |
325 |
どこかのプロセスでハンドルを閉じても他には影響しません。 |
326 |
いくつかの制限については続きを見て下さい。 |
327 |
|
328 |
=back |
329 |
|
330 |
=head2 Resource limits |
331 |
|
332 |
(リソースの制限) |
333 |
|
334 |
=begin original |
335 |
|
336 |
In the eyes of the operating system, pseudo-processes created via the fork() |
337 |
emulation are simply threads in the same process. This means that any |
338 |
process-level limits imposed by the operating system apply to all |
339 |
pseudo-processes taken together. This includes any limits imposed by the |
340 |
operating system on the number of open file, directory and socket handles, |
341 |
limits on disk space usage, limits on memory size, limits on CPU utilization |
342 |
etc. |
343 |
|
344 |
=end original |
345 |
|
346 |
オペレーシングシステムから見ると、fork() エミュレーションから生成された |
347 |
仮想プロセスは単なる同じプロセス内のスレッドです。 |
348 |
これはオペレーシングシステムによって科せられた全てのプロセスレベルの制限は |
349 |
全ての仮想プロセスで一緒に割り当てられます。 |
350 |
これには開いているファイル、 |
351 |
ディレクトリ、ソケットの数の制限、ディスク使用量の制限、 |
352 |
メモリサイズの制限、CPU 使用量の制限等が含まれます。 |
353 |
|
354 |
=head2 Killing the parent process |
355 |
|
356 |
(親プロセスの kill) |
357 |
|
358 |
=begin original |
359 |
|
360 |
If the parent process is killed (either using Perl's kill() builtin, or |
361 |
using some external means) all the pseudo-processes are killed as well, |
362 |
and the whole process exits. |
363 |
|
364 |
=end original |
365 |
|
366 |
親プロセスが kill (Perl の kill() 組み込み関数若しくは外部の同等の |
367 |
物で)されると、全ての仮想プロセスも同様に kill され、プロセス全体が |
368 |
終了します。 |
369 |
|
370 |
=head2 Lifetime of the parent process and pseudo-processes |
371 |
|
372 |
(親プロセスと仮想プロセスの生存期間) |
373 |
|
374 |
=begin original |
375 |
|
376 |
During the normal course of events, the parent process and every |
377 |
pseudo-process started by it will wait for their respective pseudo-children |
378 |
to complete before they exit. This means that the parent and every |
379 |
pseudo-child created by it that is also a pseudo-parent will only exit |
380 |
after their pseudo-children have exited. |
381 |
|
382 |
=end original |
383 |
|
384 |
通常のイベントの進み方であれば、親プロセスとそこから起動された |
385 |
それぞれの仮想プロセスは終了する前に各自の仮想子プロセスを待つでしょう。 |
386 |
これは親プロセスとそこから起動されたそれぞれのそれがまた |
387 |
仮想親プロセスである仮想子プロセスはそれらの仮想子プロセスが終了した |
388 |
後でのみ終了するでしょう。 |
389 |
|
390 |
=begin original |
391 |
|
392 |
Starting with Perl 5.14 a parent will not wait() automatically |
393 |
for any child that has been signalled with C<kill('TERM', ...)> |
394 |
to avoid a deadlock in case the child is blocking on I/O and |
395 |
never receives the signal. |
396 |
|
397 |
=end original |
398 |
|
399 |
Perl 5.14 から、子プロセスが I/O でブロックされていてシグナルを受け取れない |
400 |
場合のデッドロックを避けるために、親プロセスは C<kill('TERM', ...)> シグナルを |
401 |
受けた子プロセスを自動的に wait() しなくなりました。 |
402 |
|
403 |
=head1 CAVEATS AND LIMITATIONS |
404 |
|
405 |
(警告及び制限) |
406 |
|
407 |
=over 8 |
408 |
|
409 |
=item BEGIN blocks |
410 |
|
411 |
(BEGIN ブロック) |
412 |
|
413 |
=begin original |
414 |
|
415 |
The fork() emulation will not work entirely correctly when called from |
416 |
within a BEGIN block. The forked copy will run the contents of the |
417 |
BEGIN block, but will not continue parsing the source stream after the |
418 |
BEGIN block. For example, consider the following code: |
419 |
|
420 |
=end original |
421 |
|
422 |
fork() エミュレーションは BEGIN ブロックで呼ばれた時には完全には |
423 |
正しく動作しません。 |
424 |
fork された複製は BEGIN ブロックの内容を実行しますが、BEGIN ブロックの後の |
425 |
ソースストリームのパースを継続しません。 |
426 |
例えば、次のコードを考えてみます: |
427 |
|
428 |
BEGIN { |
429 |
fork and exit; # fork child and exit the parent |
430 |
print "inner\n"; |
431 |
} |
432 |
print "outer\n"; |
433 |
|
434 |
=begin original |
435 |
|
436 |
This will print: |
437 |
|
438 |
=end original |
439 |
|
440 |
これは次のように出力します: |
441 |
|
442 |
inner |
443 |
|
444 |
=begin original |
445 |
|
446 |
rather than the expected: |
447 |
|
448 |
=end original |
449 |
|
450 |
本来は次のようであるはずです: |
451 |
|
452 |
inner |
453 |
outer |
454 |
|
455 |
=begin original |
456 |
|
457 |
This limitation arises from fundamental technical difficulties in |
458 |
cloning and restarting the stacks used by the Perl parser in the |
459 |
middle of a parse. |
460 |
|
461 |
=end original |
462 |
|
463 |
この制限はパース途中の Perl パーサによって使われるスタックの |
464 |
複製と再開における基礎技術の複雑さに起因しています。 |
465 |
|
466 |
=item Open filehandles |
467 |
|
468 |
(開いているファイルハンドル) |
469 |
|
470 |
=begin original |
471 |
|
472 |
Any filehandles open at the time of the fork() will be dup()-ed. Thus, |
473 |
the files can be closed independently in the parent and child, but beware |
474 |
that the dup()-ed handles will still share the same seek pointer. Changing |
475 |
the seek position in the parent will change it in the child and vice-versa. |
476 |
One can avoid this by opening files that need distinct seek pointers |
477 |
separately in the child. |
478 |
|
479 |
=end original |
480 |
|
481 |
fork() した時点で開いている全てのファイルハンドルは dup() されます。 |
482 |
つまり、ファイルは親と子とで独立して閉じることができます; しかし |
483 |
dup() されたハンドルはまだ同じシークポインタを共有していることに |
484 |
注意して下さい。 |
485 |
親でシーク位置を変更するとそれは子にも波及し、その逆も同様です。 |
486 |
これは子供と分離したシークポインタが必要なファイルを開くことで |
487 |
無効にできます。 |
488 |
|
489 |
=begin original |
490 |
|
491 |
On some operating systems, notably Solaris and Unixware, calling C<exit()> |
492 |
from a child process will flush and close open filehandles in the parent, |
493 |
thereby corrupting the filehandles. On these systems, calling C<_exit()> |
494 |
is suggested instead. C<_exit()> is available in Perl through the |
495 |
C<POSIX> module. Please consult your system's manpages for more information |
496 |
on this. |
497 |
|
498 |
=end original |
499 |
|
500 |
いくつかの OS (特に Solaris や Unixware) では、子プロセスから C<exit()> が |
501 |
呼び出されると、親のオープンしているファイルハンドルはフラッシュされて |
502 |
閉じられるので、結果としてファイルハンドルが壊れます。 |
503 |
これらのシステムでは、代わりに C<_exit()> を呼ぶべきです。 |
504 |
C<_exit()> は C<POSIX> モジュールを通して Perl で利用可能です。 |
505 |
これに関するさらなる情報についてはシステムの man ページを参考にしてください。 |
506 |
|
507 |
=item Open directory handles |
508 |
|
509 |
=begin original |
510 |
|
511 |
Perl will completely read from all open directory handles until they |
512 |
reach the end of the stream. It will then seekdir() back to the |
513 |
original location and all future readdir() requests will be fulfilled |
514 |
from the cache buffer. That means that neither the directory handle held |
515 |
by the parent process nor the one held by the child process will see |
516 |
any changes made to the directory after the fork() call. |
517 |
|
518 |
=end original |
519 |
|
520 |
Perl はストリームの末尾に到達するまで、全ての開いている |
521 |
ディレクトリハンドルを完全に読み込みます。 |
522 |
それから seekdir() で元の位置にまで戻って、その後の readdir() 要求は全て |
523 |
キャッシュされたバッファで実行されます。 |
524 |
これは、親プロセスによって保持されていたり子プロセスによって保持されている |
525 |
ディレクトリハンドルは fork() 呼び出しの後に行われたディレクトリの |
526 |
変更は見えないということです。 |
527 |
|
528 |
=begin original |
529 |
|
530 |
Note that rewinddir() has a similar limitation on Windows and will not |
531 |
force readdir() to read the directory again either. Only a newly |
532 |
opened directory handle will reflect changes to the directory. |
533 |
|
534 |
=end original |
535 |
|
536 |
Windows では rewinddir() にも同じような制限があり、readdir() を使っても |
537 |
ディレクトリを再び読むことを強制しないことに注意してください。 |
538 |
新しく開いたディレクトリハンドルのみがディレクトリの変更を反映します。 |
539 |
|
540 |
=item Forking pipe open() not yet implemented |
541 |
|
542 |
(pipe open() の fork はまだ実装されていません) |
543 |
|
544 |
=begin original |
545 |
|
546 |
The C<open(FOO, "|-")> and C<open(BAR, "-|")> constructs are not yet |
547 |
implemented. This limitation can be easily worked around in new code |
548 |
by creating a pipe explicitly. The following example shows how to |
549 |
write to a forked child: |
550 |
|
551 |
=end original |
552 |
|
553 |
C<open(FOO、"|-")> 及び C<open(BAR、"-|")> 構成子は実装されていません。 |
554 |
この制限は明示的にパイプを作る新しいコードで簡単に取り除けます。 |
555 |
以下の例で fork された子に書き出す方法を示します: |
556 |
|
557 |
# simulate open(FOO, "|-") |
558 |
sub pipe_to_fork ($) { |
559 |
my $parent = shift; |
560 |
pipe my $child, $parent or die; |
561 |
my $pid = fork(); |
562 |
die "fork() failed: $!" unless defined $pid; |
563 |
if ($pid) { |
564 |
close $child; |
565 |
} |
566 |
else { |
567 |
close $parent; |
568 |
open(STDIN, "<&=" . fileno($child)) or die; |
569 |
} |
570 |
$pid; |
571 |
} |
572 |
|
573 |
if (pipe_to_fork('FOO')) { |
574 |
# parent |
575 |
print FOO "pipe_to_fork\n"; |
576 |
close FOO; |
577 |
} |
578 |
else { |
579 |
# child |
580 |
while (<STDIN>) { print; } |
581 |
exit(0); |
582 |
} |
583 |
|
584 |
=begin original |
585 |
|
586 |
And this one reads from the child: |
587 |
|
588 |
=end original |
589 |
|
590 |
そしてこちらは子から読む時です: |
591 |
|
592 |
# simulate open(FOO, "-|") |
593 |
sub pipe_from_fork ($) { |
594 |
my $parent = shift; |
595 |
pipe $parent, my $child or die; |
596 |
my $pid = fork(); |
597 |
die "fork() failed: $!" unless defined $pid; |
598 |
if ($pid) { |
599 |
close $child; |
600 |
} |
601 |
else { |
602 |
close $parent; |
603 |
open(STDOUT, ">&=" . fileno($child)) or die; |
604 |
} |
605 |
$pid; |
606 |
} |
607 |
|
608 |
if (pipe_from_fork('BAR')) { |
609 |
# parent |
610 |
while (<BAR>) { print; } |
611 |
close BAR; |
612 |
} |
613 |
else { |
614 |
# child |
615 |
print "pipe_from_fork\n"; |
616 |
exit(0); |
617 |
} |
618 |
|
619 |
=begin original |
620 |
|
621 |
Forking pipe open() constructs will be supported in future. |
622 |
|
623 |
=end original |
624 |
|
625 |
pipe open() の fork は今後実装されるでしょう。 |
626 |
|
627 |
=item Global state maintained by XSUBs |
628 |
|
629 |
(XSUB の保持しているグローバル状態) |
630 |
|
631 |
=begin original |
632 |
|
633 |
External subroutines (XSUBs) that maintain their own global state may |
634 |
not work correctly. Such XSUBs will either need to maintain locks to |
635 |
protect simultaneous access to global data from different pseudo-processes, |
636 |
or maintain all their state on the Perl symbol table, which is copied |
637 |
naturally when fork() is called. A callback mechanism that provides |
638 |
extensions an opportunity to clone their state will be provided in the |
639 |
near future. |
640 |
|
641 |
=end original |
642 |
|
643 |
それ自身でグローバル状態を保持している外部関数(XSUBs; external |
644 |
subroutines)は正しく動作しないでしょう。 |
645 |
そのような XSUB は、異なる仮想プロセスからグローバルデータに対して同時に |
646 |
アクセスするのを防ぐためのロックも保持するが、その全ての状態を |
647 |
fork() 時に自然と複製される Perl シンボルテーブル上に置くかする |
648 |
必要があるでしょう。 |
649 |
拡張に対して複製するタイミングを提供するコールバック機構は近い将来 |
650 |
提供されるでしょう。 |
651 |
|
652 |
=item Interpreter embedded in larger application |
653 |
|
654 |
(大きなアプリケーションに埋め込まれているインタプリタ) |
655 |
|
656 |
=begin original |
657 |
|
658 |
The fork() emulation may not behave as expected when it is executed in an |
659 |
application which embeds a Perl interpreter and calls Perl APIs that can |
660 |
evaluate bits of Perl code. This stems from the fact that the emulation |
661 |
only has knowledge about the Perl interpreter's own data structures and |
662 |
knows nothing about the containing application's state. For example, any |
663 |
state carried on the application's own call stack is out of reach. |
664 |
|
665 |
=end original |
666 |
|
667 |
fork() エミュレーションは Perl インタプリタを埋め込んでいて |
668 |
Perl コードを評価(eval)する Perl API を少しだけ呼び出すような |
669 |
アプリケーションの内部で実行されている時には、予期したように |
670 |
振る舞わないかもしれません。 |
671 |
これは、エミュレーションは Perl インタプリタ自身の持っているデータ構造しか |
672 |
知らず、格納しているアプリケーションの |
673 |
状態に関しては何も知らないために生じます。 |
674 |
例えば、アプリケーションの自分のコールスタックで継続している状態は |
675 |
手の届かないところにあります。 |
676 |
|
677 |
=item Thread-safety of extensions |
678 |
|
679 |
(エクステンションのスレッド安全性) |
680 |
|
681 |
=begin original |
682 |
|
683 |
Since the fork() emulation runs code in multiple threads, extensions |
684 |
calling into non-thread-safe libraries may not work reliably when |
685 |
calling fork(). As Perl's threading support gradually becomes more |
686 |
widely adopted even on platforms with a native fork(), such extensions |
687 |
are expected to be fixed for thread-safety. |
688 |
|
689 |
=end original |
690 |
|
691 |
fork() エミュレーションはコードを複数のスレッドで実行するために、 |
692 |
スレッドセーフでないライブラリを呼び出すエクステンションは fork() を |
693 |
呼び出すと正しく動作しないかもしれません。 |
694 |
Perl のスレッドサポートは徐々にネイティブな fork() を持っている |
695 |
プラットフォームにも広く導入されてきているので、そのようなエクステンションは |
696 |
スレッドセーフに修正するように期待されています。 |
697 |
|
698 |
=back |
699 |
|
700 |
=head1 PORTABILITY CAVEATS |
701 |
|
702 |
=begin original |
703 |
|
704 |
In portable Perl code, C<kill(9, $child)> must not be used on forked processes. |
705 |
Killing a forked process is unsafe and has unpredictable results. |
706 |
See L</kill()>, above. |
707 |
|
708 |
=end original |
709 |
|
710 |
移植性のある Perl コードでは、C<kill(9, $child)> は fork されたプロセスには |
711 |
使ってはいけません。 |
712 |
fork したプロセスを kill することは安全ではなく、予測できない結果を |
713 |
もたらします。 |
714 |
上述の L</kill()> を参照してください。 |
715 |
|
716 |
=head1 BUGS |
717 |
|
718 |
=over 8 |
719 |
|
720 |
=item * |
721 |
|
722 |
=begin original |
723 |
|
724 |
Having pseudo-process IDs be negative integers breaks down for the integer |
725 |
C<-1> because the wait() and waitpid() functions treat this number as |
726 |
being special. The tacit assumption in the current implementation is that |
727 |
the system never allocates a thread ID of C<1> for user threads. A better |
728 |
representation for pseudo-process IDs will be implemented in future. |
729 |
|
730 |
=end original |
731 |
|
732 |
仮想プロセス ID を負の整数値とすることは整数 C<-1> を破壊します; |
733 |
なぜなら wait() や waitpid() といった関数はその値を |
734 |
特殊な物として扱うためです。 |
735 |
現在の実装においては、システムはユーザスレッドに対してスレッド ID C<1> を |
736 |
割り当てることはないと暗黙に仮定しています。 |
737 |
よりよい仮想プロセス ID の表現は今後実装されるでしょう。 |
738 |
|
739 |
=item * |
740 |
|
741 |
=begin original |
742 |
|
743 |
In certain cases, the OS-level handles created by the pipe(), socket(), |
744 |
and accept() operators are apparently not duplicated accurately in |
745 |
pseudo-processes. This only happens in some situations, but where it |
746 |
does happen, it may result in deadlocks between the read and write ends |
747 |
of pipe handles, or inability to send or receive data across socket |
748 |
handles. |
749 |
|
750 |
=end original |
751 |
|
752 |
特定のケースで、pipe()、socket()、そして accept() 演算子によって |
753 |
生成された OS レベルのハンドルは仮想プロセスできちんと |
754 |
複製されないことがあるようです。 |
755 |
これは特定の状況でのみ発生しますが、これが発生する場所では、 |
756 |
パイプハンドルの読み書き間でのデッドロックやソケットハンドルに対する |
757 |
送受信ができないといったことが起こるようです。 |
758 |
|
759 |
=item * |
760 |
|
761 |
=begin original |
762 |
|
763 |
This document may be incomplete in some respects. |
764 |
|
765 |
=end original |
766 |
|
767 |
このドキュメントは何カ所か不完全かもしれません。 |
768 |
|
769 |
=back |
770 |
|
771 |
=head1 AUTHOR |
772 |
|
773 |
=begin original |
774 |
|
775 |
Support for concurrent interpreters and the fork() emulation was implemented |
776 |
by ActiveState, with funding from Microsoft Corporation. |
777 |
|
778 |
=end original |
779 |
|
780 |
並列インタプリタと fork() エミュレーションのサポートは |
781 |
Microsoft Corporation の資金援助で ActiveState によって実装されました。 |
782 |
|
783 |
=begin original |
784 |
|
785 |
This document is authored and maintained by Gurusamy Sarathy |
786 |
E<lt>gsar@activestate.comE<gt>. |
787 |
|
788 |
=end original |
789 |
|
790 |
このドキュメントは Gurusamy Sarathy E<lt>gsar@activestate.comE<gt> |
791 |
によって書かれ、メンテナンスされています。 |
792 |
|
793 |
=head1 SEE ALSO |
794 |
|
795 |
L<perlfunc/"fork">, L<perlipc> |
796 |
|
797 |
=cut |
798 |
|
799 |
=begin meta |
800 |
|
801 |
Translate: 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp> |
802 |
Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-) |
803 |
Status: completed |
804 |
|
805 |
=end meta |
806 |
|