Lab overview
Lab 01
Lab 02
Lab 03
Lab 04
Lab 05
Lab 06
Lab 07
Lab 08
Lab 09
Lab 10
Lab 11
Lab 12
Lab 13
Review

[Course home page]

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().

2) Without forking, call execl() (NOT execlp()) to execute "ls" with one argument, which is a directory of your choice.


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.