php posix_getppid,posix_getppid()

posix_getppid()

(PHP 4, PHP 5, PHP 7)

Return the parent process identifier

说明posix_getppid(void):int

Return the process identifier of the parent process of the current process.

返回值

Returns the identifier, as an integer.

范例

Example use of posix_getppid()<?php

echo posix_getppid(); //8259

?>answering to http://php.net/manual/en/function.posix-getppid.php#120088

if you're using proc_open and you want to get the pid of the calling php-process, use proc open with usage "exec php $scriptname". In that case, you can get the real parent-id, because php doesn't start a new shell.WARNING, when using proc_open, your child will NOT get the parent php process's PID by using posix_getppid() , it will actually get the pid of a shell ( /bin/sh ? ) started by php, which in turn starts the child. i don't currently have a good workaround for this, but i'm working around it by having the parent write it's pid to a file and having the chidren read the fileposix_getppid will not work on Windows.

Here's an alternative:

if(strncasecmp(PHP_OS, "win", 3) == 0) {

$pid = getmypid(); // child process ID

$parent_pid = shell_exec("wmic process where (processid=$pid) get parentprocessid");

$parent_pid = explode("\n", $parent_pid);

$parent_pid = intval($parent_pid[1]);

echo "Child:$pidParent:$parent_pid\n";

}

?>when using proc_open, the child's ppid is NOT your php script, but rather a shell started by proc_open's pid. to get your pid, your proc_open child needs to get the parent's parent's (aka grandparent's) pid. here's a way to do that in PHP on linux:

function posix_getpppid(): int {

return ( int ) (explode ( " ", file_get_contents ( "/proc/" . posix_getppid () . "/stat" ) ) [3]);

}

(thanks to TML @ irc.freenode.net/##PHP for the technique)

(i said most of this in a previous comment, but i didn't have a good solution for it. now i have. if someone with the privileges read this, can you delete this notice and my previous comment please?)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值