How to continue process without waiting for the output of system()?

Discussion in 'Perl Misc' started by Cod, Aug 30, 2007.

  1. Cod

    Cod Guest

    For example , I wrote:

    system("calc");
    print "hello";

    Before the calc program is closed, the print statement will not
    excute.

    Any way to change the behavior?
    I have tried fork, and system("calc&"), all failed.

    My OS is WinXP, and I use Active Perl 5.8.8.

    Thanks for your help!
     
    Cod, Aug 30, 2007
    #1
    1. Advertising

  2. Cod

    Narthring Guest

    On Aug 30, 9:57 am, Cod <> wrote:
    > For example , I wrote:
    >
    > system("calc");
    > print "hello";
    >
    > Before the calc program is closed, the print statement will not
    > excute.
    >
    > Any way to change the behavior?
    > I have tried fork, and system("calc&"), all failed.
    >
    > My OS is WinXP, and I use Active Perl 5.8.8.
    >
    > Thanks for your help!


    If you want to use threads this will work:
    use strict;
    use warnings;
    use threads;


    my $thread = threads->create(sub { thread()});
    print "hello\n";
    $thread->join();

    sub thread{
    system("calc");
    1;
    }
     
    Narthring, Aug 30, 2007
    #2
    1. Advertising

  3. Cod

    Guest

    Cod <> wrote:
    > For example , I wrote:
    >
    > system("calc");
    > print "hello";


    Without a newline in the print, you have to careful that buffering
    isn't going to get you.

    >
    > Before the calc program is closed, the print statement will not
    > excute.
    >
    > Any way to change the behavior?
    > I have tried fork, and system("calc&"), all failed.


    What do you mean by "failed"?

    >
    > My OS is WinXP, and I use Active Perl 5.8.8.



    H:\>perl -e "fork or do {system('calc'); exit}; $|=1; print 'hello' "

    Does what I expect.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 30, 2007
    #3
  4. Cod

    Ben Morrow Guest

    Quoth Cod <>:
    > For example , I wrote:
    >
    > system("calc");
    > print "hello";
    >
    > Before the calc program is closed, the print statement will not
    > excute.
    >
    > Any way to change the behavior?
    > I have tried fork, and system("calc&"), all failed.


    The (approximate) equivalent of 'cmd &' under NT is 'start cmd'. So

    system "start calc";

    As others have said, fork ought to work anyway.

    Ben
     
    Ben Morrow, Aug 30, 2007
    #4
  5. Cod

    Cod Guest

    Thank you all.

    Use Narthring's method or xhos's method, there is one problem: BEFORE
    the calc closed, the programe won't exit. Just like this:

    d:\>perl -e "fork or do {system('calc'); exi
    t}; $|=1; print 'hello' "
    hello

    AFTER calc closed, below appears:
    d:\>

    By Follow Ben Morrow say, I tried:
    d:\>perl -e "fork or do {system('calc'); exi
    t}; $|=1; print 'hello' "
    hello
    d:\>

    This time it works just what I wanted.

    Thank you all again, and sorry for my poor English:)
     
    Cod, Aug 31, 2007
    #5
  6. Cod <> wrote in news:1188570633.227615.319150
    @l22g2000prc.googlegroups.com:

    > Thank you all.
    >
    > Use Narthring's method or xhos's method, there is one problem: BEFORE
    > the calc closed, the programe won't exit. Just like this:
    >
    > d:\>perl -e "fork or do {system('calc'); exi
    > t}; $|=1; print 'hello' "
    > hello
    >
    > AFTER calc closed, below appears:
    > d:\>
    >
    > By Follow Ben Morrow say, I tried:
    > d:\>perl -e "fork or do {system('calc'); exi
    > t}; $|=1; print 'hello' "
    > hello
    > d:\>
    >
    > This time it works just what I wanted.


    If you want more control, you can use Win32::process

    #!/usr/bin/perl

    use strict;
    use warnings;

    use FindBin qw( $Bin );

    use Win32::process;
    use Win32;

    sub ErrorMsg { Win32::FormatMessage( Win32::GetLastError() ) }

    my $calc_path = q{C:/WINDOWS/system32/calc.exe};

    my $calc_process;

    Win32::process::Create(
    $calc_process,
    $calc_path,
    'calc',
    0,
    NORMAL_PRIORITY_CLASS,
    $Bin
    ) or die sprintf "Error starting '%s': %s", $calc_path, ErrorMsg();

    sleep 3;

    $calc_process->Suspend();

    sleep 3;

    $calc_process->Resume();

    sleep 10;

    $calc_process->Kill(0);

    __END__


    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)
    clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
     
    A. Sinan Unur, Sep 2, 2007
    #6
  7. Cod

    Cod Guest

    On 9 2 , 8 59 , "A. Sinan Unur" <> wrote:
    > Cod <> wrote in news:1188570633.227615.319150
    > @l22g2000prc.googlegroups.com:
    >
    >
    >
    >
    >
    > > Thank you all.

    >
    > > Use Narthring's method or xhos's method, there is one problem: BEFORE
    > > the calc closed, the programe won't exit. Just like this:

    >
    > > d:\>perl -e "fork or do {system('calc'); exi
    > > t}; $|=1; print 'hello' "
    > > hello

    >
    > > AFTER calc closed, below appears:
    > > d:\>

    >
    > > By Follow Ben Morrow say, I tried:
    > > d:\>perl -e "fork or do {system('calc'); exi
    > > t}; $|=1; print 'hello' "
    > > hello
    > > d:\>

    >
    > > This time it works just what I wanted.

    >
    > If you want more control, you can use Win32::process
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use FindBin qw( $Bin );
    >
    > use Win32::process;
    > use Win32;
    >
    > sub ErrorMsg { Win32::FormatMessage( Win32::GetLastError() ) }
    >
    > my $calc_path = q{C:/WINDOWS/system32/calc.exe};
    >
    > my $calc_process;
    >
    > Win32::process::Create(
    > $calc_process,
    > $calc_path,
    > 'calc',
    > 0,
    > NORMAL_PRIORITY_CLASS,
    > $Bin
    > ) or die sprintf "Error starting '%s': %s", $calc_path, ErrorMsg();
    >
    > sleep 3;
    >
    > $calc_process->Suspend();
    >
    > sleep 3;
    >
    > $calc_process->Resume();
    >
    > sleep 10;
    >
    > $calc_process->Kill(0);
    >
    > __END__
    >
    > Sinan
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    > clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>- -
    >
    > - -


    thx, I've tried Win32::process, It's very useful , although need more
    typing:)
     
    Cod, Sep 6, 2007
    #7
    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. =?Utf-8?B?RGlnZ2xlcg==?=

    Waiting for a file deletion to continue

    =?Utf-8?B?RGlnZ2xlcg==?=, Oct 13, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    380
    =?Utf-8?B?RGlnZ2xlcg==?=
    Oct 14, 2004
  2. Luigi
    Replies:
    5
    Views:
    425
    Donn Cave
    Apr 3, 2006
  3. Pierre Morel
    Replies:
    5
    Views:
    130
    Pierre Morel
    Feb 17, 2010
  4. Rex Gustavus Adolphus
    Replies:
    5
    Views:
    156
    Rex Gustavus Adolphus
    Jul 4, 2003
  5. PerlFAQ Server
    Replies:
    0
    Views:
    192
    PerlFAQ Server
    Apr 23, 2011
Loading...

Share This Page