I am working with forks at the moment, and I came accross a strange dehavior while trying to get a process printing 10 "." each second and second one printing 5 "*" every couple of seconds.
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
int pid = fork();
//parent
if (pid > 0){
for(int i = 0; i < 5; i++){
printf("\n*\n");
sleep(2);
}
}
//child
else{
for(int i = 0; i < 10; i++){
printf("\n.\n");
sleep(1);
}
}
return 0;
}
With this code I get the following output:
*
.
.
*
.
.
*
.
.
*
.
.
*
.
.
Which is what I expected but if I remove the \n from the printf functions like this
printf("*");
Then I get this output:
*****..........
Is there any explanation as why the order of the output is different ?