Operating Systems homework
1. (30 pts total)
(a) (10 pts) Write a C program that reverses the bytes of a given arbitrary length file and outputs them to another specified file, which should be overwritten if it already exists.
So, invoking your program as follows
./reverse infile outfile
will result in the first byte of outfile being the last byte of infile, and so on until the last byte of outfile is the first byte of infile. The given filenames are just an example, make your own infile. You must use fseek()/fread()/fwrite().
(b) (10 pts) Write another C program that reverses the bytes of a given arbitrary length file in-place, as in, the contents of the file are swapped without a temporary file and without producing a second file. Use mmap(). E.g. ./reverse2 infile results in the contents of infile being reversed after the call, and calling ./reverse2 infile twice should result in the same file as you started with.
(c) (10 pts) Compare the performance of ./reverse with ./reverse2. Use the timing function below to target your timing to just the portion of the program which actually does the reversing and produces output. Get the averages of a bunch of runs.
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
// you may use this to convert the contents of the timeval struct to ns long nanosec(struct timeval t){
return((t.tv_sec*1000000+t.tv_usec)*1000);
}
int main(){
int res; struct timeval t1, t2;
res=gettimeofday(&t1,NULL); assert(res==0); //stuff you want to measure might go here res=gettimeofday(&t2,NULL); assert(res==0);
//find average time here
}