bsder <> wrote:
> I m not sure whether the following Perl code is correct with fork().
>
> while (1) {
> if (open( FILE, "< $books" ) {
> if (!defined($kidpid = fork())) {
> die "cannot fork: $!";
> }
Control can never get here if the fork() failed, so...
> elsif ($kidpid == 0) { # child
.... you don't need an elsif, just an if would do.
> child_func();
> }
Control can never get here in the child process, so ...
> else # parent.
.... you don't need an else at all.
(and if you did need an else, you would need an opening curly bracket)
> sleep(1);
> }
> }
>
> Can anyone please give comment and suggestion of improving it?
# untested
while (1) {
if (open( FILE, "< $books" ) {
die "cannot fork: $!" unless defined($kidpid = fork());
if ($kidpid == 0) { # child
child_func();
}
# parent
sleep(1);
}
}
--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas