peschkaj has asked for the wisdom of the Perl Monks concerning the following question:

I am relatively new to perl. I feel as though I have a decent grasp of the language... well, good enough to get myself into loads of trouble/fun.

However, no matter how hard I try to figure it out, I cannot understand fork(). Can anyone out there take some of the agony and confusion out of fork()?

Replies are listed 'Best First'.
Re: Forking hell
by VSarkiss (Monsignor) on Aug 14, 2001 at 19:19 UTC

    The Perl fork function is an implementation of a concept in operating systems.

    First, you need to understand that an OS's idea of a process is something like "a context in which a program runs". (OS people, bear with me; I'm just trying to get the idea across.) In other words, if I'm going to run many programs at the same time on the same hardware, I have to keep them from bumping into each other. That's the notion of a context; the operating system creates processes and assigns resources to them. For example, the operating system has to switch contexts so that all programs have a fair chance to run. From its point of view, it can do this by providing the CPU resource to a given process. (Meditate on this until your point of view changes. ;-)

    OK, so how do I create these contexts or processes? One way is to give each process a way to create a new one. (Ignore for the moment how the first process was created. Divine intervention.) Now, a context isn't much good without something in it -- that is, a program to run. What should a newly created process be running? Well, how about just a duplicate of the calling program? That's the fork solution: it creates a duplicate (almost) of the calling program and sets both in motion.

    OK, so that's the operating systems concept of a fork. What about Perl? So far, I've got just a system call which doesn't necessarily have to be called fork; it could be called, say, newproc. Now programming languages need a way to use it.

    Perl provides fork() as a handle to that call, even if it's named something else in the underlying operating system. In other words, the Perl function fork() is part of the language, but is implemented different ways (or not at all!) on different operating systems. Its semantics are what I've outlined above: create a duplicate of the running program.

    "Wait," I hear you say (monks can hear over the internet). "If I have two duplicate programs, how do I know which is which?" Actually, the newly created process isn't an exact duplicate of the calling program. The salient difference is the return code from fork. In the new process, fork returns zero; in the parent it returns non-zero (usually an identifying number for the new process). That's why you'll usually see:

    if (fork() == 0) { # code executed by child process } else { # code executed by parent process }
    The crucial thing to realize is that two different program instances are executing the if after fork returns.

    /me cuts it short because the boss is calling

    HTH

Re: Forking hell
by clintp (Curate) on Aug 14, 2001 at 19:35 UTC
    In the way of an answer, and to maybe get a smile out of you, I offer this short tutorial on fork(). It was originally intended as a perl.com article except that I never got permission to use the characters.

    Unfortunately, I wrote it about 2 months before the R&B movie and the rights to the images were in the hands of the studio and not the original artists.

      Even though you're a UNIX guru, please have a look at the tutorial! clintp has made the most enterntaining, fun and informative tutorial I've ever seen!

      -- TMTOWTDI

Forking hell = Forked off
by larryk (Friar) on Aug 14, 2001 at 19:33 UTC
    strangely enough I asked exactly the same question just over a week ago but since we seem to have a penchant for amusing titles (no sniggering in the cheap-seats please!) you probably missed it in your super search.

    the node is Forked Off and has enlightened me - i believe it will do the same for you.

    hope this helps

       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
Re: Forking hell
by physi (Friar) on Aug 14, 2001 at 19:45 UTC
    Well fork is not only a perl command, but an unix command too.
    What does fork do ?
    It creates a copy of the actual running process. That's all!

    What is this good for ?
    Imagine you want to do several things at one time.
    For example to count forward from 1 to 10 and at the same time backward from 10 to 1.
    So you start like this:

    for ($i=1;$i<=10;$i++) { print "$i\n" }; for ($j=10;$j>=1;$j--) { print "$j\n" }; # This will give you: 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
    OK this is done one after the other in one process

    To do it at the same time you can use fork:

    $pid = fork(); if (! $pid) { ### This is the new process for ($j=10;$j>=0;$j--) { print "$j\n" }; } else { ### This is the parent process for ($i=1;$i<=10;$i++) { print "$i\n" }; }

    What ??

    Well with  $pid = fork() you create the same process again. This get's a new process-id and runs from the same point, where the fork was being done. The only difference between the parent and the child is, that only in the parent process the $pid variable is filled with the processnumber of the child process. So you can differ between the parent process ($pid=child-pid) and his child ($pid=empty).

    Now you can do whatever you want in the if (! $pid) part.
    It's important that all the vars declared and filled before the fork from the parent process are also available in the child process, but they are not visible in the other process!

    Hope this makes it a bit more clearer, but I'm not sure :-)

    ----------------------------------- --the good, the bad and the physi-- -----------------------------------