#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main( int argc, char *argv[] ){
FILE *fptr;
pid_t pid;
fptr = fopen("Shared File.txt", "a");
pid = fork();
if( pid > 0 ){ // parent process
int counter = 0;
while( counter < 10 ){
fprintf(fptr, "a");
++counter;
}
wait(NULL);
}
else{
int counter = 0;
while( counter < 5 ){
fprintf(fptr, "b");
++counter;
}
}
return 0;
}
When I execute this code, the file produced by the code contains this message: bbbbbaaaaaaaaaa
Whenever I execute this code, I get same message. Why does not the processes write to file in shuffling order ?
Why does the operating system try to finish the child process at first ?
My expectation about the message is like this: baabbaaabaaabaa There is no continuous transition between the processes.