CSC 209 lab 08 exercises, week of 5 July 2022
[solutions are available (requires teach.cs authentication)]
Attendance
As usual, please either run /u/csc209h/summer/present on the console of a
lab workstation, or get the TA to record your attendance. Please do this
first so that you don't forget.
Remember that you can check that your attendance has been recorded properly at
https://wwwcgi.teach.cs.toronto.edu/~ajr/cgi-bin/auth/present
Part 1: Fork/exec
1) Call fork() twice (just immediately following each other, no if), and then
print the value of getpid().
- Check the man pages to see what #include files you need (in addition
to stdio.h for printf, of course!).
- How many outputs do you expect?
- Note that the pids are all different.
2) Without forking,
call execl() (NOT execlp()) to execute "ls" with one argument, which is
a directory of your choice.
- execl()'s first argument is the program to execute (not just "ls",
but an appropriate path name (i.e. "ls" itself would only work if
you were cd'd to /bin))
- execl()'s subsequent arguments are the members of argv, as many as
you want
- don't forget argv[0], which should be "ls"
- after all of the argv arguments, pass a null pointer-to-char to
terminate the list
Part 2: Malloc example
In lecture we examined writing a function shorten() which takes an argument
which might look like "Tuesday" and returns a new string shortened to
the first three letters, like "Tue".
You can find the solution in /u/csc209h/summer/pub/lab/08/shorten.c
Write a function which takes an
"argv"-like array of strings (specifically, an array of pointers-to-char,
each of which is a string) and returns a new array which is this array
doubled. For example, if argv was originally the two strings "./a.out" and
"file", then the return value would be the array of the four strings
"./a.out", "file", "./a.out", "file".
You can compile this with a main() of your own devising, or you can use
mine in
/u/csc209h/summer/pub/lab/soln/08/part2-main.c
Part 3 (for credit): Linked lists
Part 3 is in
a separate series of web pages.