Changes between Initial Version and Version 1 of Dev/Perl/example


Ignore:
Timestamp:
Jan 12, 2011 2:48:23 PM (13 years ago)
Author:
mitty
Comment:

moved from ../

Legend:

Unmodified
Added
Removed
Modified
  • Dev/Perl/example

    v1 v1  
     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   * Apache2 mpm-worker + Perl 5.10 on x64 
     10{{{ 
     11#!perl 
     12#! /usr/bin/perl -w 
     13 
     14my $pid = fork; 
     15if (! defined $pid) { 
     16    # cannot fork 
     17    die("cannot fork: $!"); 
     18} 
     19 
     20if ($pid) { 
     21    # parent process 
     22    print "Content-type: text/html;\n\n"; 
     23     
     24    # output something 
     25    print "parent: $$ / child: $pid\n"; 
     26     
     27    close(STDIN); 
     28    close(STDOUT); 
     29    exit; 
     30} 
     31else { 
     32    # child proccess 
     33    close(STDIN); 
     34    close(STDOUT); 
     35     
     36    # long long proccess ... 
     37    sleep(60); 
     38} 
     39}}} 
     40   * Apache2 mpm-prefork + Perl 5.10 on x64 
     41{{{ 
     42#!perl 
     43#! /usr/bin/perl -w 
     44 
     45my $pid = fork; 
     46if (! defined $pid) { 
     47    # cannot fork 
     48    die("cannot fork: $!"); 
     49} 
     50 
     51if ($pid) { 
     52    # parent process 
     53    print "Content-type: text/html;\n\n"; 
     54 
     55    print "parent: $$ / child: $pid\n"; 
     56} 
     57else { 
     58    # child proccess 
     59    close(STDOUT); 
     60 
     61    sleep(60); 
     62} 
     63}}} 
     64   * 子プロセスでclose(STDOUT)が必要とだけ書かれている文献が多いが、Apache2 mpm-worker環境では親子ともSTDINも閉じないと親プロセスは終了しなかった。 
     65   * しかし、テストのため一度mpm-preforkに切り替えた後、再度mpm-workerに戻した後は何故かpreforkと同じ挙動(STDINを閉じなくてもOK)を示すようになった。 
     66     * apache2 -V 
     67{{{ 
     68Server version: Apache/2.2.14 (Ubuntu) 
     69Server built:   Apr 13 2010 20:22:19 
     70Server's Module Magic Number: 20051115:23 
     71Server loaded:  APR 1.3.8, APR-Util 1.3.9 
     72Compiled using: APR 1.3.8, APR-Util 1.3.9 
     73Architecture:   64-bit 
     74Server MPM:     Worker 
     75  threaded:     yes (fixed thread count) 
     76    forked:     yes (variable process count) 
     77}}} 
     78   * Apache2 mpm-preforkでは子プロセスでclose(STDOUT)するだけで良かった。 
     79     * apache2 -V 
     80{{{ 
     81Server version: Apache/2.2.14 (Ubuntu) 
     82Server built:   Apr 13 2010 20:21:26 
     83Server's Module Magic Number: 20051115:23 
     84Server loaded:  APR 1.3.8, APR-Util 1.3.9 
     85Compiled using: APR 1.3.8, APR-Util 1.3.9 
     86Architecture:   64-bit 
     87Server MPM:     Prefork 
     88  threaded:     no 
     89    forked:     yes (variable process count) 
     90}}} 
     91   * 親プロセスについてはSTDIN/STDOUTを閉じた時点でhttpdからプロセスが終了させられるので(see [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)])、exit前に何かの処理を行うことは出来ない。 
     92   * mpm-preforkでは子プロセスが終了するまで、親プロセスはゾンビとして残った。mpm-workerではそのようなことはない模様(こちらも、一度mpm-preforkに切り替えると、workerに戻してもゾンビが残るようになった)。 
     93     * ps aux | grep www-data 
     94{{{ 
     95www-data  3767  0.0  0.0      0     0 ?        Z    15:28   0:00 [fork.cgi] <defunct> 
     96www-data  3768  0.0  0.1  16700   668 ?        S    15:28   0:00 /usr/bin/perl -w /var/www/archive/fork.cgi 
     97}}} 
     98     * ただし、ゾンビとなった親プロセスは入出力がないため、しばらくするとhttpdによって殺される(see [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)])。 
     99 
     100 = making files with random name and random content = 
     101 * [http://d.hatena.ne.jp/perlcodesample/20100413/1270894115 File::Temp - 一時ファイルの作成 / Perlモジュール徹底解説 - サンプルコードによるPerl入門] 
     102 * [http://d.hatena.ne.jp/fbis/20080114/1200307393 ランダムな文字列を生成するString::Random - Unknown::Programming] 
     103 
     104 * makerandom.pl 
     105{{{ 
     106#!perl 
     107#! /usr/bin/perl -w 
     108 
     109use strict; 
     110use warnings; 
     111 
     112use File::Temp qw(tempfile); 
     113use String::Random; 
     114 
     115my $filenum = $ARGV[0]; 
     116 
     117while ($filenum-- > 0) { 
     118    my ($fh, $fname) = tempfile(DIR => '.'); 
     119     
     120    my $length = int(rand(100)); 
     121    print "making $fname ...\n"; 
     122    print $fh String::Random->new->randregex("[A-Za-z0-9]{$length}"); 
     123} 
     124}}} 
     125 
     126 = $line = <$hash{'filehandle'}>; # => GLOB = 
     127 * [http://harapeko.asablo.jp/blog/2006/10/13/559447 山カッコ演算子にハッシュの要素を使用することはできない: 国民宿舎はらぺこ 大浴場] 
     128{{{ 
     129#!perl 
     130my %file = (); 
     131 
     132{ 
     133    open my $fh, '<test.txt'    or die; 
     134    $file{'fh'} = $fh; 
     135} 
     136 
     137my $line = <$file{'fh'}>;   # NG 
     138print $line; 
     139}}} 
     140 * 実行結果 
     141{{{ 
     142GLOB(0x161aa34)my %file = (); 
     143 
     144}}}