poco
poco copied to clipboard
[question] Usage of `ProcessHandleImpl::wait()` in Unix implemention
Some of the subprocesses from the fork()
execute very quickly and complete before the waitpid()
.
In this situation poco will throw exception SystemException("Cannot wait for process")
.
int ProcessHandleImpl::wait() const
{
int status;
int rc;
do
{
rc = waitpid(_pid, &status, 0);
}
while (rc < 0 && errno == EINTR);
if (rc != _pid)
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
if (WIFEXITED(status)) // normal termination
return WEXITSTATUS(status);
else // termination by a signal
return 256 + WTERMSIG(status);
}
But maybe we still want to get it exit code, show as follow:
try
{
res = ph.wait();
LOG_INFO << "wait pid " << ph.id() << ", exit code is: " << res;
}
catch(const Poco::Exception& ex)
{
LOG_INFO << "fork process already return, manually acquire it exit code";
int status;
waitpid(ph.id(), &status, WUNTRACED);
res = WIFEXITED(status);
LOG_INFO << "wait pid " << ph.id() << ", exit code is: " << res;
}
I don't know how to do it. Is there a better way?
This issue is stale because it has been open for 365 days with no activity.