トップ «前の日(05-01) 最新 次の日(05-03)»

PC関係のメモ


PHP 5.0.4 install / fork / wait

PHP 5.0.4 install

pcntlモジュールを使いたかったのでついでに最新版のPHP5を落としてきてインストール。

./configure  --with-apxs2=/usr/local/apache2/bin/apxs \
             --enable-mbstring \
             --enable-zend-multibyte \
             --with-zlib \
             --enable-pcntl

fork

forkを使ったことが無いので勉強中。

PHPのpcntlモジュールは基本的にUNIXのシステムコールと同じ使い方のようなので、UNIXのシステムコールのサンプル等を見ながら。

あと、http://www.php.net/manual/ja/function.pcntl-fork.phpに投稿されているサンプルを参考に。

スタイルを変更してコメントを追記しただけでほとんど転載。

#!/usr/local/bin/php
<?php

class Process
{
    var $ident;

    function Process($ident){
        $this->ident = $ident;
    }

    function go(){
        for($i = 1; $i < 10; $i++){
            sleep(1);
            echo $this->ident . $i . "\n";
        }
    }
}

$childrenTotal = 3;
$childrenNow = 0;
$childrenNames = array( 'a', 'b', 'c' );

while($childrenNow < $childrenTotal){
    $pid = pcntl_fork();

    if($pid == -1) {
        die("error\n");
    }elseif($pid == 0){
        // 子プロセス
        $childrenNow++;
    }else{
        // 親プロセス
        $process = new Process($childrenNames[$childrenNow]);
        $process->go();
        die();
    }
}

?>

なんだけど、これって子プロセスを生成して自身はdie()して、さらにその子プロセスが子プロセスを生成して自身はdie()...ってなってるように見えるだけどそういうもの?

...
while($childrenNow < $childrenTotal){
    $pid = pcntl_fork();

    if($pid == -1) {
        die("error\n");
    }elseif($pid == 0){
        // 子プロセス
        $process = new Process($childrenNames[$childrenNow]);
        $process->go();
        die();
    }
    // 親プロセス
    $childrenNow++;
}

?>

で、いいと思うんだけど。

後、最後に子プロセスの終了を待って処理を行うようにするにはwaitすればいいのかな?

// 子プロセスの終了を待つ
pcntl_wait($status);
print("end\n");

?>

wait

さっきのwaitは子プロセス分必要だった。

// 生成した子プロセスの終了を待つ
$childrenNow = 0;

while($childrenNow < $childrenTotal){
    pcntl_wait($status, WUNTRACED);
    $childrenNow++;
}

print("end\n");

さっきのだと実行結果はこう

$ ./pcntl.php
a1
b1
c1
a2
b2
c2
...
a8
c8
b8
b9
end  <- ここで出てる
c9
a9

修正後

$ ./pcntl.php
a1
b1
c1
a2
b2
c2
...
a8
c8
b8
b9
c9
a9
end  <- ちゃんと最後に出た

無事最後にendが出た。

それから、CLI版だと出力バッファリングはないはずだけど念のため最初にob_end_flush()を入れておく。

最終更新時刻: 2008年11月09日

広島〜四国〜神戸 / [buy][camera]Canon バッテリーグリップ BG-E3

広島〜四国〜神戸

Canon EOS Kiss Digital N TAMRON AF18-200mm F/3.5-6.3 XR Di II

原爆ドーム

尾道

瀬戸大橋

摩耶山からの夜景

広島(1枚目の写真)を出発して、尾道(2枚目の写真)に立ち寄り、瀬戸大橋(3枚目の写真)を渡って讃岐うどんで舌鼓。最後に日本三大夜景の一つ、神戸は摩耶山からの夜景(4枚目の写真)を堪能。贅沢な一日でした。

参考サイト

Canon バッテリーグリップ BG-E3

Canon バッテリーグリップ BG-E3

前回の撮影(鹿児島で約400枚/2日)以来、充電しないまま広島に来てしまったため数枚撮った時点で電池切れ。

しかし、こういう事態(「電池は使う前に充電」→「突発的な旅行で電池切れ」)は想定済み。EOS Kiss Digital Nには単三電池が使用できるオプション(BG-E3)があるのだ。というわけで、原爆ドームから歩いて5分程度のコンプマートにて無事購入。

良かった、店頭在庫があって。

Tags: buy camera
最終更新時刻: 2008年10月30日
コメント(3) [コメントを投稿する]

_ aaaa [http://www.mylab.jp/diary/20060502.html ]

_ hajime [(・ω・ )?]

_ Gabriel  [http://1test1.com/ http://1test2.com/ http://1test3.com/ h..]


VS2010で作ったバイナリはWinXPSP1以下では動かない

http://rararahp.cool.ne.jp/cgi-bin/lng/vc/vclng.cgi?print+201011/10110008.txt

http://www.ailight.jp/Blog/sha256/archive/2011/4/22/%E5%90%84VC%E4%BB%98%E5%B1%9E%E3%81%AECRT%E3%81%AEOS%E3%82%B5%E3%83%9D%E3%83%BC%E3%83%88%E7%8A%B6%E6%B3%81%20-%20Visual%20C

各VC付属のCRTのOSサポート状況をとりまとめました。

■Visual Studio .NET 2003
Windows 98, Windows Me,
Windows NT, Windows 2000, and Windows XP

■Visual Studio 2005
Windows 98, Windows Me,
Windows NT, Windows 2000, Windows XP, and Windows Server 2003

■Visual Studio 2008
Windows 9x のサポートを廃止
Windows 2000, Windows XP, Windows Server 2003, and Windows Vista

■Visual Studio 2010
Windows XP with SP2, Windows XP with SP3, Windows Server 2003 with SP1, Windows Server 2003 with SP2, Windows Vista, Windows Server 2008, and Windows 7

http://www9.plala.or.jp/oga/vs2010.html

最終更新時刻: 2011年05月02日