構文 |
|
fork |
返り値 |
|
・親プロセスの場合:子プロセスのプロセスID ・子プロセスの場合:0 |
説明
- 新しいプロセスを生成します。
使用例
#!/usr/bin/perl
use strict;
use warnings;
$| = 1; # バッファリングをしない
if (my $pid = fork()) {
# 親プロセスの処理
print "wait begin.\n";
wait(); # 子プロセスの終了を待つ
print "wait end.\n";
} elsif (defined $pid) {
# 子プロセスの処理
exec 'netstat -n';
} else {
print "Error:$!\n";
}
print "End.\n";
#!/usr/bin/perl
use strict;
use warnings;
my @command = ('netstat', '-n');
my $ret = system @command;
if ($ret != 0) {
print "code[$ret]\n";
}
#!/usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
use LWP::Simple;
my %urls = (
'http://www.example.com/' => 'excom.html',
'http://www.example.net/' => 'exnet.html',
'http://www.example.org/' => 'exorg.html',
);
my $pm = Parallel::ForkManager->new(10);
while (my ($url, $file) = each %urls) {
$pm->start and next;
print "mirror begin($url)\n";
mirror($url, $file);
print "mirror e n d($url)\n";
$pm->finish;
}
$pm->wait_all_children;