Cocoa in the Shell

Best buffer size for read & write syscalls

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;
    uint8_t* buffer = NULL;

    stat(argv[1], &buf);

    if (-1 == (fd = open(argv[1], O_RDONLY)))
    {
        perror("open() ");
        return EXIT_FAILURE;
    }
    buffer = malloc(s.st_blksize * sizeof(uint8_t));

    unsigned long ul_bytes = 0;
    while (ul_bytes < s.st_size)
    {
        ssize_t br = read(fd, buffer, s.st_blksize);
        ul_bytes += br;
    }
    free(buffer);
    close(fd);

    return EXIT_SUCCESS;
}
Tags: