Last active
December 14, 2017 10:16
-
-
Save marinsagovac/10f29be99f471dc40155 to your computer and use it in GitHub Desktop.
PCNTL fork process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pid = pcntl_fork(); | |
if ($pid == -1) { | |
die("could not fork"); | |
} else if ($pid) { | |
exit(); // we are the parent | |
} else { | |
// we are the child | |
} | |
if (posix_setsid() == -1) { | |
die("could not detach from terminal"); | |
} | |
pcntl_signal(SIGTERM, "sig_handler"); | |
pcntl_signal(SIGHUP, "sig_handler"); | |
while (1) | |
{ | |
} | |
function sig_handler($signo) | |
{ | |
switch ($signo) { | |
case SIGTERM: | |
// handle shutdown tasks | |
exit; | |
break; | |
case SIGHUP: | |
// handle restart tasks | |
break; | |
default: | |
// handle all other signals | |
echo time(); | |
} | |
} | |
var_dump($pid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment