ConFoo 2025

posix_getppid

(PHP 4, PHP 5, PHP 7, PHP 8)

posix_getppidRetorna o identificador do processo pai

Descrição

posix_getppid(): int

Retorna o identificador do processo pai do processo atual.

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Retorna o identificador, como um int.

Exemplos

Exemplo #1 Exemplo de uso de posix_getppid()

<?php
echo posix_getppid(); // 8259
?>

adicione uma nota

Notas Enviadas por Usuários (em inglês) 4 notes

up
7
mail at spacerat dot net
7 years ago
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.
up
3
divinity76 at gmail dot com
8 years ago
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 file
up
2
Avital Yachin
6 years ago
posix_getppid will not work on Windows.
Here's an alternative:

<?php
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: $pid Parent: $parent_pid\n";
}
?>
up
1
divinity76 at gmail dot com
7 years ago
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?)
To Top