Skip to content
Advertisement

Disabling disk cache in linux

In a class project my teacher told us to make some code evaluations (C language) and to do so we need to disable the disk caching during the tests.

Currently I’m using Ubuntu 12.04, how can I do this?

Thanks.

Advertisement

Answer

You need root access to do this. You can run hdparm -W 0 /dev/sda command to disable write caching, where you have to replace /dev/sda with device for your drive:

#include <stdlib.h>
...
system("hdparm -W 0 /dev/sda1");

You can also selectively disable write caching to individual partitions like this: hdparm -W 0 /dev/sda1.

To reenable caching again just use the -W 1 argument.

man hdparm, man system

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement