pcntl_fork suport instaled but not working correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gr4v3n
    New Member
    • Sep 2010
    • 3

    pcntl_fork suport instaled but not working correctly

    Greetings.
    First of all I must apologyse for my english.

    I have used multi processing before in C but know I have a project that it would be usefull to use it in php.

    Here comes the problema I installed php support for pcntl_fork and when running a simple test program It does not work correctly.


    Code:
     $pid = pcntl_fork();
      if ($pid != -1) {
        if ($pid) {
          print "In the parent: child PID is $pid\n";
        } else {
          print "In the child\n";
        }
      } else {
        echo "Fork failed!\n";
      }

    the problem is only one of the print appears on screen.
    When I reload the page sometimes I get In the parent and sometimes in the child.

    I would be most thankfull I someone could help me

    Regards
    Last edited by Niheel; Sep 18 '10, 10:31 PM.
  • Rizladonovich
    New Member
    • Sep 2010
    • 13

    #2
    Well, your control-flow does not allow more than one statement to be printed, as is correct.

    Control Structures



    boolean casting

    And as the documentation says regarding pcntl_fork: "On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised."
    Last edited by Rizladonovich; Sep 19 '10, 06:36 AM. Reason: specified last citation to be about pcntl

    Comment

    • gr4v3n
      New Member
      • Sep 2010
      • 3

      #3
      Code:
      Rizladonovich      Well, your control-flow does not allow more than one statement to be printed, as is correct.
      I'm sorry I'm kind of a new but what does this mean? In the browser only one of them writes but php in cli both write like when I used C. Is this a problem because of the browser. My idea was to open a new page, fork the process, terminate the parent and let the child live as a deamon in order to keep aquiring data from serial port connected to a master that is connected to a fieldbus rs485 with several slaves connected to it.

      I'm now instaling php to run as cgi instead of apache module because I think the problem comes from the fact that when I fork a new apache process is created and that was not what I intended.

      I someone could give me a sugestion I would be most thankfull

      Regards

      Comment

      • Rizladonovich
        New Member
        • Sep 2010
        • 13

        #4
        I do not understand how you expect both

        "In the parent: child PID is $pid\n";
        AND
        "In the child\n";

        to be printed in same execution. :?

        It has nothing to do with the browser.

        Did you read on the links i added?
        I.e. control-structures else

        Code:
        /* 1. assign PID of the child process to variable $pid */
        $pid = pcntl_fork();
        /* 2.a IF $pid is not -1, - this means success */
        if ($pid != -1) {
            /* 2.a.I  IF $pid is not zero, - this means parent */
            if ($pid) {
                /* Print text+PID */
                print "In the parent: child PID is $pid\n";
            /* 2.a.II ELSE IF $pid is 0 (zero), - this means child */
            } else {
                /* Print message */
                print "In the child\n";
            }
        /* 3. IF $pid is -1, - this means fork failed */
        } else {
            /* 3.a Print message */
            echo "Fork failed!\n";
        }
        Same as:

        Code:
        IF FORK SUCCEEDED
            IF PID IS NON-ZERO
                Print information about pid
            ELSE
                Print information that we are child
            END IF
        ELSE IF FORK FAILED
            Print information that fork failed.
        END IF
        This is no different from C syntax.
        You can't ever get php to execute statements both in IF and ELSE, of inner iff.

        This has the same logic, :

        Code:
        IF fork succeeded AND PID is NON-ZERO
            Print information about pid
        ELSE IF fork succeeded AND PID is ZERO
            Print information that we are child
        ELSE IF fork FAILED
            Print information that fork failed.
        END IF

        Comment

        • gr4v3n
          New Member
          • Sep 2010
          • 3

          #5
          LOL. I know what an if else clause do. But in this case both should print. When fork is called the process divides in two processes. The first process is the parent and so it prints when IF clause(because $pid in this case will be the pid of the child so it's bigger thank zero. The second is the child so it prints when ELSE clause(the $pid in this process is 0). They both print, that program is a simple test to multiprocessing .

          Anyway I got it to work. I had php installed as apache module so when I fork it created a new apache process with umpredictable results. I downloaded de bynary, compiled and installed php as cgi and know it works.

          Code:
          $pid = pcntl_fork();
          if ($pid == -1) {
               die('could not fork');
          } else if ($pid) {
               echo "we are the parent";
               pcntl_wait($status); //Protect against Zombie children
          } else {
               echo "we are the child";
          }
          This code is more or less the same than the first (a simple fork test)

          OUTPUT:we are the child X-Powered-By: PHP/5.3.3 Content-type: text/html we are the parent

          Now I have a bug with X-powered bla bla beeing printed but I'll work my way aroud and solve it. Anyay thank you for trying.

          Regards :)

          Comment

          Working...