Answer:
Input file: input.txt
Output file: output.txt
input.txt is a text file of 500 MB.
CODE:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
double count = 0; // to keep the count of writes
// function which reads from input.txt and writes to output.txt
void fun(int* file_in, int* file_out, int BUFF_SIZE)
{
char *buff = (char *)malloc(BUFF_SIZE*sizeof(char));
while(read(*file_in, buff, BUFF_SIZE) != 0)
{
write(*file_out, buff, strlen(buff));
count++;
}
}
int main(int argc, char* argv[])
{
if(argc == 1)
{
printf("Usage: ./program.c [ buff_size(in bytes) ]\\");
return 0;
}
int file_in,file_out; // input and output file pointers
file_in = open("input.txt",O_RDONLY);
if(file_in == -1)
{
printf("Error while open input.txt \\");
return 0;
}
file_out = open("output.txt",O_WRONLY);
if(file_out == -1)
{
printf("Error occured while opening output.txt\\");
return 0;
}
struct timeval t1, t2; // for capture time
double elapsedTime;
// start timer
gettimeofday(&t1, NULL);
// do something
// ...
fun(&file_in, &file_out, atoi(argv[1]));
// stop timer
gettimeofday(&t2, NULL);
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec)*1000.0; // sec to ms
printf("Overall write time: %lf ms\\",elapsedTime);
printf("Per write time: %lf ms\\", elapsedTime/count);
return 0;
Step-by-step explanation:
Please see attachment for output