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?