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:
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;