Solved

Checking if a console process is alive

Posted on 1998-10-01
2
426 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
Comment Utility
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
Comment Utility
Thanks, JKR.Your suggestion works fine.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

12 Experts available now in Live!

Get 1:1 Help Now