QPTR
QPTR

Reputation: 1690

Segmentation fault (core dumped) error while trying to flush cache

I am trying out some ways of measuring the TLB size on my machine. I somehow needed to ensure that the CPU does not cache the elements of the array I am using to measure the average access time per page. So I tried this code inside the loop that I have, using the answer over here:

FILE *fp;
fp = fopen("/proc/sys/vm/drop_caches", "w"); 
fprintf(fp, "3"); 
fclose(fp);

However, I am getting the Segmentation Fault (core dumped) error. I have no idea why this could be happening. I am not very good with C and any help would be appreciated. Thanks.

Upvotes: 1

Views: 1171

Answers (1)

fluter
fluter

Reputation: 13806

Be sure to check whether the open of the file was successful, since you are writing to a system file, that certainly requires you run in privileged mode.

FILE *fp;
fp = fopen("/proc/sys/vm/drop_caches", "w");
if (fp == NULL) {
    printf("error %d: %s\n", errno, strerror(errno));
    // error handling, exit or return
}
fprintf(fp, "3"); 
fclose(fp);

Upvotes: 6

Related Questions