CS , Fall 2008 Final Exam

Similar documents
CS , Fall 2008 Final Exam

CS , Spring 2008 Final Exam

15-213/18-213, Fall 2011 Final Exam

Final Exam Introduction to Computer Systems. May 10, Name: Andrew User ID: Recitation Section: Model Solution fp

CS 4400 Fall 2017 Final Exam Practice

CS , Fall 2007 Exam 1

Final Exam Introduction to Computer Systems. May 3, Name: Andrew User ID: Recitation Section: This is an open-book exam.

CSE351 Spring 2018, Midterm Exam April 27, 2018

Introduction to Computer Systems. Final Exam. May 3, Notes and calculators are permitted, but not computers. Caching. Signals.

Machine/Assembler Language Putting It All Together

Machine-level Programs Procedure

CS429: Computer Organization and Architecture

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Assembly Programming IV

Introduction to Computer Systems. Final Exam. May 3, Model Solution. Andrew User ID: Recitation Section:

15-213/18-243, Fall 2010 Exam 1 - Version A

The Stack & Procedures

Machine-Level Programming V: Unions and Memory layout

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

x86-64 Programming III & The Stack

15-213/ Final Exam Notes Sheet Spring 2013!

Machine Program: Procedure. Zhaoguo Wang

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Recitation 15: Final Exam Preparation

15-213, Fall 2007 Midterm Exam

Machine-Level Programming III: Procedures

15-213, Fall 2007 Final Exam

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

The Stack & Procedures

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

3. Address Translation (25 pts)

Question Points Score Total: 100

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

Areas for growth: I love feedback

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

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

Do not turn the page until 12:30.

Arrays. CSE 351 Autumn Instructor: Justin Hsia

CSE 351 Midterm Exam

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

University of Washington

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

CS 3330 Exam 3 Fall 2017 Computing ID:

Final Exam. Spring Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSE 351 Spring 2017 Final Exam (7 June 2017)

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Machine Programming 3: Procedures

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

15-213/18-213, Fall 2012 Final Exam

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Procedures and the Call Stack

Structs and Alignment

CS , Fall 2004 Exam 1

Do not turn the page until 12:30.

Do not turn the page until 5:10.

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

Do not turn the page until 5:10.

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS429: Computer Organization and Architecture

CS , Spring 2009 Exam 2

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

CSE 351 Midterm - Winter 2017

Data Representa/ons: IA32 + x86-64

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Question Points Score Total: 100

How Software Executes

CS , Fall 2003 Exam 2

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Final Exam. Spring Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSE 351 Midterm - Winter 2015 Solutions

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

1 Number Representation(10 points)

CS240: Programming in C

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

Assembly IV: Complex Data Types

CSE P 501 Exam Sample Solution 12/1/11

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Do not turn the page until 5:10.

CSCI 2021: x86-64 Control Flow

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 29, 10 13:41 Page 1/1 Apr 29, 10 11:51 Page 1/1

CS , Spring 2004 Final Exam

Synchronization: Basics

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Machine-Level Programming (2)

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

How Software Executes

Computer Systems Assignment 2: Fork and Threads Package

Do not turn the page until 12:30.

CPSC 261 Midterm 1 Tuesday February 9th, 2016

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Do not turn the page until 5:10.

Corrections made in this version not seen in first lecture:

CSE 351 Final Exam - Winter 2017

Instruction Set Architecture (ISA) Data Types

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 261: MIDTERM 1 February 14, 2017

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Transcription:

Andrew login ID: Full Name: Recitation Section: CS 15-213, Fall 2008 Final Exam Friday. December 12, 2008 Instructions: Make sure that your exam is not missing any sheets, then write your full name, Andrew login ID, and recitation section (A H) on the front. The exam has a maximum score of 106 points. This exam is OPEN BOOK. You may use any books or notes you like. No calculators or other electronic devices are allowed. 1 (18): 2 (12): 3 (10): 4 (8): 5 (6): 6 (7): 7 (8): 8 (12): 9 (8): 10 (8): 11 (9): Total (106): Page 1 of 21

Problem 1. (18 points): For the following questions, indicate whether each of the statements following them is true or false. 1. The register rax currently has value 0. Which of the following statements are true? Executing movq (%rax), %rcx will cause a segmentation fault. Executing leaq (%rax), %rcx will cause a segmentation fault. Executing movq %rax, %rcx will cause a segmentation fault. Executing addq 8, %rsp will increase the stack allocation by 8 bytes. 2. After a process calls fork(), which of the following are shared by it and the new child process? (By shared, we mean that an update by one process could affect the behavior of the other.) All file table entries corresponding to files open in the parent before the call to fork(). New file table entries corresponding to files open() d by the parent after the fork(). New file table entries corresponding to files open() d by the child after the fork(). All of virtual memory. The stack. All register contents. All sockets with connections established before the fork(). 3. Consider the following global variable declaration: char str[80]; Which of the following outcomes should be of significant concern if you write a program that calls gets(str)? There could be a stack overflow leading to execution of malicious code. There could be a buffer overflow that modifies other global variables. There could be a SEGFAULT. You might get a bad score if you did this for your 15-213 lab. Page 2 of 21

4. Consider the following structure definition: struct data { short things[3]; unsigned int doodad; char stuff[8]; Which of the following holds for an x86-64 machine: The size of struct data is 18. The size of struct data is 20. The size of struct data is 24. Network code should use htons() for each entry in things that is sent to another computer. Network code should use htons() for data in stuff that is sent to another computer. 5. When you run two programs on the same Linux machine, why do you not have to worry about them using the same physical memory? The programmers knew to avoid using the same addresses and were careful not to do so. The virtual addresses used by the running programs are translated to non-overlapping physical addresses. Only one program really runs at a time, and the physical memory is saved/restored as part of each context switch. 6. Suppose we compile and run the following code on a processor where integer multiply has a 10 clock cycle latency and a 2 clock cycle issue time. That is, although a single product computation requires 10 clock cycles, the multiplier can start a new computation every 2 clock cycles. int prod = 1; for (i = 0; i < n; i+= 2) { prod = prod * a[i] * a[i+1]; // Line P Which of the following statements hold for this code: It will properly compute the product for any array a of length n. It will have a CPE of 10. It will have a CPE of 2. Changing Line P to be (a[i] * a[i+1]) will change the CPE. Changing Line P to be (a[i] * a[i+1]) could change the computed result. Page 3 of 21

Problem 2. (12 points): We are running programs on a machine where values of type int have a 32-bit two s-complement representation, and values of type long have a 64-bit two s complement representation. Right shifts are performed arithmetically. Values of type double use the 64-bit IEEE format. We generate arbitrary integer values x and y, and convert them to values of type long and double as follows: /* Create some arbitrary values */ int x = random(); int y = random(); /* Convert to long */ long lx = (long) x; long ly = (long) y; /* Convert to double */ double dx = (double) x; double dy = (double) y; For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it can yield 0, give values for the arguments that make the expression evaluate to 0. Hint: Note that some properties may hold for dx and dy that do not hold for arbitrary double-precision floating-point numbers, and similarly lx and ly are not arbitrary long s. Expression Always 1? Arguments (if could yield 0) - x == x+1 x+2*(x+x)+3*x == x<<3 x>>1 == x/2 lx * y == x * ly x + y == lx + ly ly == 0 (lx*ly/ly == lx) dx + dy == (double) (x+y) (x > y) == (dx > dy) Page 4 of 21

Problem 3. (10 points): Consider the following x86 64 assembly code: # On entry, %edi = val, %rsi = K func: pushq %r12 movl %edi, %r12d pushq %rbp movq %rsi, %rbp pushq %rbx xorl %ebx, %ebx jmp.l8.l4: addl $1, %ebx addq $8, %rbp cmpl $15213, %ebx je.l12.l8: cmpq $0, (%rbp) js.l4 movl $8, %edi call malloc testq %rax, %rax movq %rax, %rcx je.l5 testl %r12d, %r12d # Move with sign extend movslq %ebx,%rdx je.l13.l7: movq %rdx, (%rax).l5: popq %rbx popq %rbp movq %rcx, %rax popq %r12 ret.l13: movq (%rbp), %rdx jmp.l7.l12: popq %rbx xorl %ecx, %ecx popq %rbp movq %rcx, %rax popq %r12 ret Page 5 of 21

Fill in the blanks of the corresponding C function: *func(int val, K) { int i; j; for (i = 0; i < 15213; i++) if ( ) { j = ; if ( ) *j = ; return j; return NULL; Page 6 of 21

Problem 4. (8 points): The problem requires understanding how C code accessing structures, unions, and arrays is compiled. Assume the x86-64 conventions for data sizes and alignments. #include "def.h" typedef struct { short x[a][5]; /* Unknown constant A */ double y; float f; double d; str; typedef union{ double t ; str S; int array[a*5]; uni; void setval(str *p, uni *u) { double val = p -> y; u -> t = val; You do not have a copy of the file def.h, in which constants A and B are defined, but you have the following x86-64 assembly code for the function setval: setval: # rdi = p, rsi = u movq 72(%rdi),%rax movq %rax, (%rsi) ret Based on this code, determine the values of the two constants and the size of the union: A = Size of str = bytes Size of uni = bytes Page 7 of 21

Problem 5. (6 points): Consider a system with 24 GB of physical memory (with a 8 KB page size) and a 40 GB disk drive with the following characteristics: 512-byte sectors 800 sectors/track 7,200 RPM (i.e., 8ms to complete one full revolution) 6ms average seek time Imagine an application that MALLOC()s nearly 40 GB of space, initializes it to all zeros, and then randomly selects integers from across the full space and increments them. (Assume that there are no other processes.) A. What percentage of the integers selected would result in page faults? B. What is the average time to service a page fault? (round to the nearest millisecond) C. Approximately how many integers can be incremented per second? (again, rounding is fine) D. How much additional memory would be needed to double the number of integers that could be incremented incremented per second? (again, rounding is fine) Page 8 of 21

Problem 6. (7 points): Consider the following code: #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include "csapp.h" int main(int argc, char **argv) { char c; int file1 = Open_clientfd("http://www.foobarzzz.com/", 80); int file2 = Open_clientfd("http://www.foobarzzz.com/", 80); int file3; rio_readn(file1, &c, 1); rio_readn(file1, &c, 1); rio_readn(file1, &c, 1); rio_readn(file1, &c, 1); rio_readn(file2, &c, 1); file3 = dup(file2); rio_readn(file3, &c, 1); printf("1 = %c\n", c); int pid = fork(); if(pid == 0) { Close(file3); file3 = dup(file1); rio_readn(file3, &c, 1); printf("2 = %c\n", c); rio_readn(file1, &c, 1); printf("3 = %c\n", c); Page 9 of 21

else { waitpid(pid, NULL, 0); printf("4 = %c\n", c); rio_readn(file3, &c, 1); printf("5 = %c\n", c); rio_readn(file1, &c, 1); printf("6 = %c\n", c); Close(file3); Close(file2); dup2(file1, file2); rio_readn(file2, &c, 1); printf("7 = %c\n", c); return 0; Assume that http://www.foobarzzz.com/ when accessed on port 80 serves the string of bytes MACHINES. Also assume that all system calls succeed. What will be the output when this code is compiled and run? You may not need all the lines in the table given below. Output Line Number Output 1 st line of output 2 nd line of output 3 rd line of output 4 th line of output 5 th line of output 6 th line of output 7 th line of output Page 10 of 21

Problem 7. (8 points): 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* Stores two different elements in an array */ 5 int* build_array(int a, int b) 6 { 7 int *ptr = malloc(2); 8 9 /* check that both numbers are different */ 10 if (a!= b) { 11 *ptr = a; 12 *(ptr+4) = b; 13 return ptr; 14 else { 15 return NULL; 16 17 18 19 int main (int argc, char **argv) 20 { 21 int num1,num2; 22 int *ptr = NULL; 23 24 /* 25 * Two numbers are assigned randomly generated integers by the 26 * function "generate_random_integer()". 27 * Assume that function "generate_random_integer()" is 28 * bug-free and behaves as the name suggests. 29 */ 30 num1 = generate_random_integer(); 31 num2 = generate_random_integer(); 32 33 ptr = build_array(num1, num2); 34 35 printf("num1=%d,num2=%d\n", ptr[0], ptr[1]); 36 37 return 0; 38 39 40 The 40-line program shown above has two goals: (1) store two distinct integers in an array (if the integers are same, the program does not store them in the array), and (2) print the contents of the array. We ran the program on the Fish machines, and it compiles fine. Assume that generate random integer works correctly; it generates and returns a random integer. Page 11 of 21

The numbers on the left indicate line numbers. The instructors think that program has four significant problems that need to be fixed. What are the problems? Where do they occur (give line numbers and function names)? And, How do we fix them? Line Number(s) and Function Name: Problem: Fix: Line Number(s) and Function Name: Problem: Fix: Line Number(s) and Function Name: Problem: Fix: Line Number(s) and Function Name: Problem: Fix: Page 12 of 21

Problem 8. (12 points): The code samples below are designed to test your understanding of multithreading, semaphores, and the effects of multiple threads modifying the same data. You will be asked to determine the possible outputs the given code can have. For the purposes of this problem, assume that for printf, argument passing and execution of the function is one atomic operation. sem_t sem; int main() { int j; pthread_t tids[3]; sem_init(&sem, 0, 1); for (j=0; j<3; j++) { pthread_create(&tids[j], NULL, doit, NULL); for (j=0; j<3; j++) { pthread_join(&tids[j], NULL); return 0; This is the main function for all three problems below. The only differences in the problems are the details of the thread function, doit, listed on the next page. Page 13 of 21

part A: int i = 0; void *doit(void *arg) { P(&sem); i = i + 1; V(&sem); printf("%d\n", i); List all possible outputs, using one column of boxes below for each possible output. part B: int i = 0; void *doit(void *arg) { P(&sem); i = i + 1; printf("%d\n", i); V(&sem); List all possible outputs, using one column of boxes below for each possible output. Page 14 of 21

part C: int i = 0; void *doit(void *arg) { i = i + 1; printf("%d\n", i); Consider each of the following outputs in the table below. If an output could possibly be produced by the above code, after all threads are terminated, then fill in the empty box beside it with Y (otherwise N ). Output: 3 2 1 1 1 1 3 2 1 1 1 1 Possible? (Y/N) Page 15 of 21

Problem 9. (9 points): Part A: Consider the following client code to connect to a server that runs at 128.2.222.158 on port 15213, sends a message, and receives a response. The response from the server may be longer than MAXBUF and the server will send an EOF when it is done sending the response. int fd, num; char buf[maxbuf]; struct sockaddr_in server_address; char *msg = argv[1]; if((fd = socket(af_inet, SOCK_STREAM, 0)) < 0) exit(1); bzero((char *)&server_address, sizeof(server_address)); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(0x8002de9e); server_address.sin_port = (unsigned short)15213; if(connect(fd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) exit(1); if((num = rio_writen(fd, msg, strlen(msg) + 1)) < 0) exit(1); if((num = read(fd, buf, MAXBUF)) < 0) exit(1); printf("%s\n", buf); For each of the statements below, indicate whether it is true or false. Statement T/F Short counts are not an issue here because the read call cannot return a short count. The port number 15213 should have an ntohs call around it. The read call should be in a loop to ensure everything from the server is read. The port number 15213 should have an htons call around it. Page 16 of 21

Part B: Consider the following server code that creates a socket and listens for client connections. int listenfd; struct sockaddr_in server_address; if ((listenfd = socket(af_inet, SOCK_STREAM, 0)) < 0) exit(1); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = ntohl(inaddr_any); server_address.sin_port = ntohs((unsigned short)15213); if(bind(listenfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) exit(1); if(listen(listenfd, 5) < 0) exit(0); while (1) { /* process client connections */ For each of the statements below, indicate whether it is true or false. Statement T/F The calls to ntohl and ntohs should be replaced by htonl and htons The calls to listen and bind should be flipped (bind should be before listen) listenfd will accept connections for all requests to port 15213 on any IP address for this host We have to specify an address (instead of using INADDR ANY) for server address.sin addr.s addr in order for the server to accept connections propertly. The server address structure should be zeroed out with bzero before use. Page 17 of 21

Problem 10. (8 points): Consider the following C function. Assume that X and Y are 16 and 16, respectively, making the total number of accesses 2 X Y = 512. int A[X][Y], B[Y][X]; void matrix_transpose() { int i, j; for(i = 0; i < X; i++) for(j = 0; j < Y, j++) B[j][i] = A[i][j]; return; Assume the following: sizeof(int) = 4. The cache is cold when the function is called and the arrays have been initialized elsewhere. Variables i and j and pointers to A and B are stored in registers. A and B are aligned such that A[0][0] begins a cache block, and B[0][0] comes immediately following A[X 1][Y 1]. Case 1: Assume that the cache is a 1KB direct-mapped cache with 8-byte cache lines. Calculate the cache miss rate by giving the number of cache misses. For partial credit briefly explain where collisions occur w.r.t. this access pattern (i.e. using the indices). miss rate = / 512 (3 pts) Page 18 of 21

For your reference, X = 16, Y = 16, and here is the code again: int A[X][Y], B[Y][X]; void matrix_transpose() { int i, j; for(i = 0; i < X; i++) for(j = 0; j < Y, j++) B[j][i] = A[i][j]; return; Assume the cache is the same size as in case 1, but is now 2-way set-associative with an LRU replacement policy. Calculate the cache miss rate by giving the number of cache misses. miss rate = / 512 (3 pts) Would a larger cache size, with the same cache line size, reduce the miss rate? (Y/N) case 1: case 2: Would a larger cache line size, with the same cache size, reduce the miss rate? (Y/N) case 1: case 2: Page 19 of 21

Problem 11. (9 points): Consider the C code below: void handler(int sig) { printf("s\n"); exit(0); main() { int counter, pid; int *pcounter2 = NULL; signal(sigusr1, handler); counter = 1; pcounter2 = (int *)malloc(sizeof(int)); if (pid = fork()) { counter--; printf("%d\n", counter); *pcounter2 = 1; kill(pid, SIGUSR1); waitpid(pid, NULL, 0); printf("%d\n", counter); fprintf(stderr, "%d\n", *pcounter2); else { counter++; printf("%d\n", counter); *pcounter2 = 0; Use the following assumptions to answer the questions: All processes run to completion and no system calls will fail. printf() and fprintf() are atomic and they flush stdout or the specified file descriptor after printing arguments but before returning. Page 20 of 21

A. Consider each of the following outputs to stdout in the table below. If an output could possibly be produced by the above code, after all processes are terminated, then fill in the empty box beside it with Yes (otherwise No ). Output to stdout Possible? (Yes/No) 0s0 020 002 0s20 20s0 2s00 B. Consider each of the following outputs to stderr in the table below. If an output could possibly be produced by the above code, after all processes are terminated, then fill in the empty box beside it with Yes (otherwise No ). Output to stderr Possible? (Yes/No) 0 1 C. In the normal case when all processes have terminated after running the above code on the fish machines, can the operating system successfully reclaim all of its memory? (Yes/No) ALL MEMORY RECLAIMED? Page 21 of 21