Best buffer size for read & write syscalls
Jul 11, 2009
I don't know you, but when I needed to do I/O operations I often wondered what was the best buffer size to read / write bytes.
After some research I finally found, so it's sharing time.
You have to use the stat function. This function fills a struct stat which contains an interesting field (taken from the man) :
u_long st_blksize; /* optimal file sys I/O ops blocksize */
Here is a minimal example on how to use this, which just reads the file passed as argument :
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, const char* argv[])
{
int fd;
struct stat s;
unsigned char* buffer;
stat(argv[1], &buf);
if (-1 == (fd = open(argv[1], O_RDONLY)))
{
perror("open() ");
return EXIT_FAILURE;
}
if (NULL == (buffer = (unsigned char*)malloc(s.st_blksize * sizeof(unsigned char))))
{
close(fd);
perror("malloc() ");
return EXIT_FAILURE;
}
unsigned long ul_bytes = 0;
while (ul_bytes < s.st_size)
{
ssize_t bytes_read = read(fd, buffer, s.st_blksize);
ul_bytes += bytes_read;
}
free(buffer);
close(fd);
return EXIT_SUCCESS;
}