[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking if a console process is alive

Posted on 1998-10-01
2
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

656 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