Thread Perl - Mehrere Child-Prozesse starten (9 answers)
Opened by Digioso at 2006-07-06 14:43

Digioso
 2006-07-07 11:25
#67896 #67896
User since
2006-07-06
8 Artikel
BenutzerIn
[default_avatar]
Atm. sieht das Programm jetzt so aus und startet auch vier Children, die ihre Befehle ausführen. Nur gibt es nach wie vor ein inaktives Child:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/perl -w
use strict;

pipe(READER,WRITER); # used for communication between child and parent

my $zahl = 0; # number of currently running children
my $zeile; # row
my $pid; # process ID

if (open FILE, "< /sls_backup/mdjanan/sleep.txt")
{
while($zeile = <FILE>)
{
if ($zahl < 5)
{
$zahl++; # increase number of children
$pid = fork(); # a new child is born. yay :)
} # if ($zahl < 5)
if($pid != 0)
{
# parent-process
print WRITER $zeile; # send child a command
$| = 1; # flush pipe
if($zahl == 5) # four children are running. wait till one of them finishes.
{
while(! wait()) # *waiting*
{
} # while(! wait())
$zahl--; # decrease number of children
} # if($zahl == 5)
} # if($pid != 0)
else
{
# child-process
# wait for command from parent
while (my $line = <READER>)
{
exec($line); # execute command
} # while (my $line = <READER>)
} # else
} # while($zeile = <FILE>)
} # if (open FILE, "< /sls_backup/mdjanan/sleep.txt")
else
{
print "\nCan't open file /sls_backup/mdjanan/sleep.txt\n";
} # else
exit 0;

View full thread Perl - Mehrere Child-Prozesse starten