There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Similar documents
There are 8 total numbered pages, 6 Questions. You have 60 minutes. Budget your time carefully!

There are 12 total numbered pages, 4 Questions. You have 2 hours. Budget your time carefully!

CSE 153 Design of Operating Systems Fall 2018

EECS 482 Introduction to Operating Systems

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CPS 310 first midterm exam, 10/6/2014

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CMSC 412 Project #3 Threads & Synchronization Due March 17 th, 2017, at 5:00pm

Midterm Exam #1 February 28, 2018 CS162 Operating Systems

518 Lecture Notes Week 3

OS 1 st Exam Name Solution St # (Q1) (19 points) True/False. Circle the appropriate choice (there are no trick questions).

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Problem Set 2. CS347: Operating Systems

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

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Eastern Mediterranean University School of Computing and Technology Department of Information Technology. ITEC202 Operating Systems

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

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

CS-537: Midterm Exam (Spring 2001)

Computer Systems Assignment 2: Fork and Threads Package

Operating System Structure

Fall 2015 COMP Operating Systems. Lab #3

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

University of Waterloo CS350 Midterm Examination Model Solution

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Midterm Exam #1 September 28, 2016 CS162 Operating Systems

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

The Kernel Abstraction. Chapter 2 OSPP Part I

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

CS3733: Operating Systems

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

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

(In columns, of course.)

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

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

There are 19 total numbered pages, 8 Questions. You have 2.5 hours. Budget your time carefully!

Synchronization 1. Synchronization

Threads and Concurrency

Midterm - Winter SE 350

Threads and Concurrency

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

Implementation of Processes

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 1 Solution Due 3:15 PM, Wednesday, 2/1/17

CSE 120 Principles of Computer Operating Systems Fall Quarter, 2002 Halloween Midterm Exam. Instructor: Geoffrey M. Voelker

* What are the different states for a task in an OS?

First Midterm Exam September 28, 2017 CS162 Operating Systems

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Review: Program Execution. Memory program code program data program stack containing procedure activiation records

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

CSCE 313: Intro to Computer Systems

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

EECS 482 Introduction to Operating Systems

UNIX Input/Output Buffering

CS533 Concepts of Operating Systems. Jonathan Walpole

Concurrency. Stefan D. Bruda. Winter 2018

Lecture 4: Process Management

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Operating Systems Comprehensive Exam. Spring Student ID # 3/20/2013

Review: Program Execution. Memory program code program data program stack containing procedure activation records

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

Processes. Sanzheng Qiao. December, Department of Computing and Software

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

CS510 Operating System Foundations. Jonathan Walpole

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

CSC369 Lecture 2. Larry Zhang

The Big Picture So Far. Today: Process Management

CMPSCI 377: Operating Systems Exam 1: Processes, Threads, CPU Scheduling and Synchronization. October 9, 2002

Processes. Johan Montelius KTH

Killing Zombies, Working, Sleeping, and Spawning Children

Midterm - Winter SE 350

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Part II Processes and Threads Process Basics

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS 3305 Intro to Threads. Lecture 6

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Review: Program Execution. Memory program code program data program stack containing procedure activation records

A process. the stack

Chap 4, 5: Process. Dongkun Shin, SKKU

Synchronization I. Jo, Heeseung

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Synchronization 1. Synchronization

What is a Process? Processes and Process Management Details for running a program

4.8 Summary. Practice Exercises

Processes (Intro) Yannis Smaragdakis, U. Athens

Transcription:

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING MIDTERM EXAMINATION, February, 2018 Third Year Materials ECE344H1 - Operating Systems Calculator Type: 2 Exam Type: A Examiner D. Yuan Please Read All Questions Carefully! There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully! Please put your FULL NAME, UTORid, Student ID on THIS page only. Name: Student ID: Total Marks Marks Received Question 1 21 Question 2 21 Question 3 21 Question 4 12 Question 5 25 Total 100 Page 1 of 11 pages

Question 1 (21 marks): Multiple choices. There are one or more correct choices for each question. Mark all of them. You will lose marks for wrong choice. (1)(3 marks) Two threads in the same process share: (A) Program counter (B) Heap (C) File descriptors (D) Stack pointer (E) Global variables (F) None of the above Answer: B, C, E (2) (3 marks) Two threads from *different* processes share: (A) Program counter (B) Heap (C) File descriptors (D) Stack pointer (E) Global variables (F) None of the above Answer: F (2) (3 marks) In Unix Shell implementation, exec() is used in (A) parent process and overwrites child address space (B) child process and overwrites parent address space (C) child process and overwrites child address space (D) parent process and overwrites parent address space Answer: C (3) (3 marks) If a kernel-level thread executes "cin >> variable_a" (C++ input operation), which of the followings will happen? (A) The status of all threads in the same process will be changed from Running to Ready (B) Only the status of this thread will be changed from Running to Ready (C) The status of all threads in the same process will be changed from Running to Waiting (D) Only the status of this thread will be changed from Running to Waiting (E) The status of all threads in the same process will be changed from Ready to Waiting (F) Only the status of this thread will be changed from Ready to Waiting (G) The status of this thread will not be changed Answer: D Page 2 of 11 pages

(4) (9 marks) Which of the following operations will trigger the CPU to transition from user mode to kernel mode (i.e., it is an event)? If it is an event, further classify it as either exception or interrupt. A A A process divides an integer by zero A process accesses memory at address 0x00000000 User presses key "a" on the keyboard when using shell B C B C A process executes test-and-set instruction The email client receives an email Function A calls function B (B is not a system call) in a process Question 2 (21 marks): OS161 Consider the following code snippet taken from OS161. (The number at the beginning is the line number.) 1 mips_switch: 2 addi sp, sp, -44 3 sw ra, 40(sp) 4 sw gp, 36(sp) 5 sw s8, 32(sp) 6 sw s7, 28(sp) 7 sw s6, 24(sp) 17 lw sp, 0(a1) 18 19 lw s0, 0(sp) 20 lw s1, 4(sp) 21 lw s2, 8(sp) 22 lw s3, 12(sp) 23 lw s4, 16(sp) Page 3 of 11 pages

8 sw s5, 20(sp) 9 sw s4, 16(sp) 10 sw s3, 12(sp) 11 sw s2, 8(sp) 12 sw s1, 4(sp) 13 sw s0, 0(sp) 14 15 sw sp, 0(a0) 16 24 lw s5, 20(sp) 25 lw s6, 24(sp) 26 lw s7, 28(sp) 27 lw s8, 32(sp) 28 lw gp, 36(sp) 29 lw ra, 40(sp) 30 addi sp, sp, 44 31 j ra 32.end mips_switch Now answer the following questions. Each question should be answered with at most 2 sentences. (1) Which programming language is this code written in? Choose one below: (A) C (B) Java (C) x86 assembly (D) MIPS assembly (E) None of the above Answer: D (2) Explain the purpose of this entire piece of code. Context switch between 2 processes (or more precisely, kernel threads). (3) What are stored in a0 and a1? a0: address of the PCB of the old process. a1: address of the PCB of the new process. (4) If we change the order of line 17 and line 19, what would happen? New process will have wrong s0 value when it is scheduled (s0 will contain value from the old process). (5) At line 3, what information does ra contain? How is this value stored in ra? The PC when the event occurred. It was saved by the CPU. (6) What is the purpose of line 2? Allocate space on the stack of the process running when the event occurred. Page 4 of 11 pages

(7) Which of the following is true? (A) Interrupts are always disabled when this piece of code is executed (B) Interrupts are always enabled when this piece of code is executed (C) None of the above Answer: A Question 3 (21 marks): Fork bomb A fork bomb is a program that will fork an infinite number of long living processes. Thus it will exhaust the system resource. Note that if a program forks an infinite number of processes, but only a small number of these processes are long living (i.e., others terminate quickly), it is not considered as a fork bomb. Now consider the following three programs, and answer (1) whether it is a fork bomb, and (2) what are the first 3 lines of output. Make the following assumptions: - The OS scheduler is first-come-first-serve, i.e., it will schedule the processes in the order of their creation. - Whenever a process makes a system call, the OS will perform a context switch (as long as there are other ready processes), even if the process that is making the system call is older than other ready processes. - The PID of a newly created process is always the smallest positive integer that is unused (so that when a process is terminated and reaped its PID will be reused). - Initially, only PID 1 in the system is used (so the first newly created process will have PID 2), and you can assume the process with PID 1 is always sleeping. - The three programs below are executed independently of each other. Program 1: int g = 0; // global variable int main(int argc, char *argv[]) { pid_t pid; g++; if (g < 3) { pid = fork(); if (pid) { waitpid(pid); exec(argv[0]); // argv[0] contains the pathname of this program printf("process: %d\n", getpid()); Page 5 of 11 pages

Is this program a fork bomb? Choose one from below: (A) This program is a fork bomb (B) This program is not a fork bomb, but it will fork infinite number of processes (C) This program is not a fork bomb, and it will not fork infinite number of processes (D) None of the above Answer: B Write the first three lines of output below? Process: 3 Process: 3 Process: 3 Program 2: int main(int argc, char *argv[]) { pid_t pid; for (int i = 0; i < 3; i++) { printf("process: %d, start iteration: %d\n", getpid(), i); pid = fork(); if (pid == 0) exec (argv[0]); // argv[0] contains the pathname of this program waitpid(pid); printf("process: %d, end iteration: %d\n", getpid(), i); Is this program a fork bomb? Choose one from below: (A) This program is a fork bomb (B) This program is not a fork bomb, but it will fork infinite number of processes (C) This program is not a fork bomb, and it will not fork infinite number of processes (D) None of the above Answer: A Write the first three lines of output below? Process 2, start iteration: 0 Page 6 of 11 pages

Process 3, start iteration: 0 Process 4, start iteration: 0 Program 3: int main(int argc, char *argv[]) { int pid = fork(); for (int i = 0; pid; i++) { waitpid(pid); printf ("PID1: %d, PID2: %d, i = %d\n", getpid(), pid, i); pid = fork(); Is this program a fork bomb? Choose one from below: (A) This program is a fork bomb (B) This program is not a fork bomb, but it will fork infinite number of processes (C) This program is not a fork bomb, and it will not fork infinite number of processes (D) None of the above Answer: B Write the first three lines of output below? PID1: 2, PID2: 3, i = 0 PID1: 2, PID2: 3, i = 1 PID1: 2, PID2: 3, i = 2 Question 4 (12 marks): Condition variable and semaphore In OS161, suppose we are now using semaphore to implement condition variable. (You only need to consider cv_wait and cv_signal; you do not need to consider cv_broadcast.) Here is the definition of the condition variable: struct cv { char* name; struct semaphore *sem; ; Consider the following implementation of cv_wait() and cv_signal(): struct cv* cv_wait(const cv *cv, struct lock *lock) { Page 7 of 11 pages

lock_release(lock); P(cv->sem); lock_acquire(lock); struct cv* cv_signal(struct cv* cv, struct lock *lock) { V(cv->sem); Assume other parts of the code are the same as in OS161. Does this work? Explain your answer briefly. If you think it does not work, give a thread interleaving to show the problem. No it doesn't work. Here is the interleaving: Thread 1: cv_signal() Thread 2: cv_wait() <- this WON'T cause thread 2 to go to sleep! Question 5 (25 marks): Multiprocessor synchronization We have seen how the semaphore primitives P and V are implemented for uniprocessors in OS161. In this problem, we will implement these primitives for multiprocessors. The relevant code for the OS161 version of these primitives is shown below. The schedule() function moves the current thread to wait queue and chooses another thread from ready queue to run. P(sem) { spl = splhigh(); while (sem->count==0) { thread_sleep(sem); sem->count--; splx(spl); thread_sleep(void *cond) { add_to_wait_queue(cond); schedule(); V(sem) { spl = splhigh(); sem->count++; thread_wakeup(sem); splx(spl); thread_wakeup(void *cond) { thread = remove_from_wait_queue(cond); add_to_ready_queue(thread); In the four questions shown below, circle all (one or more) correct answers. In the answers below, corrupt means an incorrect update. Be careful, the questions might be harder than you thought, and marks will be deducted for each incorrect answer. Page 8 of 11 pages

1) Would the semaphore code shown above work for multiprocessors? (A) No, it could corrupt the sem->count variable. (B) No, it could corrupt the wait queue. (C) No, it could corrupt the ready queue. (D) No, it could cause a thread to wait forever. (E) Yes, it would work. Answer: A, B, C, D 2) Your partner argues that the code would clearly not work for multiprocessors and suggests a minimal change: add spin locks in the thread sleep and thread wakeup implementation. (Recall that spin locks use test-and-set instead of disabling interrupt.) No other change is made to other part of the code. thread_sleep(void *cond) { spin_lock(schedlock); add_to_wait_queue(cond); schedule(); spin_unlock(schedlock); thread_wakeup(void *cond) { spin_lock(schedlock); thread = remove_from_wait_queue(cond); add_to_ready_queue(thread); spin_unlock(schedlock); Would the semaphore code shown earlier now work for multiprocessors? (A) No, it could corrupt the sem->count variable. (B) No, it could corrupt the wait queue. (C) No, it could corrupt the ready queue. (D) No, it could cause a thread to wait forever. (E) Yes, it would work. Answer: A, D. Wait queue and ready queue have no races as they're protected by schedlock, but P and V still have data race. Goes to sleep with schedlock, causing thread wait forever. 3) Your friend from Pepperoni University argues that the code would clearly still not work for multiprocessors and suggests another minimal change (in addition to the change made in part 2): replace interrupt disabling with spin locks in the P and V implementation. P(sem) { while (sem->count==0) { thread_sleep(sem); sem->count--; V(sem) { sem->count++; thread_wakeup(sem); Page 9 of 11 pages

Would this semaphore code work for multiprocessors? (A) No, it could corrupt the sem->count variable. (B) No, it could corrupt the wait queue. (C) No, it could corrupt the ready queue. (D) No, it could cause a thread to wait forever. (E) Yes, it would work. Answer: D. Goes to sleep with schedlock and sem->spinlock, causing thread wait forever. 4) Your partner looks at the code, mutters something about Pepperoni Univ., and suggests adding spin unlock and spin lock around thread sleep in the P implementation (together with the change made in part 2 of this question): P(sem) { while (sem->count==0) { thread_sleep(sem); sem->count--; Would this semaphore code work for multiprocessors? (A) No, it could corrupt the sem->count variable. (B) No, it could corrupt the wait queue. (C) No, it could corrupt the ready queue. (D) No, it could cause a thread to wait forever. (E) Yes, it would work. Answer: D. Goes to sleep with schedlock, causing thread wait forever. 5) After looking at the your partner's solution in part 4, your friend from Pepperoni U proposes another solution by keeping the new change in part 4 while reverting the changes made in part 2. The complete solution is as follows: P(sem) { V(sem) { Page 10 of 11 pages

while (sem->count==0) { thread_sleep(sem); sem->count--; sem->count++; thread_wakeup(sem); thread_sleep(void *cond) { add_to_wait_queue(cond); schedule(); thread_wakeup(void *cond) { thread = remove_from_wait_queue(cond); add_to_ready_queue(thread); Would this semaphore code work for multiprocessors? (A) No, it could corrupt the sem->count variable. (B) No, it could corrupt the wait queue. (C) No, it could corrupt the ready queue. (D) No, it could cause a thread to wait forever. (E) Yes, it would work. Answer: B, C, D. B: b/c now wait_queue in thread_sleep is not protected; C: b/c schedule and add_to_ready_queue access ready queue unprotected. D: because the following interleaving will cause a V to be lost: T1 T2 P() { spin_lock(..); while (sem->count==0) { spin_unlock(..); --------------- context switch ----------- V(sem) spin_lock(..); sem->count++; thread_wakeup(sem); spin_unlock(..); --------------- context switch ----------- add_to_wait_queue schedule(); T1 sleeps forever! Page 11 of 11 pages