University of Toronto Mississauca April 2018 Final Examination CSG209H5S
University of Toronto Mississauca April 2018 Final Examination CSG209H5S
UtorJD: .. . . _^
Family Name(s):
Given Name(s):
The University of Toronto Mississauga and you, as a student, share a commitment to academic integrity. You are
remmded that you may be charged with an academic offence for possessing any miauthorized aids during the writing
of an exam. Clear, sealable, plastic bags have been provided for all electronic devices wifch storage, including but not
limited to: ceU pliones, SMART devices, tablets, laptops, calculators, and MP3 players. Please turn off all devices,
seal them in the bag provided, and place the bag under your desk for the duration of fche examination. You will nofc
be able to touch the bag or ifcs contents until the exam is over.
If) during an exam, any of these items are found on your person or in the area of your desk other than in the clear,
sealable, plastic bag, you may be charged with an academic offence. A typical penalty for an academic offence may
cause you to fail the course.
Please note, once tills exam h&s begun, you CANNOT re-write it.
Do not turn this page until you have received the signal to start.
In tlie meantime, please read the instructions below carefully.
Comments are not required, although they may lielp us mark your answers.
If you use any space for rough work, indicate clearly what you want marked.
You may tear the API page off the back of this exam.
Question 1. [3 MARKS]
Programming Tools
Page 2 of 16 COMT'D. ..
April 2018 Final Examination CSC209H5S
The questions in this secfciou feature a small piece of code and a blank diagram repreeenfciug memory. Your
task is to fill in the diagram so th&t it represents the v^ables and values in the code. Place tlie name
and type of each variable to the left of the diagram, place muneric and strhig values into the appropriate
locationsj iu the diagram (subdividing the boxes when necessary), and represent addresses as arrows from
the pointer location to the locafciou they pomfc to.
Pointers are 8 bytes. lutegers are 4 bytes. If the value of a variable is unspecified, write "???".
In. some cases, variables are men.tkmed without a type. You will need to infer the type of the variable but
may assume that it is located on the stack.
There are 8 points assigned to questions in this section, but the whole section is only worth 6. The
additional points are a bonus because there are some small details tha-fc would be easy to uuss.
Type
and Name Memory
Stack
Heap
Data and
Cocfe
bytes
Page 3 of 16 CONT'D. . .
April 2018 Final Examination CSC209H5S
Type
and Name Memory
Stack
Heap
Data and
Code
8 bytes
Page 4 of 16 CONT'D.
April 2018 Final Examination CSC209H5S
For the first two questions, consider this command which was executed hi the shell:
Recall fchafc the pipe operator redirects STDOUT froiii the process on. fch.e left to STDIN for the process on the
right and that "»" redirects STDQUT to a file but appends to the file, rather than. overwriting it.
Page 5 of 16 CONT'D. ..
April 2018 Final Examination CSC209H5S
In tlie code above, multiple orderiugs of the output (printf statements) can be generated based on how the
operating system schedules processes for execution. How many different orderiugs are possible? You may
assume that all fork and priatf calls succeed.
You may also assume fchafc fche lielpei' function described above main, which generates au array of the
names of the directories witiuu a given directory, is available and works properly, (If you are unsure how
to use this fimction, write a comment — "// Magic Happened - and then assume that you. have access to
a magic array of strings that contains the directory names in yow current working directory.)
Page 6 of 16 GONT'D. ..
April 2018 Final Examination CSC209H5S
// When this function returns, (*subdirs) will refer to a dynamically allocated array of
// strings. Each element in -the array is the full path to a directory that is contained
// in dir. The function returns the number of strings that have been placed in the
// d.yaafliically allocated array pointed to by subdirs,
int gQ-t;^subdirjaames(chaD: *dir, char ***subdirs) ;
int main(void) -C
Page 7 of 16 CONT'D. ..
April 2018 Final Examination CSC209H5S
(a) Can the parent and child still communicate through the pipe, with the parent writing and cluld reading?
Briefly justify your aJafiwer.
(b) If the child exits while the parent is writing data to the pipe, will the parent Imow that the child has
exited? Briefly justify your answer.
Page 9 of 16 CONT'D.
April 2018 Final Examination CSC209H5S
while (1);
return 0;
}
Page 10 of 16 CONT'D.
April 2018 Final Examination CSC209H5S
Page 11 of 16 CONT'D. ..
April 2018 Final Examination CSC209H5S
// fcLset alUds
// i&t max_fd =
while (1) {
Page 12 of 16 CONT'D. ..
April 2018 Final Examination CSC209H5S
Total Marks = 40
Page 13 of 16 CONT>D. ..
April 2018 Final Examination CSC209H8S
Useful macros
Useful structs
struct sigaction {
void (^saJiaadler)(int);
struct stat {
sigset-t sa-mask;
dev-t st_dev; /* ID of device containing file */
int sajElags;
inorb st-lno; /* inode number */
} mode-t stjnode; /* protection */
s{;ruct liosteat {
nlink-t; st-nlink; /* number of hard linlcs */
char *Ii_naiae; // name of host
uid-t st-.uid; /* user ID o£ owner */
char **h-aliases; // alias list
gicLt st-gid; /* group ID of owner */
int h-addrtype; // liost address type
dev-t st-rdev; /* dsvice ID (if special file) */
int hJ.ength; // length of address
off-t st_sizei /* total size, in bytes */
char *h-addr; // address
blksize-t st-blksize; /* blocksize for file system I/O */
} blkcnt-t stjslocks; /* aumber of 512B blocks allocated */
struct sockaddrJLn {
time_t st-atime; /* time of last access */
sa-family-t sin-Eamily;
time-t st-mtime; /* time of last modification */
unsigaed sliort Int sin^port;
time-t st-ctime; /* time of last status change */
struct in-addr sin-addr;
unsigned char pad[8]; /^Unused*/
};
}
Shell comparison operators
Shell Description
-d filename Exists as a directory
"£ filename Exists as a regular file.
Useful Makefile variables
-r filename Exists as a readable file
-w filename Exists as a writable file.
$0 target
~x filename Exists as an executable file.
$" list of prerequisites
-z string True if empty string ~w first prerequisite
stri = str2 True if strl equals s'fcr2
$? return code of last program executed
strl t= str2 True if strl not equal to s-fcr2
intl -eq int2 True if intl equals int2
-ne, -gfc, -It, -Ie For nmcbsrs
!a, >, >=, <, <° For strings
-a, -o And, or.