file_cache.c 1.09 KB
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#define BLOCK_SIZE 1024
void main()
{
char fname[10]="file";
char *fno[] = {"1", "2", "3", "4", "5", "6", "7"};
char *file;
char buffer[BLOCK_SIZE];
struct timeval start, end;
int fd=0;
int i, j;
unsigned long long time;
int status;
for (i = 0; i < 7; i++) {
file = strcat(fname, fno[i]);
printf("File %d: %s\n", i, file);
fflush(NULL);
fd = open(file, O_RDONLY);
if (!fd) {
printf("unable to open\n");
exit(0);
}
while(read(fd, buffer, BLOCK_SIZE));
lseek(fd, 0, SEEK_SET);
gettimeofday(&start, NULL);
for (j = 0; j < 10; j++) {
while(read(fd, buffer, BLOCK_SIZE));
lseek(fd, 0, SEEK_SET);
}
gettimeofday(&end, NULL);
time = (end.tv_sec*1e6+end.tv_usec) - (start.tv_sec*1e6+start.tv_usec);
printf("timestamp %llu - %llu\n", end.tv_sec*1e6+end.tv_usec, start.tv_sec*1e6+start.tv_usec);
printf("time taken to read file # %d - %llu\n", i, time);
sync();
status = system("sudo /sbin/sysctl vm.drop_caches=3");
sync();
strcpy(fname, "file");
}
}