Solved

Checking if a console process is alive

Posted on 1998-10-01
2
431 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 170
Strange date stored 22 106
Installing Microsoft R server on REDHAT or SUSE Linux 4 131
Looking for a specific application/software 2 104
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now