That's one of the side effects. Each process is scheduled independently by the kernel so you really have no way of knowing which message is going to show up first without waiting for children or something like that. If you run the program repeatedly you should see the messages come up in different orders.
The reason it's showing the pid as 0 inside the child process is because that's what it's supposed to do. This is from 'man fork':
Code:
RETURN VALUE
On success, the PID of the child process is returned in
the parent's thread of execution, and a 0 is returned in
the child's thread of execution. On failure, a -1 will be
returned in the parent's context, no child process will be
created, and errno will be set appropriately.
If you want to print the pids of the child processes you'll have to call getpid() from inside those processes.