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

发布于 2024-09-29 00:15:29 字数 1657 浏览 4 评论 0原文

我正在编写一个管理多进程的程序。这就是我所做的,而且效果很好!但现在,我想将消息从子进程发送到父进程,反之亦然(从父进程到子进程),你知道最好的方法吗?你知道我所做的是否是我想要的正确方法(从子进程到父进程发送消息、信号或共享内存,反之亦然)?

提前致谢!!


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

}

<代码>

Im writing a program that manage muti-processes. This is what I have done and its working great! but now, I want to send messages from the child processes to the parent process and viceversa (from the parent to the childs), do you know the best way? Do you know if what I have done is the proper way for what I want (send messages, or signals or share memory from the child processes to the parent process and viceversa)?

Thanks in advance!!


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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-10-06 00:15:29

用杀。如果您在 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
如果您需要做更复杂的事情,另一个选择是使用 POE

Use kill. If you set a variable in the parent before your fork, you don't need any external options.

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

See perldoc -f kill for more details on the kill call
Another option if you need to do more complex things is to use POE

梦言归人 2024-10-06 00:15:29

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

Forks::Super has a good interface for passing messages between parent and child processes (interprocess communication). With this interface, you can pass messages to the child's STDIN and read from the child's STDOUT and STDERR handles.

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";
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文