#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    40
    Rep Power
    10

    Problem with Win32::Process::Create


    Hey all,

    I'm trying to use the Win32::Process::Create module to handle some processes I need to run in parallel.

    As I've never used this module, I'm taking it real slow and building something really small and simple to test out how it works. However, I'm having some problems.

    Here's the code...

    --CreateProcess.pl
    Code:
    use Win32::Process;
    
    Win32::Process::Create( $Win32processObj,
                            "c:\\perl\\bin\\perl.exe",
                            "test1.pl",
                            0,
                            NORMAL_PRIORITY_CLASS,
                            "." ) ||  die "Failed to create process.\n";
    
    while ( !$Win32processObj->Wait( 1000 ) )
    {
            sleep(1);
    	print "waiting for process to finish\n";
    }
    
    $Win32processObj->GetExitCode( $exitcode );
    
        if (0 != $exitcode)
          {
            $rv = 1;
          }
    
    print "Process Exit Code = $exitcode\n";
    print "Return Value = $rv\n";
    --test1.pl
    Code:
    print "exiting with return_code 1\n";
    exit 1;

    As you can see, I'm using CreateProcess.pl to simply create a process which executes test1.pl. test1.pl is a simplistic script whose only purpose is to return a non-zero return code (I also have a test0.pl).

    My problem, is that the process used to invoke test1.pl never exits, and my script just sits inside the while loop. I usually end up killing it after a minute or so.

    I can verify with taskmgr that my process isn't dying, but I have no idea why. Am I implementing Win32::Process::Create correctly? Is there something wrong with my test1.pl so that it's not dying?

    Any help would be appreciated.
  2. #2
  3. 63 dorinte
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Alleghany Highlands
    Posts
    130
    Rep Power
    230
    I tried your code and rewrote it slightly, but I think what you missed is the
    Perl Code:
    $Process_Object->Kill($ExitCode);
    at the end. My slightly modified version below runs fine on my machine:

    Perl Code:
    #!\C<img src="http://images.devshed.com/fds/smilies/tongue.gif" border="0" alt="" title="Stick Out Tongue" class="inlineimg" />erl\bin\perl.exe -w
     
    use strict;
    use Win32::Process;
    use Win32;
     
    # Use the error reporting suggested in the docs:
    sub ErrorReport{
        print Win32::FormatMessage( Win32::GetLastError() );
    }
     
    # Create the process:
    my $Process_Object;
    Win32::Process::Create( $Process_Object,
                            "c:\\perl\\bin\\perl.exe",
                            "hello.pl",
                            0,
                            NORMAL_PRIORITY_CLASS,
                            "." ) ||  die &ErrorReport();
     
    # Wait for the created process to finish:
    while ( $Process_Object->Wait( 1000 ) )
    {
            sleep(1);
    	print "waiting for process to finish\n";
    }
     
    my $ExitCode;
    my $ReturnVal = 0;
    $Process_Object->GetExitCode( $ExitCode );
     
        if ( $ExitCode != 0 )
          {
            $ReturnVal = 1;
          }
     
    print "Process Exit Code = $ExitCode\n";
    print "Return Value = $ReturnVal\n";
     
    # Kill the calling process:
    $Process_Object->Kill($ExitCode);


    HTH,

    Ben N1NP
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    40
    Rep Power
    10
    Hmm, that helps a little, but I suspect the process is always returning a code for "I was terminated!"

    No matter what I make the sub-script return, your code displays the following:

    Process Exit Code: 259
    Return Value: 1

    This happens even if I have the process run a script that returns 0. Is it possible that killing the process is making it return a different code?

    EDIT: Nevermind, It's not killing the process until after it prints the values of the variable. I have no clue what's going on here.
  6. #4
  7. 63 dorinte
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Alleghany Highlands
    Posts
    130
    Rep Power
    230
    Try replacing the wait loop
    Perl Code:
    # Wait for the created process to finish:
    #
    while ( $Process_Object->Wait( 1000 ) )
    #
    {
        sleep(1);
        print "waiting for process to finish\n";
    }
    with
    Perl Code:
    $Process_Object->Wait(INFINITE);
    Then the $ExitCode will return what is set in the 'test1.pl'. AND you can get rid of the
    Perl Code:
    $Process_Object->Kill($ExitCode);
    It looks like the wait loop was the problem. Again, HTH,

    Ben N1NP
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    40
    Rep Power
    10
    Hmm... I'm afraid we're back to the process not timing out, with or without the kill statement.
  10. #6
  11. 63 dorinte
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Alleghany Highlands
    Posts
    130
    Rep Power
    230
    Maybe I am not understanding what you mean by 'timing out'. The code
    Perl Code:
    #!\C:\Perl\bin\perl.exe -w
     
    use strict;
    use Win32::Process;
    use Win32;
     
    # Use the error reporting suggested in the docs:
    sub ErrorReport{
        print Win32::FormatMessage( Win32::GetLastError() );
    }
     
    # Create the process:
    my $Process_Object;
    Win32::Process::Create( $Process_Object,
                            "C:\\Perl\\bin\\perl.exe",
                            "perl hello.pl",
                            0,
                            NORMAL_PRIORITY_CLASS,
                            "." ) ||  die &ErrorReport();
     
    $Process_Object->Wait(INFINITE);
     
    my $ExitCode = 0;
    my $ReturnVal = 0;
    $Process_Object->GetExitCode( $ExitCode );
     
        if ( $ExitCode != 0 )
          {
            $ReturnVal = 1;
          }
     
    print "Process Exit Code = $ExitCode\n";
    print "Return Value = $ReturnVal\n";
    runs the second script, waits for it to exit, prints the exit code, then exits itself.

    Ben N1NP
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    40
    Rep Power
    10
    A ha! I knew it was something simple!

    Code:
    Win32::Process::Create( $Process_Object,
                            "C:\\Perl\\bin\\perl.exe",
                            "perl hello.pl",
                            0,
                            NORMAL_PRIORITY_CLASS,
                            "." ) ||  die &ErrorReport();
    That's different from our earlier examples. I should've seen it myself... but the parameter is different.

    Even though we specify c:\perl\bin\perl.exe as the location of perl.exe, we still have to specify "perl hello.pl" as the argument.

    You'll notice I was just supplying "hello.pl" in my previous examples. Try running it like that, and you'll see what I mean.

    Thanks for all your help!

IMN logo majestic logo threadwatch logo seochat tools logo