what is the difference between pcntl_exec and exec in php?
Asked Answered
N

1

7

I've read the docs at https://www.php.net/manual/en/function.pcntl-exec.php and http://php.net/manual/en/function.exec.php but I can't really tell what the actual difference is.

Noway answered 29/1, 2018 at 17:55 Comment(0)
O
3

The pcntl_exec() function works exactly like the standard (unix-style) exec() function. It differs from the regular PHP exec() function in that the process calling the pcntl_exec() is replaced with the process that gets called. This is the ideal method for creating children

. In a simple example (that does no error checking):

switch (pcntl_fork()) {
  case 0:
    $cmd = "/path/to/command";
    $args = array("arg1", "arg2");
    pcntl_exec($cmd, $args);
    // the child will only reach this point on exec failure,
    // because execution shifts to the pcntl_exec()ed command
    exit(0);
  default:
    break;
}

// parent continues
echo "I am the parent";

Referred from comments here: https://www.php.net/manual/en/function.pcntl-exec.php

Only answered 29/1, 2018 at 18:6 Comment(2)
What we'd really like to understand is what are the relative advantages and disadvantages of exec and pcntl_exec.Pinnacle
@Pinnacle After doing pcntl_exec, it seems the php script doesn't continue, because control was passed to the pcntl_exec process. I tried putting 2 calls to pcntl_exec one after the other in a single php file, only the first one is called, control is handed over, and it ends there.Leandra

© 2022 - 2024 — McMap. All rights reserved.