I just made a simple demo program Cto see how it works fork()and came across something that I do not understand. In this example:
void fork2()
{
printf("A");
fork();
printf("B");
}
Conclusion ABAB, but in this example:
void fork2()
{
printf("A\n");
fork();
printf("B\n");
}
Output ABB(single lines, of course). The second one makes sense, because if I understand correctly fork(), he gives birth to a new child, which begins immediately after where the event occurred fork()(therefore, printf("B\n");in this case). I don’t understand why the output is different from when I include a new line character.
source
share