in reply to Duplicate Output with forking

Is it possible that you're executing the same code after the fork in both the parent and the child processes?

Whittle your code down to a small example that demonstrates the problem, and post it. People here love to have code to work from.

Replies are listed 'Best First'.
Re: Re: Duplicate Output with forking
by skazat (Chaplain) on Mar 21, 2001 at 03:49 UTC

    Here's the bare bones, snip:

    #!/usr/bin/perl -w $|++; use CGI qw(:standard); print header(); print h1('howdy!'), hr(); FORK: { if($pid = fork){ }elsif (defined($pid)){ print p('i am a child!') } } print h2("that's all folks");

    This prints

    that's all forks

    before and after it prints 'i am a child!' for me

     

    -justin simoni
    !skazat!

      You're getting the duplicate output because both the parent and the child process are falling through to the same print statement.

      Try having the child exit after it identifies itself. E.g.,

      print p('i am a child!'); exit(0); # don't fall through }

        That did the trick,

        my thanks to you,

         

        -justin simoni
        !skazat!