完了するまでに時間がかかる可能性がある子プロセスでwaitpid
使用している場合、いつ使用する必要がありますか?execl
親で使用するwaitpid
と、からの戻り値waitpid
が0であるため、子が実行されていることがわかります。ただし、waitpid
しばらくして別の関数で呼び出すと、 errno
set toで-1が返されますECHILD
。waitpid
子供が完了したかどうかわからない場合は、いつ使用すればよいですか?
//pid_t Checksum_pid = fork();
Checksum_pid = fork();
if (Checksum_pid == 0)
{
execl(path, name, argument as needed, ,NULL);
exit(EXIT_SUCCESS);
}
else if (Checksum_pid > 0)
{
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
if ( returnValue > 0)
{
if (WIFEXITED(childStatus))
{
printf("Exit Code: _ WEXITSTATUS(childStatus)") ;
}
}
else if ( returnValue == 0)
{
//Send positive response with routine status running (0x03)
printf("Child process still running") ;
}
else
{
if ( errno == ECHILD )
{
printf(" Error ECHILD!!") ;
}
else if ( errno == EINTR )
{
// other real errors handled here.
printf(" Error EINTR!!") ;
}
else
{
printf("Error EINVAL!!") ;
}
}
}
else
{
/* Fork failed. */
printf("Fork Failed") ;
}