How to use fork correctly!

N

news.hinet.net

When i fork 5 child ,then process will terminate correctly.
But if i fork 25 child to do the same process. The program
will not terminate correctly.

How to use fork correctly??

ps: OS(win2000)
========================================
use IO::Socket::INET;

foreach my $child ((0..25)) {
if (fork() == 0) {
$| = 1;
print "Child $child trying to connect\n";
my $sock = IO::Socket::INET->new("www.hinet.net:80")
or die "Could not create connection\n";

$header=<<"ENDSTR";
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*
Accept-Language: zh-tw
Accept-Encoding: gzip
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461)
Connection: close


ENDSTR

print $sock "$header";

@res=<$sock>;
open (handle,">$child");
print handle join('',@res);
close(handle);
$sock->close;
#print "Child $child exiting\n";
exit 0;
}
}
wait foreach ((0..25));
print "Parent exiting\n";
exit 0;
 
B

Ben Morrow

Quoth "news.hinet.net said:
When i fork 5 child ,then process will terminate correctly.
But if i fork 25 child to do the same process. The program
will not terminate correctly.

How to use fork correctly??

ps: OS(win2000)

You may have trouble with fork under Win32: it is not a real fork, perl
emulates it using threads. You may (or may not) have more luck with
5.8's ithreads mechanism, which provides more control.
========================================
use IO::Socket::INET;

foreach my $child ((0..25)) {
if (fork() == 0) {

You need to remember the pid; also to check of fork failed.
$| = 1;
print "Child $child trying to connect\n";
my $sock = IO::Socket::INET->new("www.hinet.net:80")
or die "Could not create connection\n";

$header=<<"ENDSTR";
GET / HTTP/1.1

Don't do this: use LWP.
wait foreach ((0..25));

You would be better using waitpid to check that each child has in fact
terminated.
print "Parent exiting\n";
exit 0;

I think you want something more like (untested):

use strict;
use warnings;

use LWP::UserAgent;

my $UA = LWP::UserAgent->new(
agent =>
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461)',
);

my @Accept = qw[
image/gif image/x-xbitmap image/jpeg image/pjpeg
application/vnd.ms-powerpoint application/vnd.ms-excel application/msword
application/x-shockwave-flash */*
];

my %headers = (
accept_language => 'zh_tw',
accept_encoding => 'gzip',
accept => \@Accept,
);

my @kids;

for my $child (0..25) {
my $kid = fork;
defined $kid or warn "fork failed: $!" and last;

if ($kid) {
$kids[$child] = $kid;
next;
}

my $res = $UA->get( 'http://www.hinet.net/',
:content_file => $child,
%headers,
);

$res->is_success or die "Request failed: " . $res->message;
exit 0;
}

for (0..$#kids) {
my $pid = waidpid $kids[$_], 0;
$pid == 0 and warn "child $_ appears to have vanished...";
$pid < 0 and warn "can't wait for child $_: $!";
$? and warn "child $_ failed: $?";
}

__END__

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fork() & pipe() 3
Fork Problem 2
problem with fork 8
issue with multiprocess - fork 2
How to use shared memory with fork() ? 5
How to use (??{code}) correctly in foreach loop? 7
Fork in windows 0
socket and fork 3

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top