I have a code similar to this:
for (i = 0; i < 3; i++)
{
pid = fork();
if (pid == 0)
{
son_function();
}
if (pid < 0)
{
exit(1);
}
}
void son_function(void)
{
printf("my pid=%d\n", getpid());
printf("%d: alpha\n", getpid());
printf("%d: beta\n", getpid());
printf("%d: charlie\n", getpid());
exit(0);
}
For some reason I can't understand, the order of execution of son_function()
is in reverse order. What I mean is that son_function()
is printing the PID
numbers from the largest to the smallest.
Another thing that freaks me is that the prints for every son will be in the one after the other, there's no way that two prints from two different processes will print to the screen at the same time.
Sample can be seen here: http://ideone.com/uBYyRX