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

Similar documents
CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

CSE 333 SECTION 3. POSIX I/O Functions

CSE 333 SECTION 3. POSIX I/O Functions

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

CS240: Programming in C

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

COMP 2355 Introduction to Systems Programming

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

CS , Spring Sample Exam 3

UNIX System Programming

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

File and Directories. Advanced Programming in the UNIX Environment

FILE SYSTEMS. Jo, Heeseung

Files. Eric McCreath

Operating System Labs. Yuanbin Wu

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

UNIX System Calls. Sys Calls versus Library Func

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

The course that gives CMU its Zip! I/O Nov 15, 2001

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

structs as arguments

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

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

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Operating System Labs. Yuanbin Wu

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

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

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

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

Operating System Labs. Yuanbin Wu

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

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

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Lecture 21 Systems Programming in C

Pointers, Arrays, and Strings. CS449 Spring 2016

Operating System Labs. Yuanbin Wu

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

CSC 271 Software I: Utilities and Internals

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Section 3: File I/O, JSON, Generics. Meghan Cowan

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

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

OPERATING SYSTEMS: Lesson 12: Directories

Files and Directories Filesystems from a user s perspective

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

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

OPERATING SYSTEMS: Lesson 2: Operating System Services

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

CS 0449 Sample Midterm

CSCE 313 Introduction to Computer Systems

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

ECE551 Midterm Version 1

Intermediate Programming, Spring 2017*

Lecture 23: System-Level I/O

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

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

Advanced Pointer Topics

Signature: ECE 551 Midterm Exam

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Programming Language B

File I/0. Advanced Programming in the UNIX Environment

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 261 Data Structures. Introduction to C Programming

Memory Allocation. General Questions

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Process Creation in UNIX

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

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

All the scoring jobs will be done by script

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

All the scoring jobs will be done by script

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

15-122: Principles of Imperative Computation, Spring Written Homework 12. Due: Sunday 15 th April, 2018 by 10pm. Name: Andrew ID: Section:

File Descriptors and Piping

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

1. Static Linking. CS3214 Spring 2012 Exercise 5

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

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Communication. Serial port programming

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

Can Great Programmers Be Taught? Experiences with a Software Design Class. John Ousterhout Stanford University

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Transcription:

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016 In all of these questions, please assume the following: Pointers and longs are 4 bytes. The machine is little endian, but that doesn't matter in any of these questions. There are no segmentation violations or bus errors in any of this code. Any assembly code questions use the jassem assembly code. Any assembly code that corresponds to compiled C code is unoptimized. Question 1: The program on the answer sheet for Question 1 reads 16-character strings from standard input, and prints the first 10,000 of them that contain the letter 'A'. This program runs correctly, in that it gives the proper output. However, it has three serious problems with it. Your job is to: 1. Identify the three problems by circling the offending lines of code for each and labeling it A, B and C. 2. For each of A, B, and C, explain why it is a problem. Don't just say, for example, runs too slowly. Instead, explain why it runs slowly. 3. Rewrite the program so that it does not have any of these problems. Question 2: When I type the command ls */f1.txt into the shell, it lists all files named f1.txt that are in sudirectories of the current directory. I want you to write lsf1.c, which works just like ls */f1.txt. If there are no files named f1.txt in the subdirectories of the current directory, simply print None Found. Here are examples. UNIX> ls Dir1 Dir2 Dir3 UNIX> ls Dir1 f1.txt f2.txt f3.txt UNIX> ls Dir2 f0.txt f1.txt UNIX> ls Dir3 Sub f0.txt f2.txt UNIX> ls Dir3/Sub f1.txt Dir1/f1.txt Dir2/f1.txt UNIX> cd Dir3 Sub/f1.txt UNIX> cd../dir2 None Found UNIX> If you are strapped for time, write down your approach to solving it. You'll get some partial credit for that. Question 3: When we compile and run the program below, the first four lines of output are: p: 0x100130 p[0]: 0x100140 p[1]: 0x100150 p[2]: 0x100160 What are the last six lines of output? #include <stdio.h> #include <stdlib.h> #include <string.h> typedef unsigned long UL; main() int **p, *q, *r; int i, j; p = (int **) malloc(sizeof(int *) * 3); for (i = 0; i < 3; i++) p[i] = (int *) malloc(sizeof(int) * 3); printf("p: 0x%lx\n", (UL) p); for (i = 0; i < 3; i++) printf("p[%d]: 0x%lx\n", i, (UL) p[i]); for (j = 0; j < 3; j++) p[i][j] = 256+i*16+j; q = *(p+1); q++; r = *p; printf("\n"); printf("a: 0x%lx\n", (UL) (p+1)); printf("b: 0x%lx\n", (UL) q); printf("c: 0x%lx\n", (UL) *q); printf("d: 0x%lx\n", (UL) *(q+1)); printf("e: 0x%lx\n", (UL) r); printf("f: 0x%lx\n", (UL) r[9]);

CS360 Midterm 1 Spring, 2016 James S. Plank March 10 Page 2 Question 4 Suppose that when this procedure runs, the value of fp is 0x16780. Also, the ret statement in proc is at memory address 0x1234. void proc(int a, int b) int c[3]; d(b, 15); return 4; Please answer the following questions. For parts E through J, I want numbers, not variables or expressions. Part A: What is the first line of assembly code in proc? Part B: What is the second line? Part C: What is the assembly instruction at address 0x122c? Part D: What is the assembly instruction at address 0x1230? Part E: What is the value of sp right after the jsr call returns? Part F: What is the address of a? Part G: What is the address of b? Part H: What is the address of c[1]? Part I: When proc returns, what is the value in memory address 0x16768? Part J: At what address can we find the value that the pc will be after proc returns? Question 5 Please convert the following C procedure into assembly code: void a(int b) int i; Question 6 Please convert the last line (the return statement) of the following C procedure into assembly code: void a(int **p) int i; while (c() + d() > 0) b += 5; return b; sscanf( %d, &i); return *(p[i]); Handy prototypes: mempcy(char *to, char *from, int nbytes) char *strchr(char *s, char tofind); int open(char *path, int flags [, int mode ] ) int close(int fd) ssize_t read(int fd, void *buf, size_t count) ssize_t write(int fd, const void *buf, size_t count) FILE *fopen(char *path, char *mode); int fclose(file *f); int fread(void *ptr, int size, int nbytes, FILE *f) int fwrite(void *ptr, int size, int nbytes, FILE *f) More Handy prototypes: strcpy(char *to, char *from) strcat(char *to, char *from) char *strdup(char *s) DIR *opendir(char *path) int closedir(dir *d); struct dirent *readdir(dir *D); int stat(char *path, struct stat buf); /* returns zero on success */ int S_ISDIR(mode_t mode) /* Is the file a directory? */ struct stat mode_t st_mode; /* File mode (see mknod(2)) */ ino_t st_ino; /* Inode number */ /* other stuff omitted */ ; struct dirent char *de_name; /* other stuff omitted */

Put your output one character per underlined place. Question 1 Here's the program circle the offending lines and label them. Explain why A, B and C are bad below: main() char s[160001]; char buf[17]; int n; s[0] = '\0'; n = 0; while (n < 10000 && read(0, buf, 16) == 16) buf[16] = '\0'; if (strchr(strdup(buf), 'A')!= NULL) n++; strcat(s, buf); write(1, s, n*16); exit(0); Rewrite the program:

Question 2 Write the code here. Don't bother with include statements.

Question 3 A: B: C: D: E: F: Write the output here: Question 5 Question 4 Part A: Part B: Part C: Part D: Part E: Part F: Part G: Part H: Part I: Part J: Write your answers here Question 6