| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = fork child process on CGI = |
| | 4 | * [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)] |
| | 5 | * [http://www.tohoho-web.com/lng/199910/99100140.htm CGI-Perlのforkで子供を置き去りにするには?] |
| | 6 | * [http://www.stackasterisk.jp/tech/program/perl04_02.jsp Perl第4回:PerlTips(バックグラウンドで処理を実行)] |
| | 7 | |
| | 8 | * sample code |
| | 9 | {{{ |
| | 10 | #!perl |
| | 11 | #! /usr/bin/perl -w |
| | 12 | |
| | 13 | my $pid = fork; |
| | 14 | if (! defined $pid) { |
| | 15 | # cannot fork |
| | 16 | die("cannot fork: $!"); |
| | 17 | } |
| | 18 | |
| | 19 | if ($pid) { |
| | 20 | # parent process |
| | 21 | print "Content-type: text/html;\n\n"; |
| | 22 | |
| | 23 | # output something |
| | 24 | print "parent: $$ / child: $pid\n"; |
| | 25 | |
| | 26 | close(STDIN); |
| | 27 | close(STDOUT); |
| | 28 | exit; |
| | 29 | } |
| | 30 | else { |
| | 31 | # child proccess |
| | 32 | close(STDIN); |
| | 33 | close(STDOUT); |
| | 34 | |
| | 35 | # long long proccess ... |
| | 36 | } |
| | 37 | }}} |
| | 38 | * close(STDOUT)を必要と書かれている文献が多いが、手元の環境(下記)ではSTDINも閉じないと親プロセスは終了しなかった。 |
| | 39 | * 親プロセスについてはSTDIN/STDOUTを閉じた時点でhttpdからプロセスが終了させられるので(see [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)])、exit前に何かの処理を行うことは出来ない。 |
| | 40 | * apache2 -V |
| | 41 | {{{ |
| | 42 | Server version: Apache/2.2.14 (Ubuntu) |
| | 43 | Server built: Apr 13 2010 20:22:19 |
| | 44 | Server's Module Magic Number: 20051115:23 |
| | 45 | Server loaded: APR 1.3.8, APR-Util 1.3.9 |
| | 46 | Compiled using: APR 1.3.8, APR-Util 1.3.9 |
| | 47 | Architecture: 64-bit |
| | 48 | Server MPM: Worker |
| | 49 | threaded: yes (fixed thread count) |
| | 50 | forked: yes (variable process count) |
| | 51 | }}} |