You are on page 1of 2

National University of Singapore

School of Computing
CS2106 Laboratory 9 Semester 1 10/11

Deadline
7 November, 2010, Sunday, 11:59pm.

Platform
This exercise can be done on either Linux-based PCs or sunfire.

Submission
Download the template for your solution:

wget http://www.comp.nus.edu.sg/~cs2106/lab09-A000000X.txt

Rename the file by replacing the string A000000X with your matriculation number.
Enter your answer into the text file and submit the file into IVLE Workbin (a folder named
Lab 9) before the deadline.

Marking Scheme
This lab exercise is worth 5 marks.
0.1 marks will be deducted per minute after the deadline for late submission, and 3 marks
will be deducted for file naming violation or format violation.
In this very short lab, you will measure the time it takes to write to an output device (either
a terminal or disk).

1. (5 marks) Download, compile, and run the following program: http://www.comp.nus.


edu.sg/~cs2106/lab09-output.c. The program writes 1,000,000 x to the standard out-
put.
We are going to use the command time to measure the time taken to by a command. The
time command returns three values. Roughly, real indicates the actual time taken to run
the command; user indicates the time CPU spent running the command; and sys indicates
the time CPU spent running the kernel due to the command.
(a) (1 point) Suppose that you have named your executable a.out. Run:
time ./a.out
time ./a.out > X
Note down the output of the commands above. Which one is faster? Writing to
terminal or to disk?
Note that if you try the second command more than once, the system might not allow
you to overwrite the existing file X. In this case, just replace X with any other file name,
or remove X before you run the command again.
(b) (1 point) Now, change the statement that output x from using write to printf, by
commenting out the call to write and uncommenting the call to printf. Compile,
and repeat the commands above.
time ./a.out
time ./a.out > Y
Note down the output of the two commands above. Which one is faster? Writing to
terminal or to disk?
Is the time taken by printf faster or slower than write?
(c) (3 points) Investigate and explain the differences in the timing you see in writing to
terminal and to disk, and in using printf and write.
(Hint: strace might be useful here).

THE END

Page 2

You might also like