CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

Similar documents
CSC209F Midterm (L0101) Fall 1998 University of Toronto Department of Computer Science

CSC209S Midterm (L0101) Spring 1999 University of Toronto Department of Computer Science

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

CSC209 Fall Karen Reid 1

System Programming. Process Control II

Lesson 2. process id = 1000 text data i = 5 pid = 1200

SOFTWARE ARCHITECTURE 3. SHELL

System Programming. Introduction to Unix

Creating a Shell or Command Interperter Program CSCI411 Lab

System Programming. Signals II

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.

CSC209 Review. Yeah! We made it!

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Implementation of a simple shell, xssh

CST Algonquin College 2

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Operating System Structure

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

Processes: Introduction. CS 241 February 13, 2012

Operating Systemss and Multicore Programming (1DT089)

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

Unix Processes 1 / 31

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

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

Implementation of a simple shell, xssh

Linux shell scripting Getting started *

Assignment clarifications

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

CSC 1600 Unix Processes. Goals of This Lecture

Last Time. on the website

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Process Management! Goals of this Lecture!

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

SE350: Operating Systems

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Fall 2015 COMP Operating Systems. Lab #3

Introduction to UNIX Shell Exercises

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

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

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

Process Management 1

Process Management 1

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

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

Computer Systems Assignment 2: Fork and Threads Package

CS240: Programming in C

File Access. FILE * fopen(const char *name, const char * mode);

Introduction to Processes

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

CS Operating Systems Lab 3: UNIX Processes

5/20/2007. Touring Essential Programs

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

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

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

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

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

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

CS167 Programming Assignment 1: Shell

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

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

Exceptional Control Flow Part I

Altering the Control Flow

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

System Programming. Process Control III

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

CS510 Operating System Foundations. Jonathan Walpole

Lecture 24: Multitasking and Signals

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Linux Refresher (1) 310/ Fourth Workshop on Distributed Laboratory Instrumentation Systems (30 October - 24 November 2006)

fork System-Level Function

Shells and Shell Programming

Chapter 1. Introduction

Altering the Control Flow

Process Management! Goals of this Lecture!

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

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

CSE 153 Design of Operating Systems Fall 2018

Command-line interpreters

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

Kernel and processes

Exceptional Control Flow: Exceptions and Processes

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Pipelines, Forks, and Shell

The Classical OS Model in Unix

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.

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

The Kernel. wants to be your friend

OS Interaction and Processes

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

Approaches to Concurrency

CSCI 4500 Operating Systems. In This Module

CSC 252: Computer Organization Spring 2018: Lecture 19

Transcription:

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science Date: November 2 nd, 1998 Time: 6:10 pm Duration: 50 minutes Notes: 1. This is a closed book test, no aids are allowed. 2. Print your name, student number, and cdf username in the space provided below. 3. There are a total of 50 marks. 4. All shell questions assume csh. 5. This test is worth 20% of your final course mark. 6. This test comprises 7 pages. Do not detach pages, and answer questions in the blank spaces provided. If you need more space, answer on the back of the pages (clearly identify which question in this case). 7. Read all questions before starting. Not all questions are worth the same number of marks, so budget your time accordingly. Answer questions in any order you desire. Name: Student Number: CDF Username: Marks: Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 1 of 7

Q1: [10 marks] 1. How do you run a program in the background? Type '&' after the command, e.g. "nedit &", or type ^z to suspend the process and then type "bg" to put the process into the background (some people had ^z and bg reversed ) 2. What does "set noglob" do when typed to the shell? Turns off filename expansion (many thought this was related to global variables) 3. (2 marks) Name 2 common UNIX shells. sh (Bourne), csh, tcsh, ksh 4. (2 marks) What is the difference between an absolute and relative pathname? Give an example of each. Absolute pathnames start with '/' and specifies a path starting from the root directory (e.g. /bin/csh). Relative pathnames specify a path starting in the current working directory (e.g.../temp/mystuff.txt) 5. What is the command line to compile myprog.c so that the executable can be debugged using gdb? gcc -g myprog.c -o myprog where the -o option is optional (many didn't get this are you using the debugger for assignments?) 6. What is the command to create a soft link "slink" to the file "/u/maclean/junk.txt"? ln -s /u/maclean/junk.txt slink 7. (2 marks) What is the value of argc and argv[2] in the C program invoked as "myprog -13 fred < csc209.lst > csc209.marks"? argc = 3 argv[2] = "fred" (Many got the argc value wrong it's important to know that the program never "sees" I/O redirection ) Q2: [5 marks] Briefly explain the I/O redirection occurring in each of the following commands: mycmd > somefile output (stdout) from "mycmd" goes into file "somefile" (created if necessary, or overwritten if it exists & "noclobber" is not set) mycmd < somefile input (stdin) for "mycmd" comes from file "somefile" mycmd >> somefile output (stdout) from "mycmd" is appended to file "somefile" mycmd >! somefile output (stdout) from "mycmd" overwrites "somefile", even if "noclobber" is set mycmd >& somefile output (stdout + stderr) from "mycmd" is written to "somefile" In the first three parts I accepted "input" to mean stdin and output to mean "stdout", but the answers should have been more specific. In order to get the mark for the 4 th one, the word "noclobber" had to be in your answer. In the last one many thought we were putting the process into the background. CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 2 of 7

Q3: [5 marks] (3 marks) What are three possible outputs of: int main(int argc, char *argv[]) int pid ; if ((pid = fork()) == 0) printf("i am = %d, Parent = %d\n", getpid(), getppid()); else printf("child = %d, I am = %d\n", pid, getpid()); 1) Assume child goes first (race condition): I am = 10, Parent = 9 Child = 10, I am = 9 2) Parent goes first (race condition): Child = 42, I am = 26 I am = 42, Parent = 26 3) Parent terminates before child prints (orphaned): Child = 42, I am = 26 I am = 42, Parent = 1 4) Assume fork() is unsuccessful: Child = -1, I am = 42 Many people only thought either the parent or the child got to printout, this is wrong. Some people had the parent's PID changing values, and some had the child with PID = 0 (this will never happen). Some thought that if fork() failed, the program would crash (not true). (2 marks) How would you change the program to ensure a unique output? Insert the lines int status ; wait(&status); at the beginning of the else clause. The wait() call synchronizes the parent & child. CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 3 of 7

Q4: [5 marks] Write a signal handler which catches interrupts typed at the keyboard (SIGINT is sent), and on the third interrupt prints "3 strikes and you're out\n", and causes the program to terminate. Show how your signal handler can be installed (a code fragment will suffice here). Recall that signal handlers are functions which have one int parameter and return void. void handler(int signo) static int count = 0 ; if (++count == 3) printf("3 strikes and you're out\n"); exit(0); To install: if (signal(sigint, handler) == -1) printf("error, unable to install handler!\n"); exit(1); Q5: [5 marks] Consider the shell variable assignment: set x = ( abc def _123 xyz ) 1. (1 mark) What is the output of "echo $#x"? 4 2. (1 mark) What is the output of "echo $?x"? 1 3. (1 mark) What is the output of "set y = $x; echo $y"? abc 4. (2 marks) Show how the word "_123" can be removed from x using a simple assignment statement. The answer "set x = ( abc def xyz )" is not acceptable in this case. set x = ( $x[1-2] $x[4] ) set x[3] = '' set x[3] = The last two were accepted, although they leave the element blank as opposed to removing it ($#x remains 4) CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 4 of 7

Q6: [10 marks] 1. (7 marks) Write a shell script findfile so that findfile name dir1 dir2... searches for the file name in the directories specified. If the file is found in one of the directories, the current directory is changed to it. Assume name is a file name, and not an absolute or relative path. #!/bin/csh -f if ( $#argv < 2 ) then echo Usage: findfile name dir1... exit endif set cdir = $cwd set name = $1 shift # remove filename from command line args foreach dir ( $argv ) end echo Trying $dir if ( -d $dir ) then cd $dir else echo $dir not a directory... continue endif if ( -e $name ) then # check for existence of $name in $cwd echo $name found in $cwd break endif cd $cdir # go back to start directory This question was poorly done. Many people didn't handle both absolute and relative paths for dirx. Many people were using 'C' constructs. Also, many people were using "find", but forgetting that it recursively searches subdirectories, which this script was not specified to do. 2. (3 marks) Why does this script have to be sourced through aliasing rather than invoked as a command? Show an acceptable alias command to achieve correct operation for the script. If this script is invoked as a command, it will be a child process of the calling process (shell). Therefore any change of directory ($cwd) will not be reflected in the parent process. alias findfile 'source findfile \!*' CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 5 of 7

Q7: [10 marks] Write a C program that takes any number of file names as command line parameters. For each filename the program should create a child process which i) prints the name of the file, and ii) uses execlp() to execute the command "wc -l" on the file. The parent process should not exit until all the children have exited, and should create the child processes in such a way that they run concurrently, not sequentially. For any children that either a) terminate abnormally, or b) return a non-zero status code, the parent process should print a line indicating either that an abnormal termination occurred, or print the non-zero status code returned. You may find some of the following function prototypes and macros useful: execlp(char *filename, char *arg0,..., (char *)0); int wait(int *status); int waitpid(int pid, int *status, int options); void *malloc(long size); void *realloc(long size); char *strcpy(char *dest, char *src); int strcmp(char *s1, char *s2); WIFEXITED(status) WEXITCODE(status) WIFSIGNALED(status) WTERMSIG(status) WIFSTOPPED(status) WSTOPSIG(status) int main(int argc, char *argv[]) int i ; for ( i=1; i<argc; i++) if (fork() == 0) printf("%s\n", argv[i]); fflush(stdout); execlp("wc", "wc", "-l", argv[i], (char *)0); exit(1); /* needed in case execlp() fails */ i = argc - 1 ; while (i) int status, pid ; pid = wait(&status); if (WIFEXITED(status) && (WEXITCODE(status)!= 0)) printf("child %d exited with return code = %d\n", pid, WEXITCODE(status)); if (!WIFEXITED(status)) printf("child %d terminated abnormally.\n", pid); i-- ; Many people only passed one "wc" to execlp(), or failed to pass "-l" as a separate parameter. Most people didn't make the children run concurrently. CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 6 of 7

(Use this page for answer overflows) CSC209F L5101 Midterm Test, November 2 nd, 1998 Page 7 of 7