php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74685 signal listion is wrong
Submitted: 2017-06-01 09:34 UTC Modified: 2017-08-21 08:21 UTC
From: shan dot liu at msn dot com Assigned:
Status: Closed Package: PCNTL related
PHP Version: 7.1.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: shan dot liu at msn dot com
New email:
PHP Version: OS:

 

 [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;		
	}
}
?>



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-01 09:40 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-06-01 09:40 UTC] requinix@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


signal_pro won't fire if you kill the parent, and when you kill the child posix_getpid will return the PID of the current process (which is the parent).
What do you think it should do?
 [2017-06-01 09:42 UTC] requinix@php.net
-Status: Feedback +Status: Open
 [2017-06-01 09:42 UTC] requinix@php.net
Oh, wait, I misread that.
 [2017-06-01 10:40 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-06-01 10:40 UTC] requinix@php.net
> kill parent id,not output
> kill child id,is output:parent id
I think you have that backwards. Are you sure you were killing the right processes?

Here's a test script I just wrote: https://pastebin.com/vwGiFYGT
It all seems to be working correctly for me.
 [2017-06-02 01:17 UTC] shan dot liu at msn dot com
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 sigterm
 [2017-06-02 01:20 UTC] shan dot liu at msn dot com
-Status: Feedback +Status: Open -PHP Version: 7.1.5 +PHP Version: 7.1.3
 [2017-06-02 01:20 UTC] shan dot liu at msn dot com
PHP 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 sigterm
 [2017-06-02 01:23 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-06-02 01:23 UTC] requinix@php.net
You're misunderstanding what is happening. Change the child's sleep to 10 seconds, run, kill the parent, and DON'T kill the child. Just wait.
 [2017-06-02 01:41 UTC] shan dot liu at msn dot com
Why the child process exit before parent process on receiving the signal?
 [2017-06-02 02:06 UTC] shan dot liu at msn dot com
i 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 execute
 [2017-06-02 02:12 UTC] shan dot liu at msn dot com
-Status: Feedback +Status: Open
 [2017-06-02 02:12 UTC] shan dot liu at msn dot com
i 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
 [2017-08-21 08:21 UTC] shan dot liu at msn dot com
-Status: Open +Status: Closed
 [2017-08-21 08:21 UTC] shan dot liu at msn dot com
may be not a bug...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC