Description
System Calls, Basic I/O
Submission & General Guidelines
● This exercise must run properly on the provided virtual machine
● Submit your answers, in the course website only, as a single file named ex1-YOUR_ID.zip (e.g. ex1-012345678.zip), containing only:
o ex1.pdf o ex1.c
Your zip file should NOT contain any folders, just the two files
● Place your name and ID at the top of every source file, as well as in the PDF with the answers.
● No late submission will be accepted.
● Do not submit handwritten work.
● Please provide concise answers, but make sure to explain them.
● Write clean code (readable, documented, consistent, etc…)
Part 1 (36 points)
In this question we will implement a C application that appends content from a file to another. The application should receive the buffer size (in bytes), source file path and target file path as command line arguments. By default, the application does not create a target file if such does not exist, unless the -f option was specified.
1. Read the manual page for the following System Calls i.e. execute: man 2 read
a. READ(2)
b. RENAME(2)
c. OPEN(2), and Carefully read about the following option flags:
O_RDONLY, O_WRONLY, O_CREAT,O_APPEND O_TRUNC, O_EXCL.
d. CLOSE(2)
2. Read the manual page for the following C Library Calls
a. PERROR(3)
b. PRINTF(3)
c. EXIT(3)
3. Complete the application in the provided ex1.c file (missing parts are marked with //TODO).
Execution output e xamples:
Output Requirements & Testing:
In order to apply a uniform testing procedure to your submissions, your program must output one of the following types of messages (precisely and case-sensitive):
• Unable to open source file for reading
• Unable to open destination file for writing
• Unable to append to destination file
• Unable to append buffer content to destination file
• Unable to read source file
• Unable to close source file
• Unable to close destination file
• Content from file <source_file> was successfully appended to <destination file>
Or one of the various arguments parsing errors, as described in the examples above.
Note that all console outputs are expected to end with a newline as shown in the examples above.
Please notice that the output of your program will be checked, as well as the contents of the file that was created during the append process, if such append takes place.
Guidelines
● Use the manuals. Chapters 2 & 3 are your friends
● Make sure to always close the files you are using
● Always check system calls return value for errors. ALWAYS.
● You are not allowed to use any external / C-Library code that copies files, if you are not sure if you are allowed to use something, ask in the forum.
● Your program must finish executing with EXIT_SUCCESS only when there were no errors (otherwise it must finish executing with EXIT_FAILURE after printing a helpful message).
● Do not change function signatures, if provided.
● Hint: the 4 system calls and 3 libc calls mentioned above are all you need to implement this part of the exercise successfully.
Using Docker for Smoke Testing
• The VM is installed with Docker software (more on that later in the semester).
• When being graded, your solution will be tested in a container identical to the one that will be built using the supplied Dockerfile.
• There are two smoke tests (sanity checks) to this exercise. You can explore the
‘EX1/check_submission/do_not_change’ folder for more details o Test1 – tests a standard scenario: source file and destination file exist and not empty
o Test2 – tests a scenario in which the destination file is missing (and there is no ‘-f’ flag)
• Usage:
o You’re given EX1 folder, containing:
▪ A ‘check_submission’ folder, which contains two tests (Test0, Test1) and a Python script to execute it (check_submission.pyc).
▪ A skeleton file ‘ex1.c’ o Do not move ANY of the files inside check_submission, or you won’t be able to use our tests.
o You should edit ‘ex1.c’ with your solution.
o Each time you want to test your final submission file (zip file):
▪ Open a terminal and go the EX1/check_submission.
▪ copy your submission file (ex1-YOUR_ID.zip) to your machine (anywhere you want)
▪ Execute ‘python check_submission.pyc <full path to your submission file>’. for example:
▪ If the two tests pass, you’ll get:
Part 2 (24 points)
We will now examine the performance of our program from part 1.
1. Create a 5MB file using the following command:
dd if=/dev/zero of=/tmp/test.5mb bs=1M count=5
2. Run your program on this file, preceded with the time(1) command, using the following buffer sizes: 100, 200, 400, 800, 1600, 3200, 6400, 12800, 25600, 51200
$ time ./ex1 -f 51200 /tmp/test.5mb /tmp/test.5mb_dest File /tmp/test.5mb was copied to /tmp/test.5mb_dest
Real 0m0.004s
User 0m0.000s
Sys 0m0.000s
3. Draw a graph (using Google Sheets or Microsoft Excel) with 3 series: Real, User, Sys. The series should show the time each run takes (in milliseconds) [Y axis], as a function of the buffer size [X axis]
4. Explain the graph:
● What is the meaning of Real, User and Sys (use Google)?
_____________________________________________________________________
_____________________________________________________________________ _____________________________________________________________________ ● Why don’t Sys and User sum up to Real?
_____________________________________________________________________
_____________________________________________________________________ _____________________________________________________________________
● Why isn’t Real a straight line, parallel to the X axis?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
5. Hypothetically, if we change the append_file in part 1 to print out to the screen a message each time we read buffer_size bytes, will the running time change significantly?
● IMPORTANT: Do not make this change. Submission with this change will result in 0 points for this question!
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Part 3 (40 points)
Answer ‘Homework 1 Quiz’ in Moodle (10 T/F questions, 4 points each).




Reviews
There are no reviews yet.