UNIVERSITY OF TORONTO SCARBOROUGH Computer and Mathematical Sciences APRIL 2016 EXAMINATIONS CSCB09H3S Software Tools & Systems Programming Instructor: Bianca Schroeder and Naureen Nizam Duration: 3 hours Examination Aids Allowed: NONE Student Number: Last (Family) Name(s): First (Given) Name(s): Lecture Section: 01 02 Do NOT turn this page until you have received the signal to start. (In the meantime, please fill out the identification section above, and read the instructions below carefully.) This final examination consists of 9 questions on 21 pages (including this one). A mark of 32 out of 80 is required to pass this course. When you receive the signal to start, please make sure that your copy of the examination is complete. You are not required to add any #include lines, and unless otherwise specified, you may assume a reasonable maximum for characters arrays or other structures. For shell programs, you do not need to include the #!/bin/sh. Note, that on the last 3 pages of the exam we have provided some information for your reference. Good Luck! MARKING GUIDE #1 / 8 #2 / 18 #3 / 8 #4 / 5 #5 / 8 #6 / 10 #7 / 8 #8 / 4 #9 / 11 TOTAL / 80
Question 1 [8 marks] For each of the following statements, mark whether it is true or false. No explanation for your answer is necessary. Part (a) [1 mark] The select system call can be used to simultaneously wait on file descriptors that are read-only and file descriptors that are write-only. o TRUE o FALSE Part (b) [1 mark] Signals are used to send several bytes of data from one process to another. o TRUE o FALSE Part (c) [1 mark] A process whose parent process is stuck in an infinite loop is an orphaned process. o TRUE o FALSE Part (d) [1 mark] If I don t have execute permissions on a directory then I cannot see the contents of the directory. o TRUE o FALSE Part (e) [1 mark] A new process is created only when a running process calls fork(). o TRUE o FALSE Part (f) [1 mark] Blocking a signal means that a signal is thrown away and can never be received. o TRUE o FALSE Part (g) [1 mark] If a pointer is written through a pipe to a child process, it can be used to access memory in the parent process. o TRUE o FALSE Part (h) [1 mark] It is important to free memory that you allocate using malloc before you exit the program because otherwise that memory will no longer be available for other programs to use. o TRUE o FALSE Page 2 of 21
Question 2 [18 marks] Part (a) [2 marks] What is the system call that allows a user program to map a domain name to the server s corresponding IP address and what underlying internet protocol/service is it based on? Part (b) [2 marks] Write a one-line shell command that will concatenate all the files in the current working directory that ends in.h and put them into a file called headers Part (c) [2 marks] Explain what a zombie state of the process is used for. Part (d) [2 marks] Name two problems that could arise if TCP did not add sequence numbers and checksums to each TCP packet? Page 3 of 21
Part (e) [2 marks] Provide one possible reason for why the write system call to a pipe might block and one possible reason for why the read system call to a pipe might block. Part (f) [2 marks] Write two different system calls that a C program could call that would cause the calling process to terminate. If there are arguments to the system call that are required to make a process terminate, state what the values of the arguments must be. Part (g) [4 marks] Assume the contents of the current working directory are: f1 f2 f3 x y z where f1 is an executable program that prints to standard output all command line arguments. Assume that the following shell commands are executed without any errors and that you have permissions to read and execute any of the directory contents. What is printed for each of the following shell commands? (Please note: ` is a back quote and is a single quote). a= f* b=`$a` c= $b echo $a echo $b echo $c echo $? Page 4 of 21
Part (h) [1 mark] Explain when and how a user s PATH variable is used? Part (i) [1 mark] Provide the full shell command you would have to type to add /user/bin to a user's PATH variable. Page 5 of 21
Question 3 [8 marks] Part (a) [1 mark] Fill in the third argument to strncat. Assume that str1 and str2 both contain valid strings. char str1[size1]; char str2[size2]; strncat(str1, str2, ) Part (b) [3 marks] You are asked to help with writing a C program that manages linked lists based on the following data structure: struct node { int value; struct node *next; ; Assuming that the functions to create a new node and inserting to the linked list already exist, your job is to write a function that will search through the entire linked list for a node with a particular value and will return a pointer to that node (or NULL if no such node was found). If multiple nodes with that value exist, return a pointer to the first one. The correct solution should not require more than 10 lines of code. // returns a pointer to the first node with the specified value struct node *search(struct node *head, int value) { Page 6 of 21
Part (c) [2 marks] Considering the following statements in a program, fill in the value for each expressions in the table below. If there is an error explain what the error is. typedef struct node{ int num; struct node *next; Node; Node a, b; Node *c; a.num=10; a.next = NULL; c = &a; c -> num = 20 b = a; b.num = 5 a.num == a.num == Part (d) [2 marks] Complete the C function below. The function returns a pointer to the first occurrence of the character c in the string s. It should return NULL if the character is not found. The correct solution should not require more than 8 lines of code. char *my_strchr(char *s, char c){ Page 7 of 21
Question 4 [5 marks] Part (a) [2 marks] Write the output of the following program in the correct order. Assume that all processes run until they terminate normally. int main() { int p1, p2; printf("a\n"); p1 = fork(); Output: if(p1 == 0) { printf("b\n"); p2 = fork(); if(p2 == 0) { sleep(2); printf("c\n"); exit(0); wait(0); printf("d\n"); exit(0); Part (b) [1 mark] How many processes are created, including the original process? Part (c) [2 marks] Explain how you could move one line of code of the program to a different location (i.e., describe which line of the code and where you would move it) such that as a result the last two lines of output by the program always consist of a D. Page 8 of 21
Question 5 [8 marks] Each of the following code fragments has a bug. Assume that the appropriate headers are included and that no external forces cause any errors, i.e. only consider errors from the program itself. For each program, explain precisely what the error is and how it could be fixed. Vague answers will not receive full marks. Part (a) [2 marks] struct node { int v; struct node *next; ; void f(struct node *n, int v) { n->v = v; n->next = NULL; int main() { int value = 10; struct node *newnode; f(newnode, value); return 0; Part (b) [2 marks] char lic_plate[7] = ABCD ; strncat(lic_plate, 314, 4); Part (c) [3 marks] int main(){ int fd[2]; char *buf1; int i; char buf2; pipe(fd); if(fork() == 0) { close(fd[1]); for(i=0; i<20; i++) { read(fd[0], buf1, 30); printf( %s\n, buf1); close(fd[0]); else { close(fd[0]); buf2 = a ; for(i=0; i<20;i++) write(fd[1], buf2, strlen(buf2)); close(fd[1]); wait(null); Page 9 of 21
Question 6 [10 marks] Write a C program that uses fork and pipe to set up the following pipeline and executes it. You do not need to worry about error checking. uniq < file1 wc You do not need to worry about error checking. The only systems calls you are allowed to use are open, close, dup2, pipe, execlp and fork. int fd[2], pid, filedes; if((pid = fork()) == 0) {/* child */ else if(pid > 0){ /* parent */ Page 10 of 21
Question 7 [8 marks] Part (a) [1 mark] When a process writes to a pipe that has already been closed it gets a SIGPIPE signal and the process terminates. Provide one line of C code that could be added to a C program that would cause the process to ignore the SIGPIPE signal. Part (b) [5 marks] Consider the following program. Assume the program runs without errors. void sig_quit(int signo) { struct sigaction newact; newact.sa_flags=0; sigemptyset(&newact.sa_mask); newact.sa_handler=sig_dfl; printf( caught SIGQUIT\n ); /*F*/ sigaction(sigquit, &newact, NULL); /*G*/ return; int main(void) { sigset_t newmask, oldmask, pendmask; struct sigaction newact; sigemptyset(&newact.sa_mask); newact.sa_handler=sig_quit; newact.sa_flags=0; /*A*/ sigaction(sigquit, &newact, NULL); sigemptyset(&newmask); sigaddset(&newmask, SIGQUIT); /*B*/ sigprocmask(sig_block, &newmask, &oldmask); sleep(10); /*C*/ if(sigismember(&pendmask, SIGQUIT)) printf( SIGQUIT pending\n ); /*D*/ sigprocmask(sig_setmask, &oldmask, NULL); /*E*/ exit(0); Page 11 of 21
Describe what happens if the signal SIGQUIT arrives at the process at each of the letter marks A through E. If any output is produced write it in the Output column, otherwise write NONE. Assume the program is restarted each time. A What happens Output B C D E Part (c) [2 marks] Describe what happens if the signal SIGQUIT arrives for the second time at the process at markers F and G. If any output is produced write it in the Output column, otherwise write NONE. Assume the program is restarted each time. What happens Output F G Page 12 of 21
Question 8 [4 marks] Part (a) [3 marks] Explain what the purpose of each of the following system calls is when setting up a socket for a server: int bind(int sockfd, struct sockaddr *addr, int addrlen) int listen(int sockfd, int n) int accept(int sockfd, struct sockaddr *addr, int *addrlen) Part (b) [1 mark] Which, if any, of the system calls above might block? Page 13 of 21
Question 9 [11 marks] Part (a) [5 marks] In this question you are asked to complete a client program that will open a connection to a server and then send a file to the server. The program takes two command line arguments: the first is the domain name of the host running the server, and the second is the name of the file to send to the server. The client uses socket programming to open a connection to the server and then send the file to the server. To make your task easier you can assume that the following helper function exists and can (should!) be used by your client to send the file to the server: int sendfile(int fd_in, int fd_out) sendfile keeps reading data from the file descriptor fd_in until it reaches end of file and writes all the data it reads to fd_out. Complete the client program below that sends the file to a server. You are not required to do error checking for any of the system calls. The actual number of lines of code you need to write is small (around 6 lines of code): #define SERVER_PORT 3000 int main(int argc, char* argv[]) { int soc, filedes; struct hostend *hp; struct sockaddr_in peer; peer.sin_family=pf_inet; hp=gethostbyname(argv[1]); peer.sin_port=htons(server_port); peer.sin_addr=*((struct in_addr *)hp->h_addr); /*Complete the rest of the code (no error checking required)*/ Page 14 of 21
Part (b) [6 marks] In this part of the question you will implement the helper function sendfile which you used in Part (a). The helper function will take as input arguments the file descriptor for the file to be sent and the file descriptor for the socket. It will use a loop to read all the data from the file in chunks of size BUFSIZ and will write all data it read to the socket. The only system calls that your sendfile implementation is allowed to use are the standard read and write system calls. You can assume that read and write always return non-negative integers, i.e. no errors occur. However, keep in mind that it is not an error if a call to read reads fewer bytes than specified, or if a call to write writes fewer bytes than specified. It s your job to check the return value of read and write, and make sure your program works correctly in those situations (i.e. all bytes from fd_in will be sent to fd_out). Complete the skeleton we provide for sendfile below. You should not need to add more than 8-10 lines of code. int sendfile(int fd_in, int fd_out){ int bytes_read, bytes_written; char buffer[bufsiz]; void *p; Page 15 of 21
**This page can be used if you need additional space for your answers.** Page 16 of 21
**This page can be used if you need additional space for your answers.** Page 17 of 21
**This page can be used if you need additional space for your answers.** Page 18 of 21
Page 19 of 21
Page 20 of 21
Page 21 of 21