返回列表 回复 发帖

[转帖]父进程与子进程communicate..利用PIPE的例子

作者:apile

# 本程序主要利用PIPE来建立Parent Process与Child Process间的互相连通,
# 利用%STATUS纪录目前Child Process的所有状态,与%CHILDREN纪录所有的Child
# Process。
# Parent Process:负责由CHILD_READ中读取所有CHILD Process的输入,并纪录
# 这些Process目前的状态。当收到INT、HUP、TERM等Signal时,即跳出主要loop
# 并将所有child Process全部杀光...
  1. #!/usr/bin/perl
  2. use strict;
  3. use IO::Select;
  4. use POSIX qw(WNOHANG);
  5. #---Define constants:定义准备先fork几个Process
  6. use constant PREFORK_CHILDREN   => 3;
  7. # debugging information:显示过程
  8. use constant DEBUG              => 1;
  9. # declare globals
  10. my $DONE=0;             # set flag to true when server done
  11. my %STATUS = ();        #child status information, child pid form keys of the ha
  12. sh, status form the values
  13. #--- 纪录所有Child Process的id...
  14. my %CHILDREN = ();
  15. #---Interrupt handles,跳出loop
  16. $SIG{TERM} = $SIG{INT}=$SIG{HUP} = sub { $DONE++ };
  17. #--- get CHLD Signal
  18. $SIG{CHLD} = sub {
  19.          while((my $child=waitpid(-1,WNOHANG)) > 0){
  20.            delete $CHILDREN{$child};
  21.          }
  22.       };
  23. # create a pipe for IPC:建立PIPE
  24. pipe(CHILD_READ,CHILD_WRITE) or die "Can't make pipe!\n";
  25. my $IN = IO::Select->new(\*CHILD_READ);
  26. # prefork some children
  27. make_new_child() for (1..PREFORK_CHILDREN);
  28. # main loop
  29. while(!$DONE){
  30.   # avoid parent block in the I/O call
  31.   if ($IN->can_read){ # got a message from one of the children
  32.     my $message;
  33.     next unless sysread(CHILD_READ,$message,4096);
  34.   # may contain several messages
  35.     my @messages = split "\n",$message;
  36.   # retrive every pid and status code
  37.     foreach (@messages){
  38.       next unless my ($pid,$status) = /^(\d+) (.+)$/;
  39.   # change status
  40.       if($status ne "done"){
  41.          $STATUS{$pid} = $status;
  42.       }else{
  43.   # delete pid
  44.          delete $STATUS{$pid};
  45.       }
  46.     }
  47.   }
  48.   warn join(' ',map {"$_=>$STATUS{$_}"} keys %STATUS),"\n" if DEBUG;
  49.   last unless %CHILDREN
  50. }
  51. warn "Termination received, killing children\n" if DEBUG;
  52. #-------------杀掉所有Child Process
  53. kill TERM => keys %CHILDREN;
  54. sleep while %CHILDREN;
  55. warn "Normal termination.\n";
  56. exit 0;
  57. #---- 建立新的Process
  58. sub make_new_child{
  59.   die "can't fork :$!" unless(defined( my $child = fork()));
  60.   if($child){   # child > 0, so we're the parent
  61.     $CHILDREN{$child} = 1;
  62.     warn "launching child $child\n" if DEBUG;
  63.   }else{
  64.     close CHILD_READ;   # no need to read from pipe
  65.     do_child();         # child handles incoming connections
  66.     exit 0;             # child is done
  67.   }
  68. }
  69. #------ child process
  70. sub do_child{
  71.    # write status code: idle
  72.    syswrite CHILD_WRITE,"$$ idle\n";
  73.    for(1..1000000){ };
  74.    syswrite CHILD_WRITE,"$$ busy\n";
  75.    for(1..1000000){ };
  76.    syswrite CHILD_WRITE,"$$ done\n";
  77. }
复制代码
哈哈哈!!!!你的IP是不是?我都知道了!!!
返回列表