0

For the code snippet below, does the file is guaranteed to be written to the disk just after\when the std::fstream is destroyed?

//the writer
#include<iostream>
#include<fstream>
#include<chrono>
#include<thread>
using namespace std;
int main(int argc,char* argv[]){
    
    {
        std::ofstream file_writer{"test.txt"};

        file_writer << "when the file is actually written out?" << std::endl;
    }


    std::this_thread::sleep_for(std::chrono::seconds(3600));  //the reader below would be called and this process is still alive.  
    return 0;
}

The code snippet is guaranteed to be called first.

Since the OS may defer the actual operation, which is out of the control of C++, there may be no actual file on the disk right now. I am not sure whether there is any potential problem or not if I call the code snippet below to read the file which is created and written by the first code snippet right after the file_writer has been destroyed.

//the reader
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char* argv[]){
    
    {
        std::ifstream file_reader{"test.txt"};

        file_reader.read();
    }
    
    return 0;
}
John
  • 2,963
  • 11
  • 33
  • Are you suggesting that there is a possibility where you create a file, write to it, close it. Then if you try to open it, Linux will tell you it does not exist? – Quimby Aug 25 '22 at 08:12
  • Can you elaborate `OS may defer the actual operation` with a working example? – Mukul Kumar Aug 25 '22 at 08:20
  • @Quimby I am afraid that `std::ifream` may encounter some problems when the file may be not actually on the hardware medium yet since the `std::oftream` does not guarantee the file has been written to the hardware medium. – John Aug 25 '22 at 08:21
  • @John Both cases are out of scope of C++. OS is responsible for writing and reading, if the OS does not guarantee that writes are visible by subsequent reads, well, there is nothing you can do from C++ to fix that. `std::ifstream` does not read from the hardware, it merely asks the OS. It is also the responsibility of OS to keep the hardware in sync with the R/W API. So I would suggest looking at Linux documentation on exactly which guarantees it provides. AFAIK file creation is atomic in Linux and immediately visible, not sure about commit to HW. – Quimby Aug 25 '22 at 08:28

1 Answers1

1

You have the output stream creating the file, which is usually synchoronous

Then you have the output stream write to file which usually goes through the page cache

So, after running the first program the file is ready for reading with respect to both data and exsistence

The problem is a bit deeper in that you assume an ordering between the runs of the programs which in 100% cases requires you to synchronize externally (via a PID-file or a semaphore)

rostamn739
  • 323
  • 3
  • 8
  • ***So, after running the first program the file is ready for reading with respect to both data and exsistence***. But the file may be not actually on the hardware medium yet. Any idea? – John Aug 25 '22 at 07:57
  • 2
    It does not matter if it is present on the hardware disk as long as subsequent reads read from the page cache (not using Direct IO) – rostamn739 Aug 25 '22 at 07:58