fork() question



I think I am getting the idea of fork() all wrong. Here is an example:


my $pid = fork();

if ($pid) { # should apply to parent only?
exit;
}

sleep 1;
print "Test\n";
exit;


'Test' does not print. If I remove the 'sleep 1' - it prints. From what
I understood fork creates two identical processes that become completely
independent from each other. What am I missing?

Peter
.



Relevant Pages

  • Re: fork defunct process, already forked twice
    ... pid = fork ... exit; ... //output a fork error message; ... if (gpid < 0){ ...
    (comp.lang.c)
  • Re: RFC: implement daemon() in the kernel
    ... Except some special cases, fork() ... The child process is created and suspended, the parent continues to run ... until it calls exec() or _exit. ... You do have to look out for any applications that fork and do not either exit or exec, but that is so much better than having to modify so many things just to get them to run. ...
    (Linux-Kernel)
  • thread death callback to be called after fork
    ... t = Thread.new{sleep} ... fork{t.join; exit} and Process.wait ... definining a finalizer is one way to trigger notification of thread death on ...
    (comp.lang.ruby)
  • Re: Running system() commands in background
    ... > I'm not sure fork will work under win32. ... Should this code be run under DOS and a reader does not know ... how to exit two command instances, if a reader selects to close ... Fork is not supported under Win32. ...
    (comp.lang.perl.misc)
  • Re: Nuby Q: Daemonize (other materials have been read!)
    ... I'm using Ruby 1.8 on a Linux box. ... exit if fork ... suffering increases your inner strength. ...
    (comp.lang.ruby)