0% found this document useful (0 votes)
24 views14 pages

209 2017

Uploaded by

Dude Nugget
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

209 2017

Uploaded by

Dude Nugget
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
UNIVERSITY OF TORONTO MISSISSAUGA APRIL 2017 FINAL EXAMINATION, CSC209H5s Software ‘Tools and Systems Programming Instructors: Chaturvedi, Potersen Duration: 2 hours Examination Aids: None Student Number: UtorID: Family Name(s): Given Name(s): ‘The University of Toronto Mississauga and you, as a student, share a commitment to academic integrity, You are reminded that you may be charged with an academic offence for possessing any unauthorized aids during the writing of an cxam. Clear, sealable, plastic bags have been provided for all electronic devices with storage, including but not limited to: cell phones, SMART devices, tablets, laptops, calculators, and MP3 players. Pleaso turn off all devices, seal them in the bag provided, and place the bag under your desk for tho duration of the examination. You will not be able to touch the bag or its contents until the exam is over. 1, 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. Plesse note, once this exein has begun, you CANNO'T re-write it Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully. ‘This final examination paper consists of § questions on 16 pages {including this one). When you receive the signal to start, please make sure that your copy of the at /4 {final examination is complete cae #2 je Comments are not required, although they may help us mark your answers, ins * * #3: 2 No error checking is required except where indicated 7 : ‘You do not need to provide include statements. fo: ° I yon use any space for rough work, indicate clesly what you want marked. cle ae ae ae ts TOTAL: /40 ‘You may tear the API page off the back of this exam, Page 1 of 16 Good Luck! CONT'D... April 2017 Final Examination CSC2091158 Question 1. [4 sarxs} Programming Tools Part (a) [3 marks] Assume that you have a project which contains the source files rcopy-client.c, rcopy-server.c, and rcopy.c. The project results in two executables rcopy-client: and reopy-server; each is built using the source file of the same name (with a “.c”) plus reopy.c. Write a Makefile that uses separate compilation to produce the two executables. (“Separate compilation” means that each source file should be compiled separately.) When make is run without a command line argument, both executables should be built. You may assume that there are no header files, and you do not need to write a clean target. Part (b) [1 MARK] You've just run your program fcopy and you seo the dreaded message “Segnentation fault: 11”. Briefly explain how you would use gdb to identify what has caused the error. Page 2 of 16 cow’ April 2017 al Examination CSC209H53 Question 2. [12 marks] Memory Several of the sections of this question feature a small piece of code and a blank diagram representing memory. Your task is to fill in the diagram go that it represents the variables and values in the code, Place the name and type of cach variable to the right of the diagram, place mumoric and string values into the appropriate locations in the diagram (subdividing the boxes when necessary), and represent addresses as arrows from the pointer location to the location they point to, Pointers are 8 bytes. Integers are 4, If the value of a variable is unspecified, write “772”, Tn some cases, variables are mentioned 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 14 points for subquestions in this section, but the question is only worth 12, ‘The additional 2 points are a bonus (because there are many sinall details that would be easy to miss). Part (a) [2 MarKs] ‘Type and Name Memory Stack int age = 37; | x = kage; 7 yee; Heap Data and Code Page 3 of 16 conr’p. Api 2017 Final Examination csC209H5s Part (b) [3 Manxs] Type and Name Memory Stack char *manet = "Ri! Me char name2[3] = "tu"; ~ char *name_pt = nane2; Heap Data and Code Part (c) [2 Marks] Write a code snippet that uses nanei and nane2 from the code above to create the string “Ritu”. Assign this value to the variable full name. Make sure the string is located in dynamic memary. Page 4 of 16 cont’... April 2017 Final Examination cSC209H5s Part (d) {3 Manks} Assume that the malloc call in the following code succeeds. ‘Type and Name Memory Stack i = malloc (sizeof (int)); | ennnaneraneatmenec i= 42; iptr = i; Heap Data and ‘Code Part (e) [1 Manx] Given the following code: int x5 asaign(... paraneters here . print£("Ad\n", x); write a prototype for the fimetion assign that results in the eode printing the value “42. Page 5 of 16 cowr'n. April 2017 Final Examination CSC209158 Part (£) [2 Marks} Type and Name Memory Stack int ‘quarters = malloc(sizeof(int 4) * 4 for (int i= 0; i <4; aH){ CO quarters [i] = ~ malloc (sizeof (int)) ; (quarters [i]) = 4; } Heap Data and Code Part (g) [1 Mark] Write code to free all of the memory allocated in the code in the previous subquestion, Page 6 of 16 cont’, April 2017 Final Examination CSC209H5S Question 3. [12 marks} Processes, Pipe, and Exec Part (a) [2 MARKS] Consider the process ID (PID), file descriptor table, and memory space to be the resources allocated to a process. Which of these resources are identical in the parent and child after the system call fork is called? How do the remaining resources differ between the parent and child processes? Part (b) [2 Manxs} Consider the process ID (PID), file descriptor table, and memory space to be the resources allocated to a process. Which of these resources remain unchanged when the system call exec is called? Which of these resources are modified, and how are they modified? Page 7 of 16 con’. April 2017 Final Examination CSC209H53 Part (c) [2 aarks] In assignment 3, we asked why hard links were difficult to identify. What is a hard link? Why, in a multi-process program, was it difficult to identify them? Part (d) [1 Marx) int main(void) { int id = 0; for (int i = 1; 4 <3; is4) { Af (fork() == 0) ¢ id = 45 printé ("Child %d forked\n", id); } else { printf ("Parent Yd created child %é\n", id, 4); + + return 0; In the code above, multiple orderings of the print statements can be created based on how the operating system schedules processes for execution. How many different orderings are possible? You may assume that the fork and printf calls all succeed. Page 8 of 16 cONr'D... April 2017 Final Examination CSC209H5S Part (e) [1 Manx] Describe one situation where dup2 is commonly used to duplicate the file descriptor of a pipe. Part (f) [4 marxs} White a snippet of code that (a) sets up a pipe, (b) forks, and (c) prepares the pipe so that tho ehild can write data thet the parent reads. Please include all necessary ervor checks in this code and report errors appropriately. Page 9 of 16 cont’... April 2017 Final Examination CSC209H58 Question 4. [3 saris] Signals Part (a) [1 marx] What is a signal? Part (b) [1 MARK] ‘Write code to install the function timeout as a handler for the signal STGALRM. Part (c) [1 Manx] In the lab, why did we block SIGALRM while we were printing? Page 10 of 16 cor’, April 2017 Final Examination CSC209H58 Question 5. [9 marks] Sockets and Select Part (a) [1 ware ‘To set up a socket for a server, you need to call socket, bind, Listen, and accept. What is the purpose of the bind call? Part (b) [1 mari) ‘To continue from part (a), why do we need to call both Listen and accept? Part (c) [1 marx] Why did we need to call htons on the value for the PORT? Page 11 of 16 conn’... April 2017 Final Examination CSC209H5S Part (d) (6 4arxs] ‘Write code for a server program. The server should accept multiple clients simultaneously and accept input: from them. Whenover it receives a message from a. client, it replies with the last: message it received and then saves the message it just received to send to the next client who sends a message. ‘The first client to say anything receives the message “First” since no previous message was received. For example, assume two clients connect to the server. The first client sends the message “A”, It receives the response “First”. The second client closes its connection. It receives no message. A third client connects to the server, It sends the message “B” and receives the response “A”. The first client sends the message “A again”, and it receives the response “B, ‘The initial server socket is set up for you, so you can focus on managing 1/0, You may essume that you will never receive more than MAX CONNECTIONS ai one time. If two messages are received simultaneously, ‘then the server can treat either of them as arriving first. Please include all necessary error checks in this code and report errors appropriately. #define MAX_CONNECTIONS 5 #define MAK_NSG_LEN 128 int main(void) { // Create the socket FD. int sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock fd < 0) { perror("server: socket"); exit (1); } // Code to sot up the server socket (bind and listen) // You may assume this code is correct, so that sock fd is ready to accept connections. Page 12 of 16 cont’, April 2017 Final Examination ‘CSC200H58 C function prototypes: int accept (int sock, struct sockaddr taddr, int +addrlen) char tasctize(const struct tm +tineptr) int bind(int sock, struct sockaddr addr, int addrion) int cloee(int fa) int closedix(DIR air) int connect(int sock, struct sockaddr taddr, int addrien) char *ctime(const tine-t *clock); int dup2(int oldfd, int newid) int execl (const char *path, const char *arg0, ... /+, (char #0 4/)3 int exocvp(const char #file, char argv(]) int fclose(FILE *stroan) int FDLISSETGnt fd, fdset +fds) void FD.SET(int fd, faset *fds) void FD.CLA(int ta, faset *tds) void FDZERO(tdset *£d5) char #fgots(char #5, int n, FILE *stream) int fileno(FILB *stream) pidt fork(void) FILE *fopen(const char *file, const char +node) int fprintf(FILB + restrict stream, const char * restrict format, ...)5 size.t fread(void sptr, sizet size, size.t nmenb, FILE +strean); mt feock(FILE +stream, long offset, int whence); /+ SEEK_SET, SEEK CUR, ox SEEK END+/ size.t fwrite(const void *ptr, size-t size, size nnemb, FILE tstrean); pidt getpid(void); Pidt gotppid(void) ; ‘unsigned long int htonl (unsigned long int hostlong) /+ 4 bytes +/ unsigned short int htons(unsigned short int hostehort) /+ 2 bytes +/ char *index(const char *s, int c) int Kill(int pid, int signo) imt Listen(int sock, int n) woid *nalloc(sizet size); ‘unsigned long int ntohi (unsigned long int netiong) usigned short int ntohs(ansigned short int netshort) int open(const char *path, int oflag) 7+ offag is OLWRONLY | OLCREAT for write and O_RDONLY for read +/ DIR +opendir(const char *nane) int pclose(FILE tetream) int pipe(int filedes(2}) FILE *popon(char *endstr, char *mode) seize.t read(int d, void +but, sizet nbytes) ; struct dirent treaddir(DIR +dir) int select(int maxfdpt, fd.set #readfds, fd.set twritetds, fd.set texceptfds, struct timeval +tineout) int sigaction(int signim, const struct sigaction aact, struct sigaction +oldact) int sigadaset (sigeet.t tect, int signin) int sigemptyset (sigcet.t +set) int sigprocnask(int how, const sigset.t 4set, sigset.t soldset) /+ how hes the value SIGBLOCK, SIG.UNBLOCK, or SIG.SETHASK */ ‘unsigned int sloep(unsigned int seconds) int socket (int family, int type, int protocol) /* family=AF.INET, sype-GOCK STREAM, protocol-0 #/ int sprintf (char #8, const char +foraat, ...) int stat(const char *filenane, struct stat *but) char totrchr(const char +8, int ¢) Page 15 of 16 En op Finat, EXAMINATION April 2017 Final Examination CSC209H58 size.t strlen(const char +8) char tstrncat(char tdost, const char ‘are, size.t n) int strncnp(const char *s1, const char #82, sizet n) char *strncpy(char dest, const char #sre, size.t n) long strtol(const char srestrict str, char trestrict endptr, int base); char *strrchr(const char 0, int c) char tstretr(const char thaystack, const char ¢needle) int aitGint statue) int waitpid(int pid, int #stat, int options) /+ options = 0 or WHOHANG#/ seizet write(int d, const void *buf, size.t nbytes); Useful macros: WIFEXITED(status) WEXITSTATUS (status) WIFSIGNALED(status) —_WTERNSIG(status) WIFSTOPPED(status) _WSTOPSIG(status) Useful structs: struct stat { dov.t stdev; /+ ID of dovico containing #110 +/ struct sigaction { ino.t stino; /+ inode mumber #/ void (ssa handler) (int); node.t st.node; /* protection #/ sigset-t samask; nlinkt stnlink; /* number of hard Links #/ int sa flags uidt st-uid; /* user ID of ower */ } gidt st.gid; /* group ID of ovner ¢/ struct sockaddr.in { dev-t st.rdev; /* device ID (if special file) */ safenilyt sinfenily; offt st-size; /* total size, in bytes */ unsigned short int sin.port; diksizet st.biksize; /* blocksize for file systen 1/0 +/ struct inaddr sinaddr; bikent-t st.blocks; /+ number of 5128 blocks allocated +/ weigned char pad[é]; /#Uaused+/ tine.t statine; /+ tine of last access */ } tine.t stntine; /+ time of last modification */ timet stctime; /+ tine of last status change +/ Useful Makefile variables: % | wildcard Be | target $ | Het of prerequisites $2 [ First prerequisite $7 | return code of Inet progran executed Page 16 of 16 Exp oF FivaL EXAMINATION

You might also like