2

マルチプロセスを管理するプログラムを書いています。これは私がやったことであり、それは素晴らしい働きをしています!しかし今、私は子プロセスから親プロセスに、そしてその逆(親から子へ)にメッセージを送りたいのですが、あなたは最善の方法を知っていますか?私がしたことは、私が望むもの(メッセージを送信するか、子プロセスから親プロセスに、またはその逆にメモリを共有する)に適切な方法であるかどうかを知っていますか?

前もって感謝します!!


#!/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++;
        }
    }

}

4

2 に答える 2

4

killを使用します。フォークの前に親に変数を設定する場合、外部オプションは必要ありません。

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);
} 

キルコールperldoc -f killの詳細については、を参照してください。より複雑なことを行う必要がある場合の別のオプションは、POEを使用することです。

于 2010-10-27T17:17:59.573 に答える
2

Forks::Super親プロセスと子プロセスの間でメッセージを渡すための優れたインターフェイスがあります(プロセス間通信)。このインターフェイスを使用すると、メッセージを子の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";
}   
于 2010-10-27T23:48:03.667 に答える