Solved

Checking if a console process is alive

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 242
format the code in java 6 101
ShiftLeft challenge 21 85
FizzBuzz challenge 9 82
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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