Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding buffering behavior of fwrite()

Tags:

c++

c

linux

fwrite

I am using the function call fwrite() to write data to a pipe on Linux.

Earlier, fwrite() was being called for small chunks of data (average 20 bytes) repeatedly and buffering was left to fwrite(). strace on the process showed that 4096 bytes of data was being written at a time.

It turned out that this writing process was the bottleneck in my program. So I decided to buffer data in my code into blocks of 64KB and then write the entire block at a time using fwrite(). I used setvbuf() to set the FILE* pointer to 'No Buffering'.

The performance improvement was not as significant as I'd expected.

More importantly, the strace output showed that data was still being written 4096 bytes at a time. Can someone please explain this behavior to me? If I am calling fwrite() with 64KB of data, why is it writing only 4096 bytes at a time?

Is there an alternative to fwrite() for writing data to a pipe using a FILE* pointer?

like image 434
Shailesh Tainwala Avatar asked Apr 25 '12 07:04

Shailesh Tainwala


1 Answers

The 4096 comes from the Linux machinery that underlies pipelines. There are two places it occurs. One is the capacity of the pipeline. The capacity is one system page on older versions of Linux, which is 4096 bytes on a 32 bit i386 machine. (On more modern versions of Linux the capacity is 64K.)

The other place you'll run into that 4096 bytes problem is in the defined constant PIPE_BUF, the number of bytes that are guaranteed to be treated atomically. On Linux this is 4096 bytes. What this limit means depends on whether you have set the pipeline to blocking or non-blocking. Do a man -S7 pipe for all the gory details.

If you are trying to exchange huge volumes of data at a high rate you might want to rethink your use of pipes. You're on a Linux box, so shared memory is an option. You can use pipes to send relatively small amounts of data as a signaling mechanism.

like image 189
David Hammen Avatar answered Oct 01 '22 15:10

David Hammen