Running non-blocking commands.

Discussion in 'Perl Misc' started by Mike Kamzyuk, Oct 18, 2005.

  1. Mike Kamzyuk

    Mike Kamzyuk Guest

    Hello. I use perl on windows. Please don't post anything related to
    unix/linux/bsd/etc.

    Here's my problem. I have a script which is trying to run another
    script. I need to run the second script without blocking the first
    one. The second script needs to share nothing with the first one -
    that is, it should have its own STDIN, STDOUT, PID, etc. I don't need
    to access anything about the second script from the first one (although
    it's not harmful either). I also don't need to access anything about
    the first perl script from the second one (although that's also not
    harmful).

    How can this be accomplished? (Short of writing a c++ exe that does it
    and calling it from the first script :)

    P.S.
    Perl cookbook's example doesn't work. I've tried it way too many
    times, trust me.
    Forking appears to hang both processes. I've done an obvious if
    statement there.


    Thanks in advance,

    Mike.
    Mike Kamzyuk, Oct 18, 2005
    #1
    1. Advertising

  2. Mike Kamzyuk

    Guest

    Mike Kamzyuk wrote:
    > P.S.
    > Perl cookbook's example doesn't work. I've tried it way too many
    > times, trust me.


    Yeah, you can't trust anything that Tom Christiansen guy writes. But
    humor us... present the code you tried to run and tell us what happened
    when you ran it.
    , Oct 18, 2005
    #2
    1. Advertising

  3. "Mike Kamzyuk" <> wrote in
    news::

    > Hello. I use perl on windows. Please don't post anything related to
    > unix/linux/bsd/etc.
    >
    > Here's my problem. I have a script which is trying to run another
    > script. I need to run the second script without blocking the first
    > one. The second script needs to share nothing with the first one -
    > that is, it should have its own STDIN, STDOUT, PID, etc. I don't need
    > to access anything about the second script from the first one
    > (although it's not harmful either). I also don't need to access
    > anything about the first perl script from the second one (although
    > that's also not harmful).
    >
    > How can this be accomplished? (Short of writing a c++ exe that does
    > it and calling it from the first script :)


    Lucky for you, one has already been written. It is called start.

    > P.S.
    > Perl cookbook's example doesn't work.


    Which example are you talking about?

    In what way does it not work.

    > I've tried it way too many times, trust me.


    I won't until you have shown exactly what you are doing, which example
    you are talking about, and in what way "it doesn't work".

    > Forking appears to hang both processes. I've done an obvious if
    > statement there.


    That does not mean anything.

    > Thanks in advance,


    Please read the posting guidelines for this group to learn how you can
    help yourself, and help others help you.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and
    remove .invalid for email address)

    comp.lang.perl.misc guidelines on
    the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 18, 2005
    #3
  4. Mike Kamzyuk

    Mike Kamzyuk Guest

    Here's the example I used. Assume $cmd is the command i'm trying to
    run.

    FORK:
    if ($pid = fork) {
    #parent here. $pid is now the child id.
    # do nothing.
    }
    elsif (defined $pid) {
    #child here
    `$cmd`;
    die "finished running child"; # i don't care about the exit code.
    }
    elsif ($! = EAGAIN) {
    #supposedly i can recover
    sleep(2);
    redo FORK;
    }
    else {
    die "can't fork.";
    }


    Now, here's the result i'm seeing.

    Suppose this is the first script:
    ..... #stuff called A
    the forking call
    ..... #stuff called B.

    The second script completes in full, but does not die.
    The first script does not run any code from B.

    Oh, if i kill the second script during the hang, the first one proceeds
    to B.
    Mike Kamzyuk, Oct 19, 2005
    #4
  5. Mike Kamzyuk

    Guest

    Mike Kamzyuk wrote:
    > Here's the example I used....


    I see you've ignored Sinan's advice to read the posting guidelines.
    These guidelines are for your benefit - they help people help you
    better. If you had read them, you would have seen this:

    >>> First make a short (less than 20-30 lines) and complete program that
    >>> illustrates the problem you are having. People should be able to run
    >>> your program by copy/pasting the code from your article.


    You further ignored the guidelines advice:
    >>> You can ask perl itself to help you find common programming mistakes by
    >>> doing two things: enable warnings (perldoc warnings) and enable
    >>> ``strict''ures (perldoc strict).
    >>> You should not bother the hundreds/thousands of readers of the newsgroup
    >>> without first seeing if a machine can help you find your problem. It is
    >>> demeaning to be asked to do the work of a machine.


    Had you "use warnings", you would have been made aware of a syntax
    problem in your code.

    > Now, here's the result i'm seeing.
    > Suppose this is the first script:
    > .... #stuff called A
    > the forking call
    > .... #stuff called B.


    That doesn't mean anything.

    You may wish to read up on the difference between using backticks and
    the "system" function.

    Since you did not post a complete program, I made some assumptions.
    This almost works (I left your syntax error intact to encourage you to
    discover it for yourself with the "use warnings" pragma):

    #!/usr/bin/perl
    use warnings;
    use strict;

    FORK:
    if (my $pid = fork) {
    #parent here. $pid is now the child id.
    my $cmd = "ls /var/tmp >/tmp/junk.$$.txt";
    system $cmd;
    print "finished running parent $$"; # i don't care about the exit
    code.
    }

    elsif (defined $pid) {
    #child here
    my $cmd = "ls /var/tmp >/tmp/junk.$$.txt";
    system $cmd;
    die "finished running child $$\n"; # i don't care about the exit
    code.
    }

    elsif ($! = EAGAIN) {
    #supposedly i can recover
    sleep(2);
    redo FORK;
    }

    else {
    die "can't fork.";
    }


    When I run this (without the syntax error), I get:

    finished running child 37896
    finished running parent 3188

    and, indeed, I get two files:
    /tmp/junk.37896.txt
    /tmp/junk.3188.txt
    , Oct 19, 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. Hendra Gunawan
    Replies:
    1
    Views:
    12,436
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    916
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    816
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    723
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    256
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page