好的,所以,我得到了信号通过但由于某种原因,该过程在收到信号后存在.
如果我在创建套接字之前添加无限循环(while(1)),那么它按规定工作.
所以……当我发出kill命令时,套接字代码中的某些东西就会退出.
我不知道它会是什么.如果没有kill,进程就会无限期地接受连接并向客户端发送消息.为什么kill(以及后面的变量的增量)会引发套接字退出循环并让进程结束?
套接字代码在……下面
[再次编辑]
$SIGNAL = 0; sub sigHandler{ #&logData("SIGNALED"); $SIGNAL++ ; } $SIG{"USR1"}=\&sigHandler; # Create a new socket,on port 9999 my $PORT = 9999; print ("opening connection on port $PORT"); $lsn = new IO::Socket::INET(Listen => 1,LocalPort => $PORT,Reuse => 1,Proto => 'tcp' ); #or die ("Couldn't start server: $!"); # Create an IO::Select handler $sel = new IO::Select( $lsn ); # Close filehandles close(STDIN); close(STDOUT); warn "Server ready. Waiting for connections . . . on \n"; # Enter into while loop,listening to the handles that are available. # this SHOULD be an infinite loop... I don't see why it would eval to false when # I send a signal to increment $SIGNAL by one. while( @read_ready = $sel->can_read ) { $MESSAGE = 0; $fh = $read_ready[0]; # Create a new socket if($fh == $lsn) { $new = $lsn->accept; $sel->add($new); push( @data,fileno($new) . " has joined."); warn "Connection from " . $new->peerhost . ".\n"; } # Handle connection else { $input = <$fh>; chomp $input; warn "GOT INPUT '$input'\n"; if($input eq "<policy-file-request/>"){ $MESSAGE = qq~<?xml version="1.0"?> <cross-domain-policy> <allow-access-from domain="*" to-ports="*"/> </cross-domain-policy>\0~; $SIGNAL++; } if ( $input eq '') {#disconnection notification by client warn "Disconnection from " . $new->peerhost . ".\n"; $sel->remove($fh); $fh->close; } if ( $input eq 'READY'){ warn "CLIENT READY = 1\n"; $CLIENT_READY = 1; } } # Write to the clients that are available foreach $fh ( @write_ready = $sel->can_write(0) ) { if($MESSAGE == 0){ #set message here based on criteria $MESSAGE = "UPDATE"; } warn "outside send if\n"; if($CLIENT_READY == 1 && $SIGNAL > 0){ warn ("sending $MESSAGE to $fh\n"); $CLIENT_READY = 0; $SIGNAL--; print $fh "$MESSAGE\0" or warn "can't send message to $fh"; } } } warn "Server ended.\n";
解决方法
如果在perl脚本中安装了
signal handler,则不必结束脚本. (不要通过调用die退出处理程序)注意,您不需要发送SIGINT或SIGKILL,也可以发送SIGUSR1.
编辑
你的kill命令的命令行后面有一个逗号,后面不应该有-USR1,(它是kill -USR1 4169)
编辑2
while(can_read)循环可能在它被信号中断时接收到空文件句柄数组时存在.您可以通过以下条件来防止这种情况:
while ((@read_ready = $sel->can_read) || 0 < $sel->count ) {
并更新循环以处理空的@ready数组.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。