I'm writing a benchmark tool in C++ where I want to clear the filesystem memory cache between experiments. I'm aware of the following console commands:
sync
echo 3 > /proc/sys/vm/drop_caches
My question is how can i do this programmatically directly within C++?
Any help is appreciated!
Just write to it :
A slightly better way is to sync just the file systems which contains your descriptors using
syncfs()
. Or even better, simply usefsync()
.fsync()
can fail if your descriptor is invalid. Look forerrno
if it returns-1
.Something like this should do the trick: