I want to know if file IO operations by multiple processes/threads are guaranteed to be sequential consistent on Linux. And if not (as this thread says), how should I code to make sure they are sequential consistent? Consider the following example, where FILE_1
and FILE_2
are two distinct file names with absolute paths where both Process A and B have read-write access.
Process A first creates FILE_1
and then creates FILE_2
:
FILE* fp1A = fopen(FILE_1, "w");
fclose(fp1A);
FILE* fp2A = fopen(FILE_2, "w");
fclose(fp2A);
Process B first reads FILE_2
and if success, reads FILE_1
:
FILE* fp2B = fopen(FILE_2, "r");
if (fp2B != NULL) {
FILE* fp1B = fopen(FILE_1, "r");
// QUESTION: is fp1B guaranteed to be not NULL here?
}
Question is given by the comment above. In other words, if one process does some file IO operations in a given order specified by its source code, are all other processes going to see the effects of these operations on the system in the same order? Is this guaranteed by some standards (POSIX etc.) or implementation defined?
What if I change "file IO" to other operations which have some visible effect on the system in a broader sense (e.g. changing a kernel parameter)?
BACKGROUND: I have been studying memory ordering in the C++11 thread model. But those concepts only concerns memory rather than OS functionalities such as file IO. I understand this is because it is a language standard independent of OS. So I want to know if any other standards provide similar concepts for OS.