Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 在Perl中,如何将消息(或信号)从父进程发送到子进程,反之亦然?_Multithreading_Perl_Process_Multiprocess - Fatal编程技术网

Multithreading 在Perl中,如何将消息(或信号)从父进程发送到子进程,反之亦然?

Multithreading 在Perl中,如何将消息(或信号)从父进程发送到子进程,反之亦然?,multithreading,perl,process,multiprocess,Multithreading,Perl,Process,Multiprocess,我正在写一个管理多进程的程序。这就是我所做的,它的工作非常好!但是现在,我想将消息从子进程发送到父进程,反之亦然(从父进程发送到子进程),您知道最好的方法吗?您知道我所做的是否符合我的要求(从子进程向父进程发送消息、信号或共享内存,反之亦然) 提前谢谢 #!/usr/bin/perl -w use strict; use warnings; main(@ARGV); sub main{ my $num = 3; #this may change in the future (it

我正在写一个管理多进程的程序。这就是我所做的,它的工作非常好!但是现在,我想将消息从子进程发送到父进程,反之亦然(从父进程发送到子进程),您知道最好的方法吗?您知道我所做的是否符合我的要求(从子进程向父进程发送消息、信号或共享内存,反之亦然)

提前谢谢


#!/usr/bin/perl -w
use strict;
use warnings;

main(@ARGV);

sub main{
    my $num = 3; #this may change in the future (it would be dynamic)
    my @parts = (1,4,9,17,23,31,46,50);
    my @childs = ();

    while(scalar(@parts)>0 || scalar(@childs)>0){
        if(scalar(@parts)>0){
            my $start = scalar(@childs) + 1;
            for($start..$num){
                my $partId = pop(@parts);
                my $pid = fork();
                if ($pid) {
                    print "Im going to wait (Im the parent); my child is: $pid. The part Im going to use is: $partId \n";
                    push(@childs, $pid);
                } 
                elsif ($pid == 0) {
                    my $slp = 5 * $_;
                    print "$_ : Im going to execute my code (Im a child) and Im going to wait like $slp seconds. The part Im going to use is: $partId\n";
                    sleep $slp;
                    print "$_ : I finished my sleep\n";
                    exit($slp);
                } 
                else{
                    die "couldn’t fork: $!\n";
                }   
            }
        }

        print "before ret\n";
        my $ret = wait();
        print "after ret. The pid=$ret\n";

        my $index = 0;
        for my $value (@childs){
            if($value == $ret) {
                splice @childs, $index, 1;
                last;
            }
            $index++;
        }
    }

}

#!/usr/bin/perl-w
严格使用;
使用警告;
main(@ARGV);
副总管{
my$num=3;#这可能会在将来发生变化(这将是动态的)
my@parts=(1,4,9,17,23,31,46,50);
我的@childs=();
而(标量(@parts)>0 | |标量(@childs)>0){
如果(标量(@parts)>0){
我的$start=标量(@childs)+1;
对于($start..$num){
my$partId=pop(@parts);
我的$pid=fork();
如果($pid){
打印“我将等待(我是家长);我的孩子是:$pid。我将使用的部分是:$partId\n”;
推送(@childs,$pid);
} 
elsif($pid==0){
my$slp=5*$\;
打印“$\:我将执行我的代码(我是一个孩子),我将等待$slp秒。我将使用的部分是:$partId\n”;
睡眠$slp;
打印“$\我已完成睡眠\n”;
退出(slp);
} 
否则{
die“无法分叉:$!\n”;
}   
}
}
打印“ret之前”\n;
我的$ret=wait();
打印“在重试后。pid=$ret\n”;
我的$index=0;
对于我的$value(@childs){
如果($value==$ret){
拼接@childs,$index,1;
最后;
}
$index++;
}
}
}

使用kill。如果在fork之前的父级中设置变量,则不需要任何外部选项

my $parent_pid = $$; # Keep a reference to the parent

my $pid = fork();
if ($pid) {
    print "Im going to wait (Im the parent); 
    my child is: $pid. The part Im going to use is: $partId \n";
    push(@childs, $pid);
} 
elsif ($pid == 0) {
   my $slp = 5 * $_;
   print "$_ : Im going to execute my code (Im a child) and Im going to wait like $slp seconds. The part Im going to use is: $partId\n";
   sleep $slp;
   print "$_ : I finished my sleep\n";

   kill 20, $parent_pid # Send a signal to the parent, 20 is SIGCHLD

   exit($slp);
} 
有关kill调用的更多详细信息,请参见
perldoc-f kill
如果您需要做更复杂的事情,另一种选择是使用,有一个很好的接口,用于在父进程和子进程之间传递消息(进程间通信)。通过此接口,您可以将消息传递给子对象的STDIN,并从子对象的STDOUT和STDERR句柄读取消息

use Forks::Super;

# set up channels to child's STDIN/STDOUT/STDERR with blocking I/O
my $pid = fork { child_fh => 'all,block' };

if ($pid) { # parent
    $pid->write_stdin("Hello world\n");
    my $msg_from_child = $pid->read_stdout(); # <-- "HELLO WORLD\n"
    print "Message from child to parent: $msg_from_child";
} 
elsif (defined($pid) && $pid == 0) { # child
    sleep 1;
    my $msg_from_parent = <STDIN>;            # <-- "Hello world\n"
    my $output = uc $msg_from_parent;
    print $output;
    exit 0;
} 
else{
    die "couldn’t fork: $!\n";
}   
使用Forks::Super;
#使用阻塞I/O设置到孩子的标准输入/标准输出/标准输出的通道
my$pid=fork{child\u fh=>'all,block'};
如果($pid){#父
$pid->write_stdin(“Hello world\n”);

我的$msg_from_child=$pid->read_stdout();#可能最好的方法是让所有进程都连接到一个消息总线上。这将“免费”为您提供世界上所有的可伸缩性…多进程-perl僧侣在那里,似乎我们也有sangoma的
perldoc-perlipc
作为初学者;
perldoc-f kill
向进程发送信号