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

Similar documents
PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

System Programming. Pipes I

15-213/18-243, Spring 2011 Exam 2

CS Operating Systems Lab 3: UNIX Processes

Week 13. Final exam review

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

CS Operating system Fall Midterm I -- Oct 5, 2017 You have 75 min. Good Luck!

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

UNIX System Calls. Sys Calls versus Library Func

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

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

CSC209 Review. Yeah! We made it!

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

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Preview. The pause() System Call. The pause() System Call. The signal() System Call 10/18/2017

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

CS 550 Operating Systems Spring Inter Process Communication

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

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 Creation in UNIX

CGS 3460 Summer 07 Midterm Exam

Process Management 1

CS Operating system Spring Final -- May 8, You have 120 min. Good Luck!

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

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

CSC209H1S Day Midterm Solutions Winter 2010

Process Management! Goals of this Lecture!

CS240: Programming in C

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

COM324 System Programming. Midterm Exam

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Workshop on Inter Process Communication Solutions

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Operating Systems and Networks Assignment 2

CSI Module 2: Processes

CMSC 313 Spring 2010 Exam 3 May 17, 2010

First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems

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

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSE 124 Discussion Section Sockets Programming 10/10/17

Tutorial on Socket Programming

Problem Set: Processes

Computer Systems Assignment 2: Fork and Threads Package

Server-side Programming

Inter-process communication (IPC)

Section 2: Processes

10. I/O System Library

CS240: Programming in C

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 470 Operating Systems Spring 2013 Shell Project

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CS 3214 Final Exam. To be considerate to your fellow students, if you leave early, do so with the least amount of noise.

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Interprocess Communication Mechanisms

Group-A Assignment No. 6

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Interprocess Communication Mechanisms

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

York University Lassonde School of Engineering Department of Electrical Engineering and Computer Science

Select the statements below which accurately describe the operation of this system. This system hangs without producing output.

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

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

Process Management! Goals of this Lecture!

Like select() and poll(), epoll can monitor multiple FDs epoll returns readiness information in similar manner to poll() Two main advantages:

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Creating a Shell or Command Interperter Program CSCI411 Lab

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Piotr Mielecki Ph. D.

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Problem Set: Processes

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Introduction. This project will focus primarily on processes.

Transcription:

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