i'm trying to print the children of a process backwards, but once i exit the for loop the children are getting printed as 0
for(i = 0; i < counter; i++)
{
switch(pid = fork())
{
case -1:
perror("Could not fork\n");
exit(1);
case 0:
reverse[arrayCount] = getpid();
arrayCount++;
exit(0);
/*parent*/
default:
wait(NULL);
}
}
int count;
for(count = depth; count >= 0; count--)
{
printf("child: %d\n", reverse[count]);
}
for(i = 0; i < counter; i++)
{
switch(pid = fork())
{
case -1:
perror("Could not fork\n");
exit(1);
case 0:
reverse[arrayCount] = getpid();
arrayCount++;
exit(0);
/*parent*/
default:
wait(NULL);
}
}
int count;
for(count = depth; count >= 0; count--)
{
printf("child: %d\n", reverse[count]);
}
Comment