Search code examples
phppcntl

Need explanation pcnt_fork()


I am new to pcnt_fork,

<?php
  $pid = pcntl_fork();
  echo $pid;
  if($pid) {
  // parent process runs what is here
  echo "-parent\n";
   }
  else {
  // child process runs what is here
  echo "-child\n";
  } ?>

ouptut : 25347-parent 0-child

my question is , here $pid will be the process id of the child process i.e 25347 right? then it should echo only parent then why its also giving 0-child as a output...can anyone explain


Solution

  • This is what the state looks like after a fork operation:

               fork()
          +------+-----+
        child    |   parent
    -------------+------------
    > echo $pid  | > echo $pid
    0            | 4650