footloose
asked on
Checking if a console process is alive
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;
}
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,
if ( handle != NULL )
{
DWORD Value;
if ( GetExitCodeProcess( handle, &Value ) && ( Value == (DWORD)STILL_ACTIVE ) )
return true;
}
return false;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER