The readings for April 10, 2017 are:
9. Filesystems (Only through Part 4)
The focus of this reading is to introduce basic system calls dealing with files and processes in C.
In your reading10/README.md
, response to the following questions:
Identify which system calls you would use to perform the following actions by briefly listing example C code:
Print out the error message associated with a failed system call.
Truncate a file.
Move to the 10th
byte of a file.
Check if a path is a directory.
Create a copy of the current process.
Replace the code in the current process with another program.
Tell a process to terminate.
Receive the exit status of a child process.
Write the C equivalent, walk.c
, to the Python code below (walk.py
):
#!/usr/bin/env python2.7 import os for name in os.listdir('.'): if os.path.isfile(name): print name, os.path.getsize(name)
This should output something like this:
$ ./walk.py walk.c 728 walk 14496 Makefile 535 README.md 23 walk.o 8272 walk.log 688 walk.py 137
Be sure to include a Makefile that builds the walk.c
program:
# Build walk program $ make Compiling walk.o... Linking walk... # Run walk program $ ./walk walk.c 728 walk 14496 Makefile 535 README.md 23 walk.o 8272 walk.log 688 walk.py 137 # Cleanup $ make clean Cleaning...
Your program must:
Compile with no warnings (be sure to have -Wall
in your CFLAGS
).
Not contain any memory errors (as reported by Valgrind).
Check if system calls fail (when appropriate).
Consider using the following system calls:
opendir: To open a directory.
readdir: To read one directory entry at a time.
stat: To read a file's metadata (including file size).
closedir: To close a directory.
To test your program, you can compare the output of your walk
program
to the output of the walk.py
script:
# Using the bash shell $ diff <(./walk | sort) <(./walk.py | sort)
Feel free to add a test
rule to your Makefile to automate this.
Note: Since the above command requires bash, you must set the
SHELL
variable in your Makefile to bash
:
SHELL= bash
Here is a possible trace:
# Trace $ make -n test echo Compiling walk.o... gcc -g -gdwarf-2 -Wall -std=gnu99 -c -o walk.o walk.c echo Linking walk... gcc -L. -o walk walk.o echo Testing walk... diff <(./walk | sort) <(./walk.py | sort) > walk.log [ `valgrind --leak-check=full ./walk 2>&1 | tee -a walk.log | grep ERROR | awk '{print $4}'` = 0 ] && ! grep -q 'failed' walk.log || cat walk.log
If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.
To submit your assignment, please commit your work to the reading10
folder
in your assignments GitLab repository. Your reading10
folder should
only contain the following files:
Makefile
README.md
walk.c