fwrite() may be blocked. It uses (usually) an internal buffer with maximum length. It will send data (all or part of its internal buffer) when the buffer is full.
The setbuf() and setvbuf() functions allow you to change the maximum buffer length and actually provide a 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 a guaranteed non-blocking record under any conditions, then you will need potentially infinite buffers, which can be somewhat expensive. You can create your own functions for buffering data (in the RAM block, using realloc() so that it grows when necessary) and writing out (using fwrite() and possible fflush() ) only at the end. Alternatively, you can try using non-blocking I / O, in which the write functions are never blocked, but may respond that they refuse to accept your data due to internal overload. Non-blocking I / O is not part of the C standard itself (there is no f*() function for this, but it can be found under different names on some systems (for example, with fcntl() and write() on Unix systems).
Thomas pornin
source share