Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fclose() function slow

Tags:

c++

I tried to create around 4 GB file using c++ fopen, fwrite and fflush and fclose functions on Linux machine, but I observed that fclose() function is taking very long time to close the file, taking around (40-50 seconds). I checked different forum to find the reason for this slowness, changed the code as suggested in forums, Used setvbuf() function to make unbuffered I/O as like write() function. but still could not resolve the issue.

        totalBytes = 4294967296 // 4GB file
        buffersize = 2000;    
        while ( size <= totalBytes )
        {
            len = fwrite(buffer, 1, bufferSize, fp);
            if ( len != bufferSize ) {
                cout<<"ERROR (Internal): in calling ACE_OS::fwrite() "<<endl;
                ret = -1;
            }
            size = size + len;
        }
        ...
        ...
        ...
        fflush(fp);
        flcose(fp);

Any solution to the above problem would be very helpful.

thanks, Ramesh

like image 265
Ramesh Avatar asked Dec 21 '22 17:12

Ramesh


1 Answers

The operating system is deferring actual writing to the disk and may not actually write the data to the disk at any writing operation or even at fflush().

I looked at the man page of fflush() and saw the following note:

Note that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

(there's a similar note for fclose() as well, although behaviour on your Linux system seems different)

like image 119
Mikko Juola Avatar answered Jan 02 '23 19:01

Mikko Juola