Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: using fork

by moritz (Cardinal)
on Oct 14, 2013 at 16:27 UTC ( #1058197=note: print w/ replies, xml ) Need Help??


in reply to using fork

What jethro said: Only fork if you need to do something in both processes.

If that's the case, here is a working example:

#!/usr/bin/env perl use 5.014; use strict; use warnings; my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process say "Client starting..."; sleep 10; # do something useful instead! say "Client terminating"; exit 0; } else { # parent process say "Parent process, waiting for child..."; # do something useful here! waitpid $pid, 0; } say "Parent process after child has finished";

One thing to note is that fork returns undef on failure, and undef == 0 compares as true, so your error handling wouldn't work. To easily test the error case, start a new bash and set ulimit -u $number_of_allowed_processes, then start the perl script.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2016-07-01 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (406 votes). Check out past polls.