potential problem with fork()

B

bsder

Hi,

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: $!";
}
elsif ($kidpid == 0) { # child
child_func();
}
else # parent.
sleep(1);
}
}

Can anyone please give comment and suggestion of improving it?

Thanks
Sam
 
T

Tad McClellan

bsder said:
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);
}
}
 
X

xhoster

bsder said:
Hi,

I m not sure whether the following Perl code is correct with fork().

while (1) {
if (open( FILE, "< $books" ) {

Syntax error, unbalanced parens. Also, you wouldn't you want to do
something more intelligent in case the file can't be opened?

if (!defined($kidpid = fork())) {
die "cannot fork: $!";
}
elsif ($kidpid == 0) { # child
child_func();

child_func() had better never return, or else you are making a fork-bomb.
}
else # parent.
sleep(1);
}
}

Can anyone please give comment and suggestion of improving it?

Without knowing what it is supposed to do?

Xho
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Tad McClellan
# untested
while (1) {
if (open( FILE, "< $books" ) {
die "cannot fork: $!" unless defined($kidpid = fork());

if ($kidpid == 0) { # child
child_func();
}
# parent

How should I guess looking on this code that child_func() never
returns? IMO, one should at least move exit() out of child_func()
into the main loop to make the intent clear...
sleep(1);
}
}

Hope this helps,
Ilya
 
J

John W. Krahn

Jim said:
You should use the 3-argument version of open(). You should print why
open did not succeed if it failed. It is usually better to use
variables rather than barewords for file handles:

if ( open( my $file, '<', $books ) ) { ^^^

...
}else{
die("Can't open $books for writing: $!");
^^^^^^^
I'm not suprised that it won't open for writing.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top