UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus DECEMBER 2017 EXAMINATIONS. CSC 209H1F Liu, Reid 3 hours. No Examination Aids

Similar documents
UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus APRIL 2016 EXAMINATIONS. CSC 209H1S Michelle Craig, David Liu 3 hours

UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus APRIL 2012 EXAMINATIONS

UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus APRIL 2017 EXAMINATIONS. CSC 209H1S Instructor: Karen Reid Duration: 3 hours

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 7

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

COMP 2355 Introduction to Systems Programming

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

CSC209H1S Day Midterm Solutions Winter 2010

Process Creation in UNIX

UNIVERSITY OF TORONTO SCARBOROUGH Computer and Mathematical Sciences. APRIL 2016 EXAMINATIONS CSCB09H3S Software Tools & Systems Programming

UNIX System Calls. Sys Calls versus Library Func

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

Question 1. Part (a) Part (b) Part (c) Part (d) Part (e) Part (f) APRIL 2012 Final Examination Solutions CSC 209H1S

Operating System Labs. Yuanbin Wu

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Process Management! Goals of this Lecture!

CSE 333 SECTION 3. POSIX I/O Functions

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC209 Review. Yeah! We made it!

structs as arguments

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

File Descriptors and Piping

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

CSE 333 SECTION 3. POSIX I/O Functions

Operating System Labs. Yuanbin Wu

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

CSC209F Midterm (L0101) Fall 1998 University of Toronto Department of Computer Science

Lesson 2. process id = 1000 text data i = 5 pid = 1200

CS 201. Files and I/O. Gerson Robboy Portland State University

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

Process Management 1

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Section 2: Processes

SOFTWARE ARCHITECTURE 3. SHELL

Recitation 8: Tshlab + VM

Student Number: Lab day, time: Instructor: Bianca Schroeder

CSC 271 Software I: Utilities and Internals

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CS240: Programming in C

15-213/ Final Exam Notes Sheet Spring 2013!

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

Pointers about pointers. Announcements. Pointer type. Example

25.2 Opening and Closing a File

Process Management! Goals of this Lecture!

Lecture files in /home/hwang/cs375/lecture05 on csserver.

CSCI 2132 Final Exam Solutions

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

ECE 650 Systems Programming & Engineering. Spring 2018

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

CS240: Programming in C

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

Basic OS Progamming Abstrac7ons

// socket for establishing connections

CS240: Programming in C

Basic OS Progamming Abstrac2ons

UNIX System Programming

Week 13. Final exam review

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Concurrent Servers Dec 2, 2009"

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Interprocess Communication E. Im

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

Oral. Total. Dated Sign (2) (5) (3) (2)

COM324 System Programming. Midterm Exam

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

Recitation 8 Processes, Signals, Tshlab

Naked C Lecture 6. File Operations and System Calls

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 550 Operating Systems Spring Inter Process Communication

Transcription:

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science St. George Campus DECEMBER 2017 EXAMINATIONS CSC 209H1F Liu, Reid 3 hours PLEASE HAND IN No Examination Aids Student Number: Last (Family) Name(s): First (Given) Name(s): 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.) Marking Guide # 1: / 8 # 2: / 6 This final examination consists of 10 questions on 18 pages. A mark of at least 31 out of 77 on this exam is required to pass this course, otherwise your final course grade will be no higher than 47%. 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 character arrays or other structures. Error checking is not necessary unless it is required for correctness or specifically requested. # 3: / 6 # 4: /10 # 5: / 8 # 6: / 8 # 7: / 6 # 8: /10 # 9: / 9 # 10: / 6 Good Luck! TOTAL: /77 Page 1

CSC 209H1F Final Examination DECEMBER 2017 Question 1. [8 marks] Circle the correct answer for the statements below. TRUE FALSE Multiple processes on a machine may use the same port number to listen for connections. TRUE FALSE A SIGPIPE signal is sent when a process makes a write call to a closed socket or pipe. TRUE FALSE The standard line ending used in network communications is \n. TRUE FALSE The function perror is used to print error messages for any function. TRUE FALSE Given the following code, the file fp can be closed immediately after the dup2 call because it is no longer needed. FILE *fp = fopen("myfs", "r"); dup2(fileno(fp), fileno(stdin)); TRUE FALSE Suppose we have a Makefile with a target t. Running make t will always create a file called t in the current working directory. TRUE FALSE The code below copies the fields of b into a. struct node a, b; //missing code to assign values to the fields in b a = b; TRUE FALSE The code below is an example of a memory leak. struct node { int val; struct node *next; ; void free_list(struct node *head) { while(head!= NULL) { free(head); head = head->next; Page 2 of 18

DECEMBER 2017 Final Examination CSC 209H1F Question 2. Part (a) [4 marks] [6 marks] Write a shell script that adds every subdirectory of your current working directory to your PATH variable. Note that the PATH variable requires an absolute path to each subdirectory. Remember that the environment variable PWD stores the absolute path to the current working directory. (Do not recurse into subdirectories.) Part (b) [2 marks] A program my prog in the parent directory of the current working directory prints out some plaintext when run. Write a single shell command that computes the number of words in the output of my prog, and prints this number to the file called outfile. Page 3 of 18

CSC 209H1F Final Examination DECEMBER 2017 Question 3. [6 marks] For each pair of code snippets below, select match if the pair would output the same thing, does not match if the output would be different, or may not match if the output might match, but not always. If you select does not match or may not match, briefly explain why. Part (a) [1 mark] int a = 3; int *b = &a; printf("%d", *b); Selection match does not match may not match Explanation int *c = malloc(sizeof(int)); *c = 3; printf("%d", c); Part (b) [1 mark] int x = 1 << 3; int y = x 1; printf("%d %d\n", x, y); int z = 8; printf("%d %d\n", z, z + 1); Selection match does not match may not match Explanation Page 4 of 18

DECEMBER 2017 Final Examination CSC 209H1F Part (c) [1 mark] int res = fork(); if (res == 0) { execl("/bin/ls", "ls", NULL); exit(42); else if (res > 0) { int status; wait(&status); printf("%d", 42); if (WIFEXITED(status)) { printf("%d", WEXITSTATUS(status)); Selection match does not match may not match Explanation Part (d) [1 mark] char *s = "hillo world"; s[1] = 'e'; printf("%s", s); printf("hello world"); Selection match does not match may not match Explanation Page 5 of 18

CSC 209H1F Final Examination DECEMBER 2017 Part (e) [1 mark] char *s = malloc(strlen("hello world")); strcpy(s, "hello world"); printf("%s", s); printf("hello world"); Selection match does not match may not match Explanation Part (f) [1 mark] char *s = "hello world"; char *t = malloc(sizeof(s)); strcpy(t, s); printf("%s", t); printf("hello world"); Selection match does not match may not match Explanation Page 6 of 18

DECEMBER 2017 Final Examination CSC 209H1F Question 4. [10 marks] Consider the program below. Part (a) [2 marks] (i) What is the value of strlen(course) just before stem returns? (ii) What is the value of strlen(s) just before stem returns? (iii) What is the value of strlen(ptr) just before stem returns? (iv) What does the print statement in main print? Part (b) [8 marks] Draw a complete memory diagram showing all of the memory allocated immediately before stem returns. Clearly label the different sections of memory (stack, heap, global), as well as different stack frames. You must show where each variable is stored, but make sure it s clear in your diagram what is a variable name vs. the value stored for that variable. You may show a pointer value by drawing an arrow to the location in memory with that address, or may make up address values. char *stem(char *course) { char *s = malloc(strlen(course) + 1); strncpy(s, course, strlen(course) + 1); char *ptr = s; while(*ptr!= 'H') { ptr++; *ptr = '\0'; // Draw memory diagram at this point return s; int main() { char *c1 = "CSC209H1F"; char *c2 = stem(c1); printf("%s %s\n", c1, c2); return 0; Page 7 of 18

CSC 209H1F Final Examination DECEMBER 2017 Question 5. [8 marks] Implement the following function according to its documentation. struct pair { char *name; char *value; /* * You are given a null-terminated string, s, in the following form: * - It contains between 1 and 10 lines, where each line except the last ends with '\n' * - Each line is of the form "<name>: <value>", where <name> and <value> are non-empty strings. * Note that there is exactly one space between the colon and value string. * - Each name and value string do *not* contain a colon (but may contain spaces). * * For example: * - "a: bc" * - "name1: a\nname2: b" * - "Content-Type: text/plain\nhost: teach.cs.toronto.edu" * * Your job is to parse the string, storing each name and value in an array of * struct pairs. Any unfilled pairs in the array have their name and value * fields set to NULL (we've initialized this for you). */ struct pair *parse_pairs(char *s) { // Initialize an array (assume there's at most 10 pairs in s). struct pair *data = malloc(10 * sizeof(struct pair)); for (int i = 0; i < 10; i++) { data[i].name = NULL; data[i].value = NULL; return data; Page 8 of 18

DECEMBER 2017 Final Examination CSC 209H1F Question 6. [8 marks] Write a program that takes at least one command-line argument, where each argument is the name of an executable to run. The program should fork a new child for each command-line argument, and the child should call execl to run the executable named by the argument. (The executable itself takes no arguments.) The child should exit with a non-zero value if the execl fails. The parent process should wait for all of its child processes to complete. If all of its child processes exit successfully with an exit status of 0, the parent prints "Success\n". Otherwise, the parent prints "Failure\n". Here is an example of how the program could be called: $./run_all ls date ps Page 9 of 18

CSC 209H1F Final Examination DECEMBER 2017 Question 7. [6 marks] Suppose we want to write a program that does the following: The parent creates a child process. The parent sends the child process a single integer. The child multiplies the integer by 2, then sends the result back to the parent. The parent prints the result to standard output. Here is an incorrect implementation of this program. We have omitted error-checking for brevity. Note that lack of error checking is not the problem. int main() { int val, result; int fd[2]; pipe(fd); Part (a) [2 marks] Explain what could go wrong with the parent process when we run this program. int pid = fork(); if (pid > 0) { // Parent process val = 10; write(fd[1], &val, sizeof(int)); close(fd[1]); else { // Child process read(fd[0], &val, sizeof(int)); close(fd[0]); result = val * 2; write(fd[1], &result, sizeof(int)); close(fd[1]); return 0; Part (b) [2 marks] Explain what could go wrong with the child process when we run this program. // Parent process here read(fd[0], &result, sizeof(int)); close(fd[0]); printf("result: %d\n", result); return 0; Part (c) [2 marks] Briefly explain how to fix the program. Page 10 of 18

DECEMBER 2017 Final Examination CSC 209H1F Question 8. [10 marks] For each of the statements below, explain one example of what would cause the outcomes or results described in the comments in each box. Answers must be precise. result = read(fd, buf, SIZE); // process blocks result = read(fd, buf, SIZE); // result == 0 result = read(fd, buf, SIZE); // result < SIZE result = read(fd, buf, SIZE); // process exits with a segmentation fault result = write(fd, buf, SIZE); // process blocks result = wait(&status); // process blocks result = wait(&status); // returns immediately and result > 0 result = select(maxfd, &rset, NULL, NULL, NULL); // process blocks result = accept(fd, &addr, sizeof(addr)); // process blocks result = accept(fd, &addr, sizeof(addr)); // result > 0 Page 11 of 18

CSC 209H1F Final Examination DECEMBER 2017 Question 9. [9 marks] Below is an implementation of an interactive client that reads a hostname from standard input, makes a connection to a server running on PORT, and then reads two pieces of information from the server: the number of users (as a 4-digit null-terminated string), and the average load (as a 4-digit null-terminated string). Assume connect to server and add host are implemented correctly, and that connect to server will not block. Error checking is omitted for brevity. #define MAXHOSTS 10 struct server { int soc; char host[64]; char number[5]; char load[5]; ; // Add hostname and soc to an empty slot in servers array. void add_host(struct server servers, char *hostname, int soc); // Create a socket and connect to the server at port and hostname. Assume connection succeeds. int connect_to_server(int port, const char *hostname); int main() { fd_set rset, allset; int maxfd; char buf[maxline]; struct server servers[10]; // initialize all strings in servers to empty strings, and the soc field to -1 init_server(servers); FD_ZERO(&allset); maxfd = 0; while (1) { // Get hostname from stdin and make connection read(fileno(stdin), buf, MAXLINE); int soc = connect_to_server(port, buf); add_host(servers, buf, soc); FD_SET(soc, &allset); if (soc > maxfd) maxfd = soc; rset = allset; select(maxfd+1, &rset, NULL, NULL, NULL); // Check to see which hosts have data ready for (int i = 0; i < MAXHOSTS; i++) { if (FD_ISSET(servers[i].soc, &rset)) { read(servers[i].soc, servers[i].number, 5); read(servers[i].soc, servers[i].load, 5); printf("%s: %s %s\n", servers[i].host, servers[i].number, servers[i].load); Page 12 of 18

DECEMBER 2017 Final Examination CSC 209H1F Part (a) [3 marks] When we run the program all the output is correct, but we notice that we cannot type in another host name to check until we have received all of the output from the previous host. There are three problems with the program that need to be fixed to address this issue. Explain each problem. Failing to check for errors is not a problem with the code, and all helper functions are correctly implemented. Part (b) [6 marks] Fix the code by rewriting it below. You do not need to re-write any code above the call to FD ZERO. Page 13 of 18

CSC 209H1F Final Examination DECEMBER 2017 Question 9. (continued) Page 14 of 18

DECEMBER 2017 Final Examination CSC 209H1F Question 10. [6 marks] You are given a binary file that contains an array of structs of the type shown below. Complete the following function according to its documentation. #define MAXNAME 32 typedef struct { char name[maxname]; int offset; int length; Inode; /* Write an Inode containing the fields "name", "offset", and "length" to the * Inode at index "ind" in the file named "filename". Include appropriate error * checking to handle the case where "ind" is not a valid index. * Return 0 if the write was successful, and -1 in the case of error. * Do not use a loop. */ int write_inode(char *filename, int ind, char *name, int offset, int length) { Page 15 of 18

CSC 209H1F Final Examination DECEMBER 2017 This page can be used if you need additional space for your answers. Total Marks = 77 Page 16 of 18

DECEMBER 2017 Final Examination CSC 209H1F C function prototypes and structs: int accept(int sock, struct sockaddr *addr, int *addrlen) int bind(int sock, struct sockaddr *addr, int addrlen) int close(int fd) int closedir(dir *dir) int connect(int sock, struct sockaddr *addr, int addrlen) int dup2(int oldfd, int newfd) int execl(const char *path, const char *arg0,... /*, (char *)0 */); int execvp(const char *file, char *argv[]) int fclose(file *stream) int FD ISSET(int fd, fd set *fds) void FD SET(int fd, fd set *fds) void FD CLR(int fd, fd set *fds) void FD ZERO(fd set *fds) char *fgets(char *s, int n, FILE *stream) int fileno(file *stream) pid t fork(void) FILE *fopen(const char *file, const char *mode) /* mode can be "r", "w", "r+", "w+", "a", "a+" */ int fprintf(file * restrict stream, const char * restrict format,...); size t fread(void *ptr, size t size, size t nmemb, FILE *stream); int fseek(file *stream, long offset, int whence); /* SEEK_SET, SEEK_CUR, or SEEK_END*/ size t fwrite(const void *ptr, size t size, size t nmemb, FILE *stream); pid t getpid(void); pid t getppid(void); unsigned long int htonl(unsigned long int hostlong) /* 4 bytes */ unsigned short int htons(unsigned short int hostshort) /* 2 bytes */ char *index(const char *s, int c) int kill(int pid, int signo) int listen(int sock, int n) void *malloc(size t size); unsigned long int ntohl(unsigned long int netlong) unsigned short int ntohs(unsigned short int netshort) int open(const char *path, int oflag) /* oflag is O_WRONLY O_CREAT for write and O_RDONLY for read */ DIR *opendir(const char *name) int pipe(int filedes[2]) ssize t read(int d, void *buf, size t nbytes); struct dirent *readdir(dir *dir) int select(int maxfdp1, fd set *readfds, fd set *writefds, fd set *exceptfds, struct timeval *timeout) int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) /* actions include SIG_DFL and SIG_IGN */ int sigaddset(sigset t *set, int signum) int sigemptyset(sigset t *set) int sigprocmask(int how, const sigset t *set, sigset t *oldset) /*how has the value SIG BLOCK, SIG UNBLOCK, or SIG SETMASK */ unsigned int sleep(unsigned int seconds) int socket(int family, int type, int protocol) /* family=pf INET, type=sock STREAM, protocol=0 */ int sprintf(char *s, const char *format,...) int stat(const char *file name, struct stat *buf) char *strchr(const char *s, int c) size t strlen(const char *s) char *strncat(char *dest, const char *src, size t n) int strncmp(const char *s1, const char *s2, size t n) char *strncpy(char *dest, const char *src, size t n) long strtol(const char *restrict str, char **restrict endptr, int base); Page 17 of 18

CSC 209H1F Final Examination DECEMBER 2017 int wait(int *status) int waitpid(int pid, int *stat, int options) /* options = 0 or WNOHANG*/ ssize t write(int d, const void *buf, size t nbytes); WIFEXITED(status) WIFSIGNALED(status) WIFSTOPPED(status) WEXITSTATUS(status) WTERMSIG(status) WSTOPSIG(status) Useful structs struct sigaction { void (*sa handler)(int); sigset t sa mask; int sa flags; struct sockaddr in { sa family t sin family; unsigned short int sin port; struct in addr sin addr; unsigned char pad[8]; /*Unused*/ Shell comparison operators Shell Description -d filename Exists as a directory -f filename Exists as a regular file. -r filename Exists as a readable file -w filename Exists as a writable file. -x filename Exists as an executable file. -z string True if empty string str1 = str2 True if str1 equals str2 str1!= str2 True if str1 not equal to str2 int1 -eq int2 True if int1 equals int2 -ne, -gt, -lt, -le For numbers!=, >, >=, <, <= For strings -a, -o And, or. Useful Makefile variables: $@ target $^ list of prerequisites $< first prerequisite $? return code of last program executed Important environment variables: PATH, PWD, HOME Useful shell commands: cat, cut, echo, ls, read, set, sort, test, uniq ps aux - prints the list of currently running processes grep (returns 0 if match is found, 1 if no match was found, and 2 if there was an error) grep -v displays lines that do not match wc (-clw options return the number of characters, lines, and words respectively) diff (returns 0 if the files are the same, and 1 if the files differ) $0 Script name $# Number of positional parameters $* List of all positional parameters "$@" List of all positional parameters where each parameter is quoted $? Exit value of previously executed command Page 18 of 18 End of Examination