Week 11 Laboratory Exercises
Objectives
- learning how to retrieve environment variables
- understand how fork, wait and execve work
- understand how posix spawn works
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
lab11 and changing to this directory.
mkdir lab11 cd lab11
There are some provided files for this lab which you can fetch with this command:
1092 fetch lab11
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
Exercise — individual:
Append to a Diary File
$HOME/.diary
Write a C program, diary.c, which appends 1 line to $HOME/.diary.
The line should be its command-line arguments separated by a space followed by a '\n'.
$HOME represents the HOME environment variable. This means you will need to retrieve
the value of the environment variable, join it with the name of the file we wish to open and then use the resulting
pathname in order to open it.
diary.c should print nothing on stdout. It should only append to $HOME/.diary.
dcc diary.c -o diary ./diary Lisa cat $HOME/.diary Lisa ./diary in this house ./diary we obey the laws of thermodynamics cat $HOME/.diary Lisa in this house we obey the laws of thermodynamics
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest diary
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab11_diary diary.c
You must run give
before Monday 27 July 09:00 (2026-07-27 09:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Compile C Files
compile_c_files.c,
a C program that given the pathname of C source code files compiles them with DCC. For example:
dcc compile_c_files.c -o compile_c_files ls -1 compile_c_files compile_c_files.c file_modes.c file_sizes.c ./compile_c_files file_sizes.c file_modes.c running the command: "/usr/local/bin/dcc file_modes.c -o file_modes" running the command: "/usr/local/bin/dcc file_sizes.c -o file_sizes" ls -1 compile_c_files compile_c_files.c file_modes file_modes.c file_sizes file_sizes.c file file_modes file_modes: ELF 64-bit LSB executable # ...
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest compile_c_files
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab11_compile_c_files compile_c_files.c
You must run give
before Monday 27 July 09:00 (2026-07-27 09:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Find time travelling files
Write a C program, time_travelling_files.c, which takes a list of files
as command-line arguments.
Your program should, for each file, check if the file's access time or the file's modification time is in the future.
If either the file's access time or the file's modification time is in the future, your program should should print a message in the format below.
You can test your program by using the Unix utility touch(1) to set the the access time or modification time of files.
dcc time_travelling_files.c -o time_travelling_files touch a b c d ./time_travelling_files a b c d touch a -a -d "+10 days" touch b -m -d "next Friday" ./time_travelling_files a b c d a has a timestamp that is in the future b has a timestamp that is in the future touch c -m -d "-1 year" touch d -a -d "3 hours ago" ./time_travelling_files a b c d a has a timestamp that is in the future b has a timestamp that is in the future touch c -m -d "now + 5 min" ./time_travelling_files a b c d a has a timestamp that is in the future b has a timestamp that is in the future c has a timestamp that is in the future
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest time_travelling_files
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab11_time_travelling_files time_travelling_files.c
You must run give
before Monday 27 July 09:00 (2026-07-27 09:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Delete non-ASCII characters from a file
Write a C program, leave_only_ascii.c, which takes one argument, a filename.
Your program should remove all non-ASCII bytes from the file.
After it has run the file should contain only ASCII bytes.
It should print nothing on stdout. It should only change the file.
Assume a byte is non-ASCII if it contains a value between 128..255 inclusive.
dcc leave_only_ascii.c -o leave_only_ascii echo -e 'hello\xBAworld' >file2 ls -l file2 -rw-r--r-- 1 z5555555 z5555555 12 Nov 8 08:18 file2 ./non_ascii file2 file2: byte 5 is non-ASCII ./leave_only_ascii file2 ls -l file2 -rw-r--r-- 1 z5555555 z5555555 11 Nov 8 08:18 file2 ./non_ascii file2 file2 is all ASCII
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest leave_only_ascii
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab11_leave_only_ascii leave_only_ascii.c
You must run give
before Monday 27 July 09:00 (2026-07-27 09:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Challenge Exercise — individual:
Compile C Files If Needed
Copy your code from the last activity into compile_if_needed.c
cp compile_c_files.c compile_if_needed.c
Unfortunately the code you have written so far is inefficient.
It recompiles the C file even if this is not necessary.
Modify compile_if_needed.c so that it only compiles C files if necessary.
A compilation is necessary if the binary doesn't exist.
A compilation is also necessary if the C file has been changed since the last compilation. In other words, if the modification time of the C file is more recent than the modification time of the binary.
Follow the output format below.
./compile_if_needed file_sizes.c file_modes.c running the command: "/usr/local/bin/dcc file_modes.c -o file_modes" running the command: "/usr/local/bin/dcc file_sizes.c -o file_sizes" ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling rm file_sizes ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling running the command: "/usr/local/bin/dcc file_sizes.c -o file_sizes" echo >> file_sizes.c # add a new-line to file_sizes.c ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling running the command: "/usr/local/bin/dcc file_sizes.c -o file_sizes" ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest compile_if_needed
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab11_compile_if_needed compile_if_needed.c
You must run give
before Monday 27 July 09:00 (2026-07-27 09:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
In-Class Exercise — group:
Presentation
This week during lab class, you will be divided into small groups and assigned an activity from this week to give a short presentation on (2–5 minutes per group).
There is no give or autotest for the group presentation.
Submission
give.
You can run give multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 12 Monday 09:00:00 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest runs for you.
(Hint: do your own testing as well as running autotest.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1092 classrun -sturec