4

私はperlスレッドを使用する以前のマルチスレッドプログラムを終了し、それは私のシステムで動作します。問題は、実行する必要のある一部のシステムでは、スレッドサポートがperlにコンパイルされておらず、追加のパッケージをインストールできないことです。したがって、スレッド以外のものを使用する必要があり、コードをfork()の使用に移行しています。これは、サブタスクを開始する際に私のWindowsシステムで機能します。

いくつかの問題:

  1. 子プロセスがいつ終了するかを判断するにはどうすればよいですか?スレッド数が特定の値を下回ったときに新しいスレッドを作成しました。実行中のスレッドの数を追跡する必要があります。プロセスの場合、いつ終了するかを知るにはどうすればよいので、一度に存在するカウンターの数を追跡し、作成時にカウンターをインクリメントし、終了時にデクリメントすることができますか?

  2. 親プロセスがOPENで取得したハンドルを使用したファイルI/Oは、子プロセスで安全ですか?子プロセスごとにファイルに追加する必要があります。これはUNIXでも安全です。

  3. フォークとスレッドに代わるものはありますか?Parallel :: ForkManagerを使用しようとしましたが、システムにインストールされていません(Parallel :: ForkManagerを使用してエラーが発生しました)。追加のモジュールをインストールせずに、perlスクリプトがすべてのUNIX/Windowsシステムで機能する必要があります。

4

2 に答える 2

6

典型的な使用法:

use POSIX ':sys_wait_h';    # for &WNOHANG

# how to create a new background process
$pid = fork();
if (!defined $pid) { die "fork() failed!" }
if ($pid == 0) { # child
    # ... do stuff in background ...
    exit 0;      # don't forget to exit or die from the child process
} 
# else this is the parent, $pid contains process id of child process
# ... do stuff in foreground ...

# how to tell if a process is finished
# also see  perldoc perlipc
$pid = waitpid -1, 0;           # blocking wait for any process
$pid = wait;                    # blocking wait for any process
$pid = waitpid $mypid, 0;       # blocking wait for process $mypid
# after blocking wait/waitpid
if ($pid == -1) {
    print "All child processes are finished.\n";
} else {
    print "Process $pid is finished.\n";
    print "The exit status of process $pid was $?\n";
}

$pid = waitpid -1, &WNOHANG;    # non-blocking wait for any process
$pid = waitpid $mypid, 0;       # blocking wait for process $mypid
if ($pid == -1) {
    print "No child processes have finished since last wait/waitpid call.\n";
} else {
    print "Process $pid is finished.\n";
    print "The exit status of process $pid was $?\n";
}

# terminating a process - see  perldoc -f kill  or  perldoc perlipc
# this can be flaky on Windows
kill 'INT', $pid;               # send SIGINT to process $pid

perldoc -f forkwaitpidwait、、killおよび_ Windows ではサポートされていませんが、イベントのハンドラーの設定に関するperlipc内容は特に役立つはずです。perlipcSIGCHLD

フォークされたプロセス間の I/O は、通常、Unix と Windows で安全です。ファイル記述子は共有されているので、このようなもののために

open X, ">", $file;
if (fork() == 0) {  # in child
    print X "Child\n"; 
    close X;
    exit 0;
}
# in parent
sleep 1;
print X "Parent\n";
close X;

子プロセスと親プロセスの両方が同じファイルに正常に書き込みます (ただし、出力バッファリングに注意してください)。

于 2010-06-18T02:00:36.510 に答える
3

を見てくださいwaitpid。これは、実行する必要のある9つのタスク(1から9)を持つコードです。これらのタスクを実行するために最大3人のワーカーが起動します。

#!/usr/bin/perl

use strict;
use warnings;
use POSIX ":sys_wait_h";

my $max_children = 3;
my %work = map { $_ => 1 } 1 .. 9;
my @work = keys %work;

my %pids;
while (%work) {
    #while there are still empty slots
    while (@work and keys %pids < $max_children) {
        #get some work for the child to do
        my $work = shift @work;

        die "could not fork" unless defined(my $pid = fork);

        #parent
        if ($pid) {
            $pids{$pid} = 1;
            next;
        }

        #child
        print "$$ doing work $work\n";
        sleep 1;
        print "$$ done doing work $work";
        exit $work;
    }

    my $pid = waitpid -1, WNOHANG;

    if ($pid > 0) {
        delete $pids{$pid};
        my $rc = $? >> 8; #get the exit status
        print "saw $pid was done with $rc\n";
        delete $work{$rc};
        print "work left: ", join(", ", sort keys %work), "\n";
    }

    select undef, undef, undef, .25;
}
于 2010-06-18T01:56:27.997 に答える