Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Question on how fork works.

by gnu@perl (Pilgrim)
on Jul 01, 2002 at 00:44 UTC ( [id://178439]=perlquestion: print w/replies, xml ) Need Help??

gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I have a newbie question. I have used fork before, but I am not completely clear on how it works. Here is a little sample script I wrote to help illustrate my question:
#!/usr/bin/perl -w if ($pid = fork) { print "1. in pid $$\n"; waitpid($pid,0); } else { print "2. in pid $$\n"; }
From what I understand, when you 'fork' a new copy of your complete program is started. My main question is this, where does the execution start from in the fork-ed program? It obviously cannot start from the beginning or it would go on forking forever. In my little example I understand why 'print "1. in pid $$\n";' is executed in the parent process ( ($pid = fork) is true), but why is it not executed in the child as well? If execution started from the beginning, it would fork again. Also, why in the child process does ($pid = fork) evaluate to false and run the 'else' portion? I hope I explained things clearly and TIA for your help. Chad.

Replies are listed 'Best First'.
Re: Question on how fork works.
by samtregar (Abbot) on Jul 01, 2002 at 01:57 UTC
    When a process forks execution continues in two processes at the same point. The difference is that in parent process the fork() returns the PID of the child and in the child it returns undef. So, in your example the parent enters the first branch of the if block and the child enters the second.

    Perl didn't invent this behavior - fork() is just a think wrapper around the normal UNIX fork() system call. The behavior is the same.

    -sam

      The docs describe it just a little differently. According to perlfunc, it returns 0 (zero) to the child process. undef is returned to the parent if the fork fails.

      - Matt Riffle

        Good point. I should have kept it to what I knew and said it returned false.

        -sam

Re: Question on how fork works.
by clintp (Curate) on Jul 01, 2002 at 01:59 UTC
    A quick and easy-to-read tutorial on fork() I did for a publication a long time ago. Unfortunately, I never got permissions for the characters because the rights had just been licensed for the really sad R&B movie. But enjoy anyway.
Re: Question on how fork works.
by Abigail-II (Bishop) on Jul 01, 2002 at 12:45 UTC
    At a fork(), we get two almost identical copies of the same process. Everything gets copied (well, virtually anyway - the OS might use a 'copy-on-write technique), program, data, program counter, attributes, with a small set of differences. Included in the differences are: - fork() returns the process id of the child in the parent, and it returns 0 in the child. - The child gets a new process id (which is returned by fork() in the parent) - the parent keeps the id. - The parent keeps the 'ppid' - the ppid of the child equals the pid of the parent. - Filelocks are not inherited by the children. - Pending signals (including alarm) are not inherited. Stevens has a full list.

    Note that if fork() would start the child at the beginning of the program, something like:

    perl -e 'fork'
    would never end.

    Abigail

Re: Question on how fork works.
by gnu@perl (Pilgrim) on Jul 01, 2002 at 02:07 UTC
    Just wanted to add a thank you for all your help. The links submitted here clarified what I thought was going on and gave me a deeper explination.
Re: Question on how fork works.
by bronto (Priest) on Jul 01, 2002 at 10:01 UTC

    You guessed right about the starting point of the child process. I'll try to explain about what happens then.

    So, as you told, execution starts at "the forking point". What happens here is that while in the parent process gets its child's PID from the fork call, child gets 0 (as you can read on perldoc -f fork). In some sense, your program splits at fork just before it returns some value (well, it's not how it really work but... I think you got the idea...).

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://178439]
Approved by samtregar
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-24 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found