免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Perl perl fork
最近访问板块 发新帖
查看: 9104 | 回复: 3

perl fork [复制链接]

论坛徽章:
0
发表于 2003-03-27 09:06 |显示全部楼层
在perl使用fork进行多进程工作时,我现在遇到一个问题,主程序在wait()的时候,只能退出部分子进程,假设fork了2个子进程,只有1个能退出。
子进程在退出的时候,释放系统资源是否必须有一定的限制?比如:关闭文件的句柄,销毁模块的对象等等,我现在主程序不能等待所有的子进程退出,一直就挂在那里。 子进程的代码应该做怎样的检查??

谢谢!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2003-03-27 13:20 |显示全部楼层

perl fork

In parent process,
you need to  capture

$SIG{CHLD} =\&some_handle;

because , the signal "CHLD"  for the  parent,

will cause the parent to  exit...

and I guess your question is that

when the first child process end, it throws a CHLD

signal to the parent, and the parent process

dies by default,

and at this time , the root process (1) will take over

the position of the parent process...

then the second child process end, it throws a CHLD

signal to the root process...and itself becomes a zombie.....

So If you don't wanna this situation happens ,

just add the line I write above to handle the CHLD Signal....

and maybe you will get the right answer...

Sorry, I am not at my office, and I can't type in chinese at

this machine.....

Try it....

论坛徽章:
0
发表于 2003-03-27 18:33 |显示全部楼层

perl fork

$SIG{CHLD}:当子程序中断或退出时,用于通知主程序的信号。

foreach my $taskfile (@taskfile_list) {
        $child_id = fork();
        if ($child_id ==0) {
my $task_processor = new FC_TaskProcessor($Assistant_Var,$common_msg,$mddb_para_hash);
my $result = $task_processor->;task_process($taskfile);
$task_processor = $task_processor->;close();
        exit(0);
        }
        else {
                push @child_pid,$child_id;
        }
}

for(my $i=0;$i<@child_pid;$i++) {
        print "Wait: $child_pid[$i] \n\n";
        waitpid($child_pid[$i],0);
}
exit(0);

FC_TaskProcessor模块中可能有未释放的资源(文件、telnet等句柄)。

目前情况是子程序无法退出,你看我上面的代码是否有问题?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2003-03-29 10:38 |显示全部楼层

perl fork

#!/usr/bin/perl
use IO::Socket;
use POSIX 'WNOHANG';
require "/apile/xxx-lib.pl";
use constant TIMEOUT=>;1; #1 minute

open(STDERR,">;/tmp/svr.err" or die "Can't open STDERR: $!";

my $timeout = shift||TIMEOUT;

#signal handler for child die events
$SIG{CHLD}=sub{while(waitpid(-1,WNOHANG)>;0){ }};

#retrive socket form STDIN
die "STDIN is not a socket" unless -S STDIN;

my $listen_socket = IO::Socket->;new_from_fd(\*STDIN,"+<"
   or die "Can't create socket: $!";

while (1){
  $connection = eval{
    local $SIG{ALRM} =sub{die "timeout"};
    alarm($timeout*60);
    return $listen_socket->;accept;
  };
  alarm(0);
  exit 0 unless $connection;

  die "Can't fork: $!" unless defined(my $child = fork());
  if($child == 0){
    $listen_socket->;close;
    interact($connection);
    exit 0;
  }
  $connection->;close;
}

這是我拿來寫Wait Server用的程序碼,從
Network programming with perl中抄出來的..
給你當參考...最要緊得是SIG{CHLD}要讓他不要
使得Parent Process死掉,所以才有上面的語法..
因為Parent一死...Child process的行為就很難
控制了..也許你看到的是Zombie也說不定..
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP