Our highest priority is to satisfy the customer through early and continuous delivery of valuable and working software.

Friday, December 21, 2007

Perl - fork() example

Here is a small example which demonstrate fork() implementation in PERL.


while(1)
{
my @children;

defined (my $pid = fork) or die "Cannot fork: $!\n";

if ($pid)
{
# Parent Process...

push(@children, $pid);
wait();
my $ret = $?;

if ($ret)
{
warn("Abnormal child exit: $!\n");
$daemon = undef;
last;
}
}
else
{
# Child Process...
exit(0);
}

while (scalar(@children))
{
wait();
my $ret = $?;
shift(@children);

if ($ret)
{
warn("Abnormal child exit: $!\n");
$daemon = undef;
}
}

}

continue
{
last unless $daemon;
sleep(10);
}

5 comments:

  1. Keep up the good work. Merry Christmas & Happy New Year!

    ReplyDelete
  2. Well, this is useless as an example. What is it supposed to do? Where is the description?

    ReplyDelete
  3. why do we wait for child processed later again?

    ReplyDelete
  4. I agree. This is a useless example

    ReplyDelete
  5. Dumb"est" blog ever seen

    ReplyDelete