I have some doubt about how fprintf()
works. I read that fprintf()
is not guaranteed to perform atomic append operations to the file it's writing to.
What does it mean in practice?
Consider the following simple situation, for example:
Inside the same folder I have different copies of the same code (code below) running simultaneously and printing values into a common file (Data.txt), for example:\
#define N 10000000
int main(){
FILE* fp_Data;
fp_Data=fopen("Data.txt", "a");
srand48(time(NULL));
int i;
double u;
for(i=0; i<N; i++){
u = drand48();
fprintf(fp_Data, "%f\n", u);
}
fclose(fp_Data);
}
could something go wrong (overwritten data/missing data)?
[EDIT]
As pointed by Damien there is a similar question about this problem(Is fprintf() thread safe?)
In that question it talks about from different threads of the same process, not indipendent processes pointing to the same FILE*.
In that case for POSIX standards (which is the one I should consider on any pc with unix SO?) guaranties the thread-safety, meaning I don't risk overwritten/missing data issues. My doubt now is:
Is it also true for indipendent processes pointing to the same FILE*?