On 3/10/06, Saurabh Singhvi <[EMAIL PROTECTED]> wrote:
> also i tried using this
>
> my $child = fork ();
>
>      unless ($child) {
>          sleep 5;
>          print "child\n";
>      }
> print 2;
>
> block of code, but i had a problem. Instead of the print statements,
> i had 2 system calls one inside the block and one outside. Now,
> as was expected, the fork allowed the one outside to get started.
> But then a strange thing happened. After the fork process was finished,
> the code re-executed the post system process, resulting in multiple, same
> commands, which is obviously not what i wanted.
>
> here is how it was:
>
>         my $child = fork();
>         unless($child) {
>             system('oggenc','something something') == 0 or die "$?" ;
>         }
>     system('mencoder','something something') == 0 or die "system 'mencoder'
> failed: $?" ;
>
> Now what happened was. after oggenc finished, it again started mencoder.
> resulting in
> an undesirable mencoder process, creating a worse situation than original.
>
> kindly tell me what to do.
>
> thanks
> Saurabh
>

add another if.

    unless ($child) {
        system(command_1);
    }

    if ($child) {
        system(sommand_2);
    }

Forking is tricky. You really need to read the docs, but the short
version is this: forking creates complete copy of the current process
at the time of the fork. Any code after the fork() call will be
evaluated and executed in the child just as it would be in the parent.
If there is code you don't want the child to execute, you need to
create tests to keep it from executing.

This is why some people like Spork: it takes some of the logic burden
off the programmer.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to