I have written the following.
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void) {
int i, pid;
pid = fork();
if(pid == 0)
{
int j;
for(j=0; j < 10; j++)
{
printf("child: %d\n", j);
fflush(stdout);
}
exit(0); /* Note that we do not use exit() */
}
else if(pid > 0)
{
int i;
for(i=0; i < 10; i++)
{
printf("parent: %d\n", i);
fflush(stdout);
}
}
else
{
fprintf(stderr, "couldn't fork");
return;
}
}
When i run it, it prints 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9.
My question is, shouldn't it print them mixed up?
Shouldn't the parent and child process run in parallel instead of sequentially?