1

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.

Community
  • 1
  • 1
QPTR
  • 1,620
  • 7
  • 26
  • 47
  • 1
    when calling `fopen()`, always check (!=NULL) to assure the operation was successful. Most likely, the program will have to be run from `root` (administrator) mode, otherwise a open for write with fail. – user3629249 Apr 25 '16 at 17:53

1 Answers1

6

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);
fluter
  • 13,238
  • 8
  • 62
  • 100
  • Thanks so much for the answer. I just tried this, and it seem to give error 13 (could you please correct the %s, I think its supposed to be %d because otherwise it gives the datatype error). So after that I tried to do "sudo su" and tried to run the .c file in root. But now it seems to continue forever. As in the output doesn't come out on the terminal. Or in a file. Even though when I actually run the commands in root mode, they seem to work. Or at least give no errors. I 've also tried to run the commands using the system() call. But that doesn't seem to work either. – QPTR Apr 25 '16 at 06:39
  • errno 13 is EACCESS, that means you don't have write permission to the file, so you can su to root, or run the program with sudo. can you be more specific on what issue do you have now? – fluter Apr 25 '16 at 06:49
  • Basically, now when I try to run the program (in privileged mode with `sudo su `), it is just there running on the terminal with no sign of ending. It doesn't seem to be a problem with my code because without trying to flush the cache (or include the above code), it seems to work okay (that is output the times on the terminal as its supposed to do). – QPTR Apr 25 '16 at 06:57
  • So you mean when you run `sudo ./a.out`, your program hangs, while if you su to root, then run `./a.out` it was fine? – fluter Apr 25 '16 at 07:02
  • Nope, actually either doing `sudo ./a` (I am using make) or doing su to root seems to make it hang. While when I run the program without flushing the cache, then it seems to work okay. Not okay as in the ultimate okay but okay as in there seems to be no problem with the code that is running without flushing the cache. Only including the code above, and then running it with `sudo ./a` or `su sudo` seems to make it hang. – QPTR Apr 25 '16 at 07:08
  • It seems you did more than setting the cache. Can you post the entire code? – fluter Apr 25 '16 at 07:09
  • Just added the code. Can I remove it after a little while tho? Its part of an assignment (I'll add it back once the assignment has been checked). – QPTR Apr 25 '16 at 07:15
  • @QPTR sure, it's ok. – fluter Apr 25 '16 at 07:15
  • You code looks fine, it is supposed to be slow this way. You can continueing open, write, close the file in a loop, I'm not sure why are you doing that, but your code runs as expected. Drop caches 700000 times certainly take some time. :) – fluter Apr 25 '16 at 07:20
  • Also you can add some printf in the loop to make sure. – fluter Apr 25 '16 at 07:20
  • Thanks for the printf heads up! Now again its giving `Segmentation fault (core dumped)` but after flushing cache a number of times so like this : `Flushing cache ...` `Flushing cache ... ` `Flushing cache ... ` `error 24: 329244908` `Flushing cache ... ` `Segmentation fault (core dumped)` But that happened coz I was trying to fclose outside the loops (hence error 24: too many open files error). I've put it back to how it was before. And no sign of the "Flushing cache" statement. Hmm. – QPTR Apr 25 '16 at 07:38
  • Additionally, my system is slowing down overall. I wonder if something weird is going on. – QPTR Apr 25 '16 at 07:41
  • I edited your code, please give a try, it should be faster now. – fluter Apr 25 '16 at 07:42
  • Try the second version, at least don't open and close the same file in the loop. – fluter Apr 25 '16 at 07:50
  • Thanks so much. It seems to be flushing alright now. As in not giving any errors. Will take a long time tho. Thanks for the patience. :) – QPTR Apr 25 '16 at 09:32