I have a simple code to test the fork()
function.
#include<stdio.h>
#include<unistd.h>
#define MAX_COUNT 10
void main(void)
{
pid_t pid;
int i;
fork();
pid = getpid();
for(i = 1; i <= MAX_COUNT; i++)
{
printf("PID = %d, i = %d\n", pid, i);
}
}
It didn't work as I expected.
My expectation is: parent's result and child's result appear alternately. Can someone explain this and teach me how to fix it? Thanks!
PID = 3663, i = 1
PID = 3663, i = 2
PID = 3663, i = 3
PID = 3663, i = 4
PID = 3663, i = 5
PID = 3663, i = 6
PID = 3663, i = 7
PID = 3663, i = 8
PID = 3663, i = 9
PID = 3663, i = 10
PID = 3664, i = 1
PID = 3664, i = 2
PID = 3664, i = 3
PID = 3664, i = 4
PID = 3664, i = 5
PID = 3664, i = 6
PID = 3664, i = 7
PID = 3664, i = 8
PID = 3664, i = 9
PID = 3664, i = 10