Question
Write a program scat (simple cat or slow cat) that copies data from the standard input to the standard output. a) Implement the data copying
Write a program scat (simple cat or slow cat) that copies data from the standard input to the standard output.
a) Implement the data copying loop using the C library functions getc()/putc() and using the system calls read()/write() operating on a single byte at a time. Your scat program should accept the command line options -l and -s: The option -l selects the C library copy loop (using getc()/putc()) and the option -s selects the system call copy loop, using one byte at a time read()/write() system calls. In case there are multiple options on the command line, the last option wins. If there is neither a -l nor a -s option, the program uses the C library functions copy loop.
b) Use your scat program to copy a large file to /dev/null (a device file that discards all data) and measure the execution times:
time ./scat -l < some-large-file > /dev/null
time ./scat -s < some-large-file > /dev/null
Repeat the measurements a few times to get stable results. What do you observe? Explain. Use strace to investigate the read/write sizes that are used by the two variants of your program. How many read/write calls in total are executed while copying your large file?
c) Extend your program by implementing another option -p and a copy loop that uses the Linux specific sendfile() system call. Set the amount of data that is copied in each call of sendfile() such that it matches the number of bytes read and written by the C library. Measure the execution time.
time ./scat -p < some-large-file > /dev/null
What do you observe? Explain.
Hand in the source code of your scat program and the results of your analysis. Make sure that your program handles all error situations appropriately. Use the getopt() function of the C library for the command line option parsing.
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