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

Re: Fork child process always finishes with parent

by markkawika (Monk)
on Sep 02, 2009 at 23:53 UTC ( [id://793055]=note: print w/replies, xml ) Need Help??


in reply to Fork child process always finishes with parent

I am unable to duplicate what you describe. I modified your code to see if the pid file contains a pid after returning from the wait() call, and it does:

#!/usr/bin/perl use strict; use warnings; my $fork_pid = fork(); if ($fork_pid == 0) { write_pid(); exit 0; } elsif ($fork_pid) { wait; open my $FILEH, '<', 'mypid.txt'; my $line = <$FILEH>; close $FILEH; print "I am $$. Saw child pid $line"; } else { die "Couldn't fork"; } sub write_pid { open my $FILEH, '>', 'mypid.txt'; print $FILEH $$ . "\n"; close $FILEH; }

When I run it, I get this:

$ ./blah.pl I am 7202. Saw child pid 7203

Indicating that the pid file is being written properly with the child's pid.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-09-15 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.