I want to use execl in child process after doing fork. execl will execute the script which will take around 120 seconds of time. I tried almost all the combination with waitpid, wait and waitid with different argument (0, WNOHANG, etc.,) but in all case I am getting -1 return value. So I want to know in which wait function I need to use when? So I can concentrate in one wait function to make it work.
One more interesting thing which I observed from my logs is that when I am doing nothing in child process it is showing my parent thread as orphaned. I don't know how it is possible? How can my parent thread become orphaned?
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t Checksum_pid = fork();
if (Checksum_pid < 0)
printf("Fork Failed\n");
else if (Checksum_pid == 0)
{
execl("/bin/ls","ls",(char *)NULL) ;
exit(EXIT_FAILURE);
}
else
{
int childStatus;
pid_t returnValue = waitpid(Checksum_pid, &childStatus, 0);
if (returnValue > 0)
{
if (WIFEXITED(childStatus))
printf("Exit Code: %d\n", WEXITSTATUS(childStatus));
}
else if (returnValue == 0)
printf("Child process still running\n");
else
{
if (errno == ECHILD)
printf(" Error ECHILD!!\n");
else if (errno == EINTR)
printf(" Error EINTR!!\n");
else
printf("Error EINVAL!!\n");
}
}
return 0;
}