Week 10 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
lab10
and changing to this directory.
mkdir lab10 cd lab10
There are some provided files for this lab which you can fetch with this command:
1092 fetch lab10
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 lab10_diary diary.c
You must run give
before Tuesday 05 November 09:00 (2024-11-05 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 lab10_compile_c_files compile_c_files.c
You must run give
before Tuesday 05 November 09:00 (2024-11-05 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 lab10_compile_if_needed compile_if_needed.c
You must run give
before Tuesday 05 November 09:00 (2024-11-05 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.
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 11 Tuesday 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