PHP̱ʵ

Ͷ壺junjie [/a> ] ת ʱ䣺2014-10-15 Ҫ

ƪҪHP̱ʵ,LinuxʵPHP̱,ҪѿԲο

ĽûPHPǿԿӰ־һôƪһPHP̵ĻԼſƣʱνн

1. ׼

֮ǰȷIJM$ Windowsƽ̨ΪûWindowsLinux / BSD / UnixӦöûġȷ˹ҪHPģǷСն


$ php -m

鲢ǰPHPչһpcntlosixбС

1.1. pcntl

Ҳpcntl˳DZʱչȥһ밲װHPôҪ밲װPHPü-enable-pcntl


$ cd /path/to/php_source_code_dir
$ ./configure [some other options] --enable-pcntl
$ make && make install

1.2. posix

ͻֻҪʱ-disable-posix

2. Ԥ֪ʶ

ǰҪinux˽⡣أɸӰ㲻ͬ󣬱6ȡһӰֳЩ16˶ǸճҲͻǽпȻ󣬲һĵطDZ˸Ե£֮䲻֪ʲôȻ񶯻Ƭһ۾֮нȻֻ16֮ǰͬļ䡣

ͬѧ˵ⲻӵûӰȥһ

֪ʶǴ˽ӽô¡ĴȫһģһĶӰǰִݡ

3. Ӱ

û֪ʶôأ򿪾ȿһʣfork

3.1. fork

ӣǷֲһҪpcntl_fork()ȼ򵥿һPHPܡһHPű


$pid = pcntl_fork(); // һóɹͱЩͬ
if ($pid == -1) {
    die('fork failed');
} else if ($pid == 0) {
} else {
}

pcntl_fork()һӽ͸ΨһIDIDPIDIDͬ²鿴psmanpsôman psΪ-1forkʧˡifǰ䣺echo $pid . PHP_EOL;Ľű˵͸Ĵģ

67789 # Ǹbr /> 0     # ӽbr />

pcntl_fork()óɹڸ᷵ӽIDӽصֱfƸӽͬ¡

3.2. /strong>

Ȼ˵˵16ԭͷ


$parentPid = getmypid(); // Ǵ˵16֮ǰļbr /> $pid = pcntl_fork(); // һóɹͱЩͬ
if ($pid == -1) {
    die('fork failed');
} else if ($pid == 0) {
    $mypid = getmypid(); // getmypid()ǰID
    echo 'I am child process. My PID is ' . $mypid . ' and my father's PID is ' . $parentPid . PHP_EOL;
} else {
    echo 'Oh my god! I am a father now! My child's PID is ' . $pid . ' and mine is ' . $parentPid . PHP_EOL;
}

br />

Oh my god! I am a father now! My child's PID is 68066 and mine is 68065
I am child process. My PID is 68066 and my father's PID is 68065

ǿpcntl_fork()óɹһõpidֵ0һõpidIDķУ$pidֵIJͬͬĴ롣Ĵġ֧ͨ䲻ͬ/p>

3.3. /strong>

ոan psôһs auxrepŵĺһSTATÿ̵̬״̬ZʬZombieȸκδӽɽʬOopsӰһԺʧӽ˻һڣֱȻռʲô棬ܰԺһŵĽʬôֵֹġǻռPID

һ˵ǰչҵͿˡpcntlչһcntl_wait()ὫһӽֹӽʬĻءӽҪգû

3.4. /strong>

ȹ죿ᷢʲôʲôҲᷢɻӽᱻŽ1ŽЩļ̸1ŽõشЩ1ŽԶһʬ취رǵĸ

4. /strong>

һ¶ˣźͳȷʵһҪĶžźŵƣźŵƣӦ˵˰ûʲôӦ Ctrl+C رϣжϵźţSIGINTʱȥӦkill [PID]δκĻյһIGTERMšյźŵִôпܸıΪܰ

4.1. עź/strong>

ֻҪѭĹʼź趨õĺpcntl_signal()ǰΪɶPHPأγ򽫸IGINTΪעã


// һյSIGINTźϢ
function signalHandler($signal) {
    if ($signal == SIGINT) {
        echo 'signal received' . PHP_EOL;
    }
}
// ᣺յSIGINTignalHandler()
pcntl_signal(SIGINT, 'signalHandler');
while (true) {
    sleep(1);
    // do something
    pcntl_signal_dispatch(); // յsignalHandler()
}

ִһʱ Ctrl+C ᷢʲô

4.2. ŷַ

˵һpcntl_signal()עźźյŲ䴦cntl_signal_dispatch()/ do something룺


for ($i = 0; $i < 1000000; $i++) {
    echo $i . PHP_EOL;
    usleep(100000);
}

ֵͣ԰ Ctrl+C ôӦʲôûܶ˸CֱͣΪֱûִpcntl_signal_dispatch()ͲûignalHandler()ignal received

4.3. 汾

濴PHPᷢpcntl_signal_dispatch()HP 5.3ֵģPHP汾.3ʹźŴ5.3İ汾Ҫעźǰ䣺declare(ticks = 1);ÿִһͼָͼһźţ⵽עţ͵źŴˬģֱָĵطһͺá

4.4. ܽʬ

صյϣ= ="ӽˣˣܺܳһ䲻˳Ӵʱɡ˾ˣĵһ˭أһPPIDˡȻں˸PIḌҲǽʬĸһźţSIGCHLDȻôʾһpcntl_wait()

4.5. /strong>

ϣոanillʵ̷ţPHPҲԵosix_kill()ͬڸӽˡڸǰرӽôforkڸIDǰθӽźżɡ

5. ʵ/strong>

PHPĶ̸ͦģĻҲС඼ôпյĻдһһ

1.16˷Ӱֳ
2.ÿ1030ÿʲô
3.֤ԭܵ󿪶һ֤
4.ʹnohupԭ˹رպܹ
5.ѷ5дļţԿSIGUSR1IGUSR2ʱԭ
6.ˣرռֳ/p>

ʾ

1.whileѭ֤עsleep100%PUռ
2.ն˱رһIGHUPbr /> 3.arse_ini_file()NI

/a> - /a> - ϵ - - վ - Ͷ - Ͷ/a>

©CopyRight 2006-2017 JB51.Net Inc All Rights Reserved. ű֮


此为网页(www.jb51.net)快照,查看来源