Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is fwrite non-blocking?

Tags:

c

file-io

Before calling fflush can I consider fwrite to be = a non-blocking write? If not, why not and what are my alternatives?

like image 207
John Michaels Avatar asked Mar 22 '10 15:03

John Michaels


People also ask

Is fwrite synchronous?

fwrite(obj,A,' mode ') writes binary data with command line access specified by mode . If mode is sync , A is written synchronously and the command line is blocked. If mode is async , A is written asynchronously and the command line is not blocked. If mode is not specified, the write operation is synchronous.

Can fwrite fail?

Because fwrite() may buffer output before writing it out to the stream, data from prior fwrite() calls may be lost where a subsequent call to fwrite() causes a failure when the buffer is written to the stream.

Is fwrite thread safe?

fwrite() is a function that writes to a FILE*, which is a (possibly) buffered stdio stream. The ISO C standard specifies it. Furthermore, fwrite() is thread-safe to a degree on POSIX platforms.


3 Answers

fwrite() may block. It uses (usually) an internal buffer with a maximum length. It will send the data (all or part of its internal buffer) when the buffer becomes full.

The setbuf() and setvbuf() functions let you alter the buffer maximum length, and actually provide the block for the buffer, but the details are implementation dependent, so you will have to read the documentation for your specific C library.

Conceptually, if you want guaranteed non-blocking writes under all conditions, then you need potentially infinite buffers, which can be somewhat expensive. You could make your own functions for buffering data (within a block of RAM, using realloc() to make it grow when necessary) and write out (with fwrite() and possible fflush()) only at the end. Alternatively, you could try to use non-blocking I/O in which write functions never block but may respond that they refuse to accept your data due to internal congestion. Non-blocking I/O is not part of the C standard itself (there is no f*() function for that) but can be found under various names on some systems (e.g. with fcntl() and write() on Unix systems).

like image 91
Thomas Pornin Avatar answered Oct 03 '22 03:10

Thomas Pornin


Technically fwrite() is a blocking call in that it does not return until the procedure has completed. However the definition of completion for fwrite() is that the data you supply has been written to an internal file buffer. As a side effect some of that buffer may also be written to the disk as part of the fwrite() call but you cannot rely on that behavior. If you absolutely require the data to be on the disk you need to call fflush().

like image 34
Andrew O'Reilly Avatar answered Oct 03 '22 01:10

Andrew O'Reilly


fwrite() is blocking. fwrite() may call fflush() internally at any time.

If all you need it to buffer, then buffer in your own array. fwrite's buffer is typically a few K.

like image 22
Joshua Avatar answered Oct 03 '22 03:10

Joshua