Solved

Checking if a console process is alive

Posted on 1998-10-01
2
433 Views
Last Modified: 2013-11-20
I have a console application running on a NT workstation.
It spawns off one or more instances of a child program using CreateProcess. It retains each child's Process ID and periodically checks if the children are alive or not, using OpenProcess.

The OpenProcess sometimes returns a NULL handle even if the child process is alive. This seems to happen when the NT server is really really busy - the child processes do some heavy number crunching.

Any suggestions ?


int main( int argc, char *argv[] )
{
   ...

   STARTUPINFO si =
   {  
      sizeof(STARTUPINFO), NULL, NULL, NULL, 0,0,0,0,0,0,0, STARTF_USESHOWWINDOW, SW_SHOWNORMAL, 0, NULL, 0, 0, 0
   };
   PROCESS_INFORMATION pi;
   if ( !CreateProcess(NULL, (char *)aCommand.data(), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi) )
      cout << "Failed to start Child Process, CreateProcess returned " << GetLastError() << endl;
   else
      cout << "Child Process ID is " << pi.dwProcessId << endl;
   }
}

bool isActive( DWORD ProcessID )
{
   //Get handle to process
   HANDLE handle = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, ProcessID );
   if ( handle != NULL )
   {
      DWORD Value;
      if ( GetExitCodeProcess( handle, &Value ) && ( Value == (DWORD)STILL_ACTIVE ) )
         return true;
   }

   return false;
}
0
Comment
Question by:footloose
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1322821
Use the 'hProcess' member of the 'PROCESS_INFORMATION' structure filled in by the 'CreateProcess()' call instead of using 'OpenProcess()'. Besides, 'GetExitCodeProcess()' (as 'GetExitCodeThread()') is not very reliable for this task - i prefer calling 'WaitForSingleObject()' - the object's state is signaled when it terminates, so 'WAIT_TIMEOUT' guarantees that it's still running...
0
 

Author Comment

by:footloose
ID: 1322822
Thanks, JKR.Your suggestion works fine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modbus - whats the maximum I can store in one register? 4 82
Annoying "thing" blocks my view 4 133
Making an alias 7 115
Message not shown 5 87
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question