The printf call would run one or more write(2) system calls, and the order they are processed would be the actual order of the output. One or more, because it depends on the buffering inside the C library. With line-buffered output (going to the terminal), you'd likely get two write calls, once for the initial newline, and another for the rest.
write(1, "\n", 1);
write(1, " 1234567890 \n", 13);
It's possible for another process to be scheduled between the calls, giving
first the two empty lines, then the lines with the digits, but given that there's not much processing going on, it's unlikely on an unloaded system.
Note that since both processes print the exact same thing, it doesn't matter which one goes first, as long as one does not interrupt the other.
If the output goes to a file or a pipe, the default is for it to be fully buffered, so you'd probably only get one write call (per process), and no chance of mixed output.
Your example of intermixed digits would be possible if the digits were being output one-by-one, with individual system calls. It's hard to understand why a sensible library implementation would do that when printing a static string whose length is known. With more writes in a loop, intermixed output would be more likely:
Something like this:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
setbuf(stdout, NULL); /* explicitly unbuffered */
int x = fork();
for (i = 0 ; i < 500 ; i++) {
printf("%d", !!x);
}
if (x) {
wait(NULL);
printf("\n");
}
return 0;
}
Gives me output like below. Most of the time that is, not always. It's up to the system to decide how to schedule the processes. The unpredictability is why we usually try to avoid race-conditions.
111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111111111111
111100000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000010000001001100
110000000011001100110011000000010011001100110000000100110011001100000001001
100110011000000010011001100110000000100110011001100000001001100110011000000
...