file_contention.c 1.12 KB
#define _GNU_SOURCE
#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 buffer[BLOCK_SIZE] __attribute__ ((__aligned__ (512)));
struct timeval start, end;
int fd=0;
char *file;
int i = 0;
unsigned long long time;
int status;
char *names[] = {"1", "2", "3", "4", "5", "6", "7"};
while (i < 7) {
int pid = fork();
if (pid == 0) {
file = strcat(fname, names[i]);
fd = open(fname, O_RDONLY | O_DIRECT);
if (!fd) {
printf("unable to open\n");
exit(0);
}
printf("accessing %n", file);
sleep(2);
gettimeofday(&start, NULL);
while(read(fd, buffer, BLOCK_SIZE));
gettimeofday(&end, NULL);
lseek(fd, 0, SEEK_SET);
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();
exit(0);
}
i++;
}
sleep(10);
wait(&status);
}