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

Similar documents
Assignment 1. Teaching Assistant: Michalis Pachilakis (

Processes COMPSCI 386

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

Operating System Structure

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes. Processes (cont d)

Recitation 8: Tshlab + VM

Operating Systemss and Multicore Programming (1DT089)

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

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Process Management! Goals of this Lecture!

CPS 310 first midterm exam, 10/6/2014

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

CSE 410: Systems Programming

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

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

Unix Processes. What is a Process?

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

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

SOFTWARE ARCHITECTURE 3. SHELL

Processes, Signals, I/O, Shell Lab : Introduc>on to Computer Systems Recita>on 9: Monday, Oct. 21, 2013 Marjorie Carlson Sec>on A

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 1 Questions (document version 1.1)

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Inter-Process Communication

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

Process Turnaround Time Total Wait Time P 1 12 ms 0 ms P 2 21 ms 12 ms P 3 23 ms 18 ms P 4 20 ms 17 ms

Process Management 1

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

Lecture 24: Multitasking and Signals

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Final Exam Questions (document version 1.1) WITH SELECTED SOLUTIONS

CSC 1600 Unix Processes. Goals of This Lecture

File Descriptors and Piping

CPS 310 first midterm exam, 2/26/2014

Process Control. Philipp Koehn. 23 April 2018

Process Creation in UNIX

Operating Systemss and Multicore Programming (1DT089)

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

Process Management! Goals of this Lecture!

The Life and Death of A Process

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

CSC209 Fall Karen Reid 1

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

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Exceptions, Processes and Signals

CSCI0330 Intro Computer Systems Doeppner. Project Shell 2. Due: November 8, 2017 at 11:59pm. 1 Introduction 2

CSC 252: Computer Organization Spring 2018: Lecture 19

CS240: Programming in C

Operating Systems. Lecture 05

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

ESE 333 Real-Time Operating Systems 2 What is an operating system? Two views: 1. Top-down view: Extended machine ffl Covers the details of the hardwar

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

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

CSC209 Review. Yeah! We made it!

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

System Programming. Signals II

Exceptional Control Flow Part II Nov. 2, 2009"

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

CS 550 Operating Systems Spring Inter Process Communication

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Processes: Introduction. CS 241 February 13, 2012

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

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

CSI Module 2: Processes

COMP 3100 Operating Systems

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

Design Overview of the FreeBSD Kernel CIS 657

(In columns, of course.)

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Advanced Unix Concepts. Satyajit Rai

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Killing Zombies, Working, Sleeping, and Spawning Children

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

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

Announcement (1) sys.skku.edu is now available

Systems programming and Operating systems, 2005 Tentamen

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

W4118 Operating Systems. Junfeng Yang

Interrupts, Fork, I/O Basics

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Transcription:

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS] April 2, 2014 Answer the questions in the spaces provided on the question sheets. If you run out of room for an answer, continue on the back of the page. Show all work, but please be legible. Microscopic writing will not be graded. You are allowed a single crib sheet for this exam on an 8.5 x11 sheet of paper, hand written. You must turn in your crib sheet with your exam. Name: Section: Alpha: Question Points Score 1 15 2 15 3 15 4 15 5 15 Total: 75

1. Consider the following program: int i, status ; for (i=0;i <3; i++){ if( fork () == 0){ /* child */ printf (" Child : %d: Hello World!", i); _Exit (0); else { /* parent */ printf (" Parent : %d: Hello World!\ n",i); wait (& status ); if( WIFSIGNALED ( status )){ printf (" Process signaled \n"); return 0; (a) [4 points] To the right of the program, complete the drawing of the process tree above, where each dot represents a process and a fork results in a split, up for the parent and down for the child. Below answer: How many total processes are created, including the initial parent process? How many total processes are running after the loop? Explain. 4 total processes created 1 process running after loop [+1 for total process +1 for running after and +2 for process tree] (b) [4 points] How many times does Child:... print and how many times does Parent:... print to standard output? Explain. Child:... does not print at all because buffer not flushed with Exit() Parent:... prints 3 times, once for each fork call [+2 for each, partial credit for discussion of buffers but wrong totals] (c) [3 points] Does the message Process signaled print? Why or why not based on the value of status? No because the child exited and was not terminated by a signal. This information is contained within the status and checked with WIFSIGNALED() [+2 for No, +1 for No but wrong reason, +1 for mention of status] (d) [4 points] At the end of the program, are there any zombie processes? If so, why are there zombies and what happens to them when the parent terminates? If not, why aren t there any zombie processes? Yes. Three child processes are created but only one is waited on. The other two become zombies. After the parent terminates they will be orphaned and inherited by init which will reap them. [+2 for yes, +1 orphaned, +1 for inherited by init; +1 for no with mention of waiting] Page 2 of 6

2. (a) [3 points] Consider the execution of the following commands, what is the output of jobs? #> sleep 200 & #> head -c 10 / dev / urandom > rand #> cat & #> sleep 10 ^Z #> bg [1] Running sleep 200 & [2] Stopped cat [3] Running sleep 10 & [+3 for all in right state, +2 for all in running state, +1 for some combination] (b) [3 points] Based on the above commands, what shell command will bring sleep 200 to the foreground? fg 1 or fg %1 [+2 for fg; +1 for indicating job number] (c) [3 points] For how many seconds does the following pipeline run for? Explain. sleep 1000 cat /proc/cpuinfo grep processor sleep 10 1000 seconds because all run in parallel [+2 for 1000 seconds, +1 for parralel, +1 for 1010 seconds with some explanation] (d) [3 points] Using the following pipeline of commands and their pid (indicated in italics above each command), what is the pgid of each process in the pipeline? Will all processes terminate after a Ctrl-C? Why or why not? 1994 1995 1996 1997 sleep 20 sleep 30 sleep 40 sleep 50 The pgid is 1994, the pid of the first process in the pipeline. Yes, all terminate after Ctrl-C because sent to the foreground process group [+2 for 1994, +1 for yes sent to foreground process group] (e) [3 points] At the mark, what process state would the parent process be in, Ready and Waiting, Ready and Running, Blocked and Waiting? Explain. if( fork () == 0){ while (1); else { wait ( NULL ); // MARK Blocked and Waiting: Blocking on the system call wait until the child completes, which it wont. [+2 for Blocked and Waiting, +1 for explanation] Page 3 of 6

3. int main ( int argc, char * argv []){ char str1 [] = " Go Navy! "; char str2 [] = " Beat Army! "; char buffer [1024]; int pfd1 [2], pfd2 [2], n; (a) [3 points] In the program to the right, two pipes are opened before fork() how is it that the child also has access to them to communicate with the parent? Open files, including pipes, are duplicated on a fork, and they are communicate via the kernel buffers. [+2 for duplication on fork, +1 for kernel buffers] (b) [4 points] In the program to the right, which system call will complete first, MARK 2 in the child or MARK 5 in the parent? Explain. MARK 2 will complete before MARK 5 because the parent will block on the read() until data is written to the pipe. Expected output is: Go Navy! Beat Army [+2 for Mark 2 over Mark 5, +1 mention blocking, +1 for right output] pipe ( pfd1 ); pipe ( pfd2 ); if( fork () == 0){ /* Child */ // MARK 1 close ( pfd1 [0]); close ( pfd2 [1]); // MARK 2 write ( pfd1 [1], str1, strlen ( str1 )); // MARK 3 n = read ( pfd2 [0], buffer, 1024); write (1, buffer, n); else { /* Parent */ // MARK 4 close ( pfd1 [1]); close ( pfd2 [0]); // MARK 5 n = read ( pfd1 [0], buffer, 1024); write (1, buffer, n); // MARK 6 write ( pfd2 [1], str2, strlen ( str2 )); wait ( NULL ); return 0; (c) [4 points] In the above program, what is MARK 1 and MARK 4 doing to the pipe? Why are the operations mirrored in parent and child? The pipes are being widowed, and the procedure is mirrored because the parent and child read/write from different ends of each pipe. [+3 for widowing, +1 for using different ends in parent/child] (d) [4 points] Consider the following change in the program at MARK 2 where str1 is written continually to the pipe in a loop. Will the child ever reach MARK 3? If yes, why, and if no, why not? // MARK 2 while (1){ write ( pfd [1], str1, strlen ( str1 )); The child will never reach MARK 3 because the pipe will fill up and subsequent writes will block until the pipe is read again, which it is not. [+3 Never reach MARK 3, +1 for explanation] Page 4 of 6

4. Consider the following program int fd1 = open (" file1. txt ", O_RDONLY ); int fd2 = open (" file2. txt ", O_WRONLY O_CREAT, 0640); pid_t cpid ; char c; while (1){ if( ( cpid = fork ()) == 0){ /* Child */ close (1); dup2 (fd2,1); if( read (fd1, &c, 1) > 0){ // read 1 byte from file write (1, &c, 1); // write 1 byte to stdout _exit (0); // exit hard else { /* Parent */ wait ( NULL ); // wait for child to exit if( read (fd1, &c, 1) <= 0){ // read 1 byte from file break ; // exit on EOF or read fail else { write (1, &c, 1); // otherwise write 1 byte to stdout [+1 for new file descriptor table entries; +3 for dup in child and shared files; +2 for dup without shared or shared without dup] return 0; (a) [4 points] Complete the figure of the Process Table and Open File Table entries for the above program, drawing arrows for references and including new file descriptor table entries. You only need to complete one of the children since they are all the same. (b) [4 points] If file1.txt contained the phrase Go Navy! Beat Army!, where indicates a space, what is written to standard output and what is written to file2.txt? Explain. file2.txt: G ay etam! (every other char starting with G ) stdout: onv!ba ry (every other char starting with o ) Child writes everything to file2.txt because that is duped onto its standard output. [+3 for write outputs or +1 for all to stdout; +1 for explanation] (c) [4 points] If file1.txt was linked in the v-node/i-node structure like below, would the functionality of the program change if file1.txt were replaced by file3.txt? Explain, and what kind of link is this? Functionality is the same because O.S. follows the links. This is a symbolic link. [+2 for same with explanation; +2 for symbolic] (d) [3 points] In which node, v-node or i-node, is device specific information stored for reading and writing the file from/to the device? i-node [All or nothing] Page 5 of 6

5. Consider the following program int ticks =0; void handler ( int signum ){ ticks ++; printf (" tick tock : %d\n", ticks ); if( ticks < 10){ alarm (1); else { raise (9); //<-- MARK 1 signal ( SIGALRM, handler ); //<-- MARK 2 alarm (1); // MARK 3 while (1){ pause (); //<-- MARK 3 (a) [3 points] At MARK 2, what is the purpose of the call to signal() with respect to future deliveries of SIGALRM from the O.S.? Registers a signal handler with the O.S., handler is called whenever a SIGALRM is delivered. [+2 Handler is called when SIGALRM is delivered, +1 register with OS] (b) [4 points] What is the result of the system call at MARK 1? What is the name of the signal being raised? Sends the current process the designated signal, SIGKILL whose number value is 9 [+2 raise description, +2 SIGKILL] (c) [4 points] How many SIGALRM signals are delivered to the program? Explain. 10 SIGALRMs are delivered. First alarm scheduled in main, then every time the handler is invoked, another alarm is schedule, stopping at 10. [+3 10 SIGALRMs, +1 for explanation] (d) [4 points] What is the purpose of pause() at MARK 3? Is it better or worse then replacing it with the following code without pause()? //MARK 3 while(1); pause() will suspend the process a signal is handled, and it better than the busy loop because it does not waist process resources. [+2 for explanation, +2 for betteer than busy loop, +1 for better without explanation] Page 6 of 6