CSE2005- Operating System
Determine the file read time for sequential and random access based of varying
sizes of the files. Take care not to read from cached data - used the raw device
interface. Draw a graph log/log plot of size of file vs average per-block time.
INTRODUCTION
It is about the sequential and random file access in an operating system, and to
simulate a sequential memory access and a random memory access (via the malloc()
function ).
❖ We will deal with :-
create a random file
od -A n -t d -N 1000 /dev/urandom >
//dumpfile2 run dd to write the files and see times taken
How to use dd command
[Link]
#dd if=dumpfile of=speetest bs=1M count=100 conv=fdatasync
We can repeat the same multiple times and note the time taken.
Change the Block size and count size and notice the time.
Check if it went through the cache.
SOUCE CODE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
int max = -1; int mb
= 0; char* buffer;
if(argc > 1) max =
atoi(argv[1]);
while((buffer=malloc(1024*1024)) != NULL && mb != max)
{
memset(buffer, 0, 1024*1024);
mb++;
printf("Allocated %d MB\n", mb);
}
return 0;
}
$ sudo swapoff -a
$ free -m
Find the effect of disk cache on swapping.
$ free -m
./[Link] 500
$ free -m
Clearing the disk cache.
1
CSE2005- Operating System
$ free -m
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
$ free -m
Effects of disk cache on load times
$ cat [Link] print "Hello
World! Love, Python"
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
$ time python [Link]
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
$ free -m
$ dd if=/dev/zero of=bigfile bs=1M count=200
$ ls -lh bigfile
$ free -m
$ time cat bigfile > /dev/null
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
$ time cat bigfile > /dev/null
Sample Source Code (For Sequential File Access):
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <exception>
using namespace std;
void readseq(){ //
sequential read
ifstream fin;
[Link]("dumpfil
e"); char temp;
while(
[Link](temp))
cout << temp;
cout << endl;
[Link]();
}
void randomseek(){
// random seek using rand.
/* an array is created that stores all the used positions every
newly generated random number is checked against the array.
If number already exists in array, then generate new random
number. If number is found that is not in array, then push it into
2
CSE2005- Operating System
the array and read a character */
srand((double)(clock())); // seed rand with current
time ifstream fin; [Link]("dumpfile"); [Link](0,
ios::end); int size = [Link](); [Link](); int *a;
cout<<size<<endl ;
try{ a = new
int[size];
}
catch(bad_alloc& ba){
cerr << "Bad alloc caught: " << [Link]() << endl;
}
char temp; bool used = false, found =
false; int pos = 0, n = 0; while(n < size-
1){ found = false; do{ used = false;
pos = rand()%(size-1); for (int i = 0; i <
n; ++i){ if(a[i] == pos){ used = true;
break;
}
}
if( !used ){
found = true;
a[n] = pos;
n++;
}
}
while( !found ) [Link](a[n-
1], ios::beg);