完了するまでに時間がかかる可能性がある子プロセスでwaitpid使用している場合、いつ使用する必要がありますか?execl
親で使用するwaitpidと、からの戻り値waitpidが0であるため、子が実行されていることがわかります。ただし、waitpidしばらくして別の関数で呼び出すと、 errnoset 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") ;
}