Re: Simple fork examples




"Simon" <shmh@xxxxxxxxxxxxxx> wrote in message-id: <XF8hi.1000$4A1.769@xxxxxxxxxxxxxxxxxxxxxxxxxx>


[snip]
I ran your script, and got the following..

C:\>ballsonforehead.pl
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
.
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
.

"it_says_BALLS_on_your forehead" <simon.chao@xxxxxxx> wrote in message
news:1183126806.423422.290430@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jun 29, 10:13 am, "Simon" <s...@xxxxxxxxxxxxxx> wrote:
Hi guys!

I have read through the perldoc and must be really stupid.

I want to get my hands on some really basic example scripts of using
fork,
and what it is used for.
Ive looked on the net and cant find too many examples of this anywhere,
that
I can get a grip on.



use strict; use warnings;

$| = 1;

my $num_kids = shift;

^
notice the shift does not supply a default.
you must start this script with a integer as an argument.


my $pid;
for my $i ( 1.. $num_kids ) {
$pid = fork;
die "couldn't fork" if (!defined $pid);
if ( $pid == 0 ) {
print "$i about to sleep 3 secs...\n";
sleep 3;
print "$i done sleeping!\n";
exit;
}
print "pid '$pid'...i am parent\n";
}
do {
$pid = waitpid( -1, 0 );
} until $pid == -1;


In addition, if you want the script to work on more platforms,
afaik it is better to use threads.


.



Relevant Pages

  • Re: Simple fork examples
    ... I ran your script, and got the following.. ... Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl ... $pid = fork; ...
    (comp.lang.perl.misc)
  • CGI Script Fork
    ... to the browser and it will display them as they come. ... If, on the other hand, you really need to fork a background task, you ... may be thwarted by the apache perl cgi module that keeps the perl ... > I'm working on a cgi script that basically, upon running it, runs a ...
    (perl.beginners)
  • Re: understanding shared memory
    ... fork() doesn't come into the picture for subroutines. ... > a separate SpamAssassin server and feeding the email to it from the ... with multiple threads in Perl, ... script didn't die on a slower single-CPU machine. ...
    (comp.os.linux.misc)
  • fork() slowness
    ... Fork Performance. ... I'm writing a quick and dirty stress test script ... seconds/transaction. ... If I start a single process, I get an average of about 0.0001 ...
    (comp.lang.perl.moderated)
  • Re: Does recursive call able to print in same page as main call
    ... you tried running this script directly from the command line, ... I've found a lot of Perl CGI problems caused by strict system-security ... setting causing the ActivePerl fork problem under the http server. ...
    (comp.lang.perl.misc)