- 论坛徽章:
- 0
|
如果是WindowsXP收割子进程我知道,但不知是否是你想要的:
方法1:- #!/usr/bin/perl -w
- foreach (1..10){
- unless ($pid = fork()) {
- unless (fork()) {
- print "Child $_ : My pid = $$\n";
- &cmd($_);
- }
- exit 0;
- }
- waitpid($pid, 0);
- }
- sub cmd{
- my $da=time();
- print "$_[0] $da\n";
- }
复制代码 方法2:- #!/usr/bin/perl
- our $zombies = 0;
- $SIG{CHLD} = sub { $zombies++};
- my $i=0;
- foreach (1..10) {
- $i++;
- reaper() if $zombies;
- my $pid = fork();
- unless(defined($pid))
- {
- print ("Fork process failured!\n");
- exit();
- }
- if ($pid)
- {
- # This is the child process.
- cmd($i);
- exit();
- }
- else
- {
- # This is the parent process.
- next;
- }
- }
- sub reaper {
- my $zombie;
- our %Kid_Status;
- $zombies = 0;
- while (($zombie = waitpid( -1, WNOHANG)) != -1) {
- $Kid_Status{$zombie} = $?;
- }
- }
- sub cmd{
- my $da=time();
- print "$_[0] $da\n";
- }
复制代码 |
|