#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
write(STDOUT_FILENO,"1",1);
if(fork() > 0) {
write(STDOUT_FILENO,"2",1);
write(STDOUT_FILENO,"3",1);
}
else {
write(STDOUT_FILENO,"4",1);
write(STDOUT_FILENO,"5",1);
}
write(STDOUT_FILENO,"\n",1);
return 0;
}
Output is 1 2 3 \n 4 5 \n
Why is that that if I replace all write functions for printf
(without newline character in the end) like write(STDOUT_FILENO,"1",1)==printf("1")
I get 1 2 3 \n 1 4 5 \n
, like the child is copying the line above the fork?