Can somebody explain fork clearly to me?

Discussion in 'Perl Misc' started by Steve, Feb 24, 2010.

  1. Steve

    Steve Guest

    As it clearly states on oreilly's site:
    "The fork command actually creates a child process, and returns the
    PID of the process to the parent, and a value of zero to the child"

    My brain is having an implosion trying to understand code such as
    this:
    1. unless(fork)
    2. if ($pid = fork)

    that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
    this, else to this.
    Steve, Feb 24, 2010
    #1
    1. Advertising

  2. Steve <> wrote:
    >As it clearly states on oreilly's site:
    >"The fork command actually creates a child process, and returns the
    >PID of the process to the parent, and a value of zero to the child"
    >
    >My brain is having an implosion trying to understand code such as
    >this:
    >1. unless(fork)
    >2. if ($pid = fork)
    >
    >that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
    >this, else to this.


    Has nothing to do with fork() but with boolean values in Perl or
    actually with scalars. In Perl any scalar can be used in many different
    capacities and has simultaneously at least a string, a numerical, and a
    boolean value.
    The boolean value of a number is false if and only if that number is 0.
    Therefore ""unless 6124" is the same as "unless <true>". (*)

    It is a convenient short-hand. If you prefer you can replace the "fork"
    command with "fork != 0" and you will get exactly the same behaviour.

    jue

    *<true> indicating the true value, not the string '<true>', which on the
    other hand is a true value itself.
    Jürgen Exner, Feb 24, 2010
    #2
    1. Advertising

  3. Steve

    Willem Guest

    Steve wrote:
    ) As it clearly states on oreilly's site:
    ) "The fork command actually creates a child process, and returns the
    ) PID of the process to the parent, and a value of zero to the child"
    )
    ) My brain is having an implosion trying to understand code such as
    ) this:
    ) 1. unless(fork)
    ) 2. if ($pid = fork)
    )
    ) that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
    ) this, else to this.

    It's actually very simple.
    As soon as fork() is executed, the following code is executed *twice*.
    Once in the parent process and once in the child process.

    At that point, the two processes are completely indistinguishable, except
    for one thing: the return value of fork().

    So, the if() that checks the value of the fork() call is actually making
    sure that the parent process and the child process are then going in
    different directions.

    if (my $pid = fork) {
    say "Hello, I am the parent process! The child process is $pid";
    } else {
    say "Hello, I am the child process!";
    }

    In this piece of code (barring strange exceptions or typos on my part),
    *both* 'say' statements will be executed. One in the parent process,
    one in the child process.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Feb 24, 2010
    #3
  4. Steve

    C.DeRykus Guest

    On Feb 24, 11:20 am, Steve <> wrote:
    > As it clearly states on oreilly's site:
    > "The fork command actually creates a child process, and returns the
    > PID of the process to the parent, and a value of zero to the child"
    >
    > My brain is having an implosion trying to understand code such as
    > this:
    > 1. unless(fork)
    > 2. if ($pid = fork)
    >
    > that's like saying "unless 6124" where 6124 is a PID.  or if 6124, do
    > this, else to this.


    There will be 2 processes as soon as the fork below occurs
    at runtime with fork() returning the child process id (PID,
    eg 6124, to the parent and 0 to the child itself.

    unless ( fork ) { # or 'unless ($pid = fork)'
    .....
    } else { # child: fork returns zero
    ....
    }

    But, Perl's backend compiles the above to a separate fork
    call followed by the branching logic. So, after the fork,
    both parent and child exist and each can evaluate fork's
    return and branch correctly.

    --
    Charles DeRykus
    C.DeRykus, Feb 24, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Dean
    Replies:
    4
    Views:
    256
    John Dean
    Sep 15, 2003
  2. srikanth
    Replies:
    5
    Views:
    272
    Richard Heathfield
    Feb 28, 2006
  3. asdfghjk
    Replies:
    17
    Views:
    603
    Keith Thompson
    Aug 19, 2010
  4. Vittal

    Can somebody explain?

    Vittal, Aug 10, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    138
    Vittal
    Aug 12, 2004
  5. yezi
    Replies:
    19
    Views:
    216
    Bart Lateur
    Sep 30, 2005
Loading...

Share This Page