Been practicing with those system calls, but I stucked into this code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //first child
printf("\ni'm the first child, my pid is %d", getpid());
fflush(stdout);
break;
default: //parent
sleep(5); /** sleep is generating problems **/
printf("\ni'm the parent process, my pid is %d", getpid());
printf("\ngenerating a new child");
fflush(stdout);
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //second child
printf("\nhere i am, the second child, my pid is %d", getpid());
break;
default: //parent
wait((int *)0);
printf("\nback to parent, my pid is %d", getpid());
}
}
return 0;
}
The output I'm getting is:
i'm the first child, my pid is 6203 i'm the parent process, my pid is 6202 generating a new child back to parent, my pid is 6202 Process returned 0 (0x0) execution time: 5.004 s Press ENTER to continue here i am, the second child, my pid is 6204
What I'm trying it's a simple print of these messages managing the timing via sleep()
.
I can't understand why the program is returning before printing the second child message.
The default case(the one right after the second fork) got printed before its child(second) acting on the output like he's ignoring its wait()
. Its child therefore got printed after the process returns.
I wasn't able to figure out what's the problem. I've marked sleep() function since if I substitute it with wait((int *)0);
the processes flux is working how it was designed to (anyhow, without any timing).
At this point I'm not sure anymore about process flux, or the sleep() usage (man pages wasn't that helpful, way too concise to be honest).