The idea of the code is: Give fork 3 times, that is, from the parent create three children (with count = 0, count = 1 and count = 2). The children then upon returning from the loop will see that their i == 0 and will exit the loop. When entering the other condition, they (the children, because i == 0), when starting the count should appear 0,1,2 , but it is appearing 0,0,0 , then give an exec by passing: 1-the path with the name of the executable. 2-What is the number of the child (0,1,2).
I have the parent program: ...
i = 1;
for( count = 0; count < 3; count++) {
if( i != 0 ) {
i = fork();
} else {
break;
}
}
if( i == 0 ) {
printf("%d",count);
char arg[10];
sprintf(arg, "%d", count);
execl("/home/Downloads/filho",arg,NULL);
.... My expected printf ouuput was:
0.1,2
But it's coming out:
0.0,0
EDIT ----- Maybe in the child I'm taking the value in the wrong way: I'm going through this:
char arg[10];
sprintf(arg, "%d", count);
execl("/home/Downloads/filho",arg,NULL);
And not child executable:
int n = atoi(argv[1]);
But in this way it generates a problem that no result appears in the child executable. and if I put it like this:
int n = atoi(argv[2]);
It printable the results of the child executable, but in the wrong way.
son.c:
int main( int argc, char *argv[] )
{
int n = atoi(argv[2]);
printf("Filho #%d,n);
exit(0);
}
Expected output:
Son # 1 Son # 2 Son # 3
Output when exiting:
Son # 0 Son # 0 Son # 0