Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > potential problem with fork()

Reply
Thread Tools

potential problem with fork()

 
 
bsder
Guest
Posts: n/a
 
      11-25-2005
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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-25-2005
bsder <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-25-2005
bsder <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      11-25-2005
[A complimentary Cc of this posting was sent to
Tad McClellan
<(E-Mail Removed)>], who wrote in article <(E-Mail Removed)>:
> # 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-26-2005
Jim Gibson wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the potential problem? happy C Programming 6 01-03-2010 12:37 PM
Potential Memory Leak problem in std::string in C++ vidya.bhagwath@gmail.com C++ 8 08-16-2006 07:05 AM
Job potential for MCSD.net? Bootstrap Bill Microsoft Certification 2 03-06-2004 05:50 AM
my potential next car Network Guru MCSE 18 01-30-2004 05:33 PM
Potential ASP.NET bug Robert May ASP .Net 1 10-16-2003 09:30 PM



Advertisments