|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-06-01 09:34 UTC] shan dot liu at msn dot com
Description:
------------
//
<?php
if(pcntl_fork())die();
$fp = pcntl_fork();
if (!$fp) {
//child
sleep(300);
exit;
}
//parent
function signal_pro($signo){
echo posix_getpid();
};
pcntl_signal(SIGTERM,'signal_pro');
declare(ticks = 1){
while (true){
$pid=pcntl_wait($status);
if($pid==-1)break;
}
}
?>
//linux shell
kill parent id,not output
kill child id,is output:parent id
signal listen is wong???
Test script:
---------------
<?php
if(pcntl_fork())die();
$fp = pcntl_fork();
if (!$fp) {
//child
sleep(300);
exit;
}
//parent
function signal_pro($signo){
echo posix_getpid();
};
pcntl_signal(SIGTERM,'signal_pro');
declare(ticks = 1){
while (true){
$pid=pcntl_wait($status);
if($pid==-1)break;
}
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 23:00:01 2025 UTC |
pcntl_fork() && exit; $parent = posix_getpid(); if ($child = pcntl_fork()) { echo "parent: parent is " . posix_getpid() . "\n"; echo "parent: child is {$child}\n"; echo "parent running\n"; } else { echo "child running\n"; sleep(1000); //child processes not register signal exit;//child processes stop here } //only parent run this code... pcntl_signal(SIGTERM, function() use ($child) { echo $child ? "parent sigterm\n" : "child sigterm\n"; }); declare(ticks = 1){ while (true){ //on term kill parent processes ,no response //but i kill child processes,parent processes receive a term signal,why? $pid=pcntl_wait($status); if($pid==-1)break; } } // [root@rabbitmq-node02 gs]# php -v // PHP 7.1.3 (cli) (built: Mar 21 2017 10:38:32) ( NTS ) // Copyright (c) 1997-2017 The PHP Group // Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies // with Zend OPcache v7.1.3, Copyright (c) 1999-2017, by Zend Technologies // [root@rabbitmq-node02 gs]# php tt.php // parent: parent is 77506 // parent: child is 77507 // parent running // child running // [root@rabbitmq-node02 gs]# kill 77506 // kill parent processes,No output // [root@rabbitmq-node02 gs]# kill 77507 //kill child processes,parent processes receive a term signal,why,child not register signal function // [root@rabbitmq-node02 gs]# parent sigtermPHP CODE: <?php pcntl_fork() && exit; $parent = posix_getpid(); if ($child = pcntl_fork()) { echo "parent: parent is " . posix_getpid() . "\n"; echo "parent: child is {$child}\n"; echo "parent running\n"; } else { echo "child running\n"; sleep(1000); //child processes not register signal exit;//child processes stop here } //only parent run this code... pcntl_signal(SIGTERM, function() use ($child) { echo $child ? "parent sigterm\n" : "child sigterm\n"; }); declare(ticks = 1){ while (true){ //on term kill parent processes ,no response //but i kill child processes,parent processes receive a term signal,why? $pid=pcntl_wait($status); if($pid==-1)break; } } ?> // test history... // [root@rabbitmq-node02 gs]# php -v // PHP 7.1.3 (cli) (built: Mar 21 2017 10:38:32) ( NTS ) // Copyright (c) 1997-2017 The PHP Group // Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies // with Zend OPcache v7.1.3, Copyright (c) 1999-2017, by Zend Technologies // [root@rabbitmq-node02 gs]# php tt.php // parent: parent is 77506 // parent: child is 77507 // parent running // child running // [root@rabbitmq-node02 gs]# kill 77506 // kill parent processes,No output // [root@rabbitmq-node02 gs]# kill 77507 //kill child processes,parent processes receive a term signal,why,child not register signal function // [root@rabbitmq-node02 gs]# parent sigtermi try c code,is normal: ```c #include <stdio.h> #include <stdlib.h> #include <signal.h> void signal_test(int signal_num){ printf("%d",getpid()); fflush(stdout); } int main(){ if(!fork()){ sleep(100); return EXIT_SUCCESS; } signal(SIGTERM,signal_test); int t,c; while(1){ c=wait(t); if(c==-1)break; } return EXIT_SUCCESS; } ``` on term: [root@rabbitmq-node02 gs]# gcc t.c -o t [root@rabbitmq-node02 gs]# ./t & [1] 77776 [root@rabbitmq-node02 gs]# kill 77776 //kill parent processes is output.. 77776[root@rabbitmq-node02 gs]# //why php is child all exit,The signal function begins to executei try c code,is work: ```c #include <stdio.h> #include <stdlib.h> #include <signal.h> void signal_test(int signal_num){ printf("%d",getpid()); fflush(stdout); } int main(){ if(!fork()){//child sleep(100); return EXIT_SUCCESS; } //on parent process register signal function signal(SIGTERM,signal_test); int t,c; while(1){ c=wait(t); if(c==-1)break; } return EXIT_SUCCESS; } ``` on term: [root@rabbitmq-node02 gs]# gcc t.c -o t [root@rabbitmq-node02 gs]# ./t & [1] 77776 [root@rabbitmq-node02 gs]# kill 77776 //kill parent processes is output.. 77776[root@rabbitmq-node02 gs]# //why php is child all exit,The signal function begins to execute