potential problem with fork()

Discussion in 'Perl Misc' started by bsder, Nov 25, 2005.

  1. bsder

    bsder Guest

    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
     
    bsder, Nov 25, 2005
    #1
    1. Advertising

  2. 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
     
    Tad McClellan, Nov 25, 2005
    #2
    1. Advertising

  3. bsder

    Guest

    bsder <> 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
     
    , Nov 25, 2005
    #3
  4. [A complimentary Cc of this posting was sent to
    Tad McClellan
    <>], who wrote in article <>:
    > # 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
     
    Ilya Zakharevich, Nov 25, 2005
    #4
  5. 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
     
    John W. Krahn, Nov 26, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert May

    Potential ASP.NET bug

    Robert May, Oct 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    415
    Robert May
    Oct 16, 2003
  2. Lloyd Sheen
    Replies:
    0
    Views:
    328
    Lloyd Sheen
    Jan 24, 2004
  3. Replies:
    8
    Views:
    645
    Jim Langston
    Aug 16, 2006
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    579
    Eric Snow
    Jan 8, 2009
  5. happy

    What is the potential problem?

    happy, Jan 2, 2010, in forum: C Programming
    Replies:
    6
    Views:
    413
    Paul N
    Jan 3, 2010
Loading...

Share This Page