I would like to know if there's some way to get inside the while statement (printf("Death of child: %d with exit code %d\n",i, WEXITSTATUS(status));
) the integer i
from the child.
I'm getting the output from the execlp
(this program has a timeout inside that returns a random number) but I would like to get which is the identification i
of each returned response.
int main (int argc, char *argv[])
{
int i;
int pid;
int status;
int pro= atoi (argv[argc - 1]);
char programName[] = "./child";
if (argc < 2 || argc > 2){
printf("Only one argument is allowed\n");
exit (0);
}
for (i = 0; i < pro; i++)
{
pid = fork();
if(pid == 0)
{
printf("Created child %d with pid=%d\n",i, getpid());
char child[10];
snprintf(child, 10, "%d", i);
if (execlp(programName, programName, child, NULL) < 0){
printf("Error running: %s\n",programName);
}
exit(2);
}
}
while(wait(&status)>0)
{
if ( WIFEXITED(status) ){
printf("Death of child: %d with timeout %d\n",i, WEXITSTATUS(status));
if (WEXITSTATUS(status) > 5)
{
printf("Time exceeded\n");
}
}
}
exit (0);
}
Maybe my approach is not correct. Any tip is welcome. I'm completely new in c.