Question
Write a C progrram, unixio.c, which reads a given file using Unix open()/read() system calls as well as C fopen()/fgetc()/fread() functions and measure how long
Write a C progrram, unixio.c, which reads a given file using Unix open()/read() system calls as well as C fopen()/fgetc()/fread() functions and measure how long it takes to finish reading the file.
*READ THE FILE FROM THE COMMAND LINE
-
open: is a Unix system call that opens a given file.
-
read: is a Unix system call that reads a file into a given buffer.
-
close: is a Unix system call that closes a given file.
-
fopen: is a C standard I/O function that opens a given file.
-
fgetc: is a C standard I/O function that reads one byte from a file.
-
fread: is a C standard I/O function that reads a file into a given data structure.
-
fclose: is a C standard I/O function that closes a give file.
IMPORTANT USE THIS:
#include // open #include // read #include // read #include // read #include // fopen, fread #include
#include // gettimeofday
struct timeval start, end; // maintain starting and finishing wall time.
void startTimer( ) { // memorize the starting time gettimeofday( &start, NULL );
}
void stopTimer( char *str ) { // checking the finishing time and computes the elapsed time gettimeofday( &end, NULL ); printf("%s's elapsed time\t= %ld ",str, ( end.tv_sec - start.tv_sec ) * 1000000 +
(end.tv_usec - start.tv_usec)); }
int main( int argc, char *argv[] ) {
OUTPUT:
Testing your program:
For the testing purpose, you first need to create a file with a specific size, for example, using the truncate utility. As shown below, you can shrink or extend the size of a file named filename to 1M by using the truncate command. If the filename doesn't exist, it will be created
truncate -s 1M filename
Learn the syntax of the truncate utility using the man command man truncate
$gcc -o unixio unixio.c $./unixio filename 1024 1
batch test: create a bash shell script named t.sh with following content:
#!/bin/bash buffersize=(1 256 512 1024 2048 2096) ## Start testing for value in ${buffersize[*]} do ## Testing Unix I/O system calls tcommando="./unixio filename $value 1" eval $tcommando ## Testing C calls tcommando="./unixio filename $value 0" eval $tcommando done ## Testing is done! :-)
Note that you need to set up proper permissions on the above testing script before you execute the script, for example,
chmod +x t.sh , which allows everyone to execute the script.
4
Then, enter ./t.sh
Assume you have successfully executed your program and your program passes your tests, please record your execution output when reading a file by 1, 256, 512, 1024, 2048, and 2096 bytes per read and fread, and save your execution output into a file named a2output.txt by entering
./t.sh> a2output.txt
The following shows example execution outputs when using the above testing script for reading a file by 1, 512, 1024, 2048, and 2096 bytes per read and fread.
Using Unix I/O systems calls to read a file by 1 bytes per read Unix read's elapsed time = 426349 Using C functions to read a file by 1 bytes per fread C fread's elapsed time = 9812
Using Unix I/O systems calls to read a file by 256 bytes per read Unix read's elapsed time = 2002
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started