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

Similar documents
CSC209F Midterm (L5101) 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

SOFTWARE ARCHITECTURE 3. SHELL

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

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

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

System Programming. Introduction to Unix

Processes. Processes (cont d)

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

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

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

CPS 310 first midterm exam, 2/26/2014

CST Algonquin College 2

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

628 Lecture Notes Week 4

System Programming. Process Control II

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

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

Process Management! Goals of this Lecture!

Last Time. on the website

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

CSC209 Fall Karen Reid 1

System Programming. Process Control III

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

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Creating a Shell or Command Interperter Program CSCI411 Lab

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

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

Processes: Introduction. CS 241 February 13, 2012

Pipelines, Forks, and Shell

fork System-Level Function

Unix Processes 1 / 31

UNIX System Programming Lecture 3: BASH Programming

Process Management 1

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

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.

COMP 2355 Introduction to Systems Programming

Process Management 1

Review of Fundamentals

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

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

Operating System Structure

ปฏ บ ต การ #3. A Simple Shell Interpreter

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

Environment Variables

8. Characters, Strings and Files

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

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

CS Operating Systems Lab 3: UNIX Processes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

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

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

CSC 1600 Unix Processes. Goals of This Lecture

The Classical OS Model in Unix

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Implementation of a simple shell, xssh

CS 550 Operating Systems Spring Process III

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

CS240: Programming in C

CSE 333 Autumn 2014 Midterm Key

Programming Assignment #1: A Simple Shell

COM324 System Programming. Midterm Exam

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

CS240: Programming in C

Linux shell scripting Getting started *

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

518 Lecture Notes Week 3

TDDB68 Lesson 2 Pintos Assignments 3 & 4. Mattias Eriksson 2010 (or

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

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

bash, part 3 Chris GauthierDickey

System Programming. Signals II

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Process Creation in UNIX

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

Lecture 24: Multitasking and Signals

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

PESIT Bangalore South Campus

Compile the Hello World program

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

Computer Systems Assignment 2: Fork and Threads Package

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

OS Lab Tutorial 1. Spawning processes Shared memory

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

Processes. CSE 351 Autumn Instructor: Justin Hsia

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

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

Solutions to the first midterm. COSC 4330/6310 Summer 2013

CS 470 Operating Systems Spring 2013 Shell Project

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

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

CS 201. Processes. Gerson Robboy Portland State University

Transcription:

CSC209F Midterm (L0101) Fall 1998 University of Toronto Department of Computer Science Date: November 6 th, 1998 Time: 1: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: Marking Scheme Marks: Q1 Q2 Q3 Q4 Q5 Q6 Total CSC209F L5101 Midterm Test, November 6 th, 1998 Page 1 of 7

Q1: [15 marks] 1. Why are the commands logout/exit built into the shell? If they weren't, they would be child processes and couldn't (easily) terminate the parent. 2. Where should your alias statements be placed, in.login or.cshrc? Explain..cshrc since.login isn't sourced every time a new shell is created 3. (2 marks) What is the difference between a shell variable and an environment variable? (List at least 2 differences.) (1 mark) Shell vars can have multi-valued (array) properties (1 mark) Shell vars aren't visible to an exec()'d process while environment vars are 4. (2 marks) List two different ways in which a shell script can protect itself from unexpected behaviours due to aliased commands. (1 mark) #!/bin/csh -f (1 mark) preface commands with '\' to over-ride any aliases 5. How would you repeat your 2 nd most recent command without retyping it?!-2 6. Consider the. and.. special files. Is it correct to say that these files are system-created hard links to directories? Yes. 7. (2 marks) Joe wants to print out a numbered listing of the file "fa". He types "cat -n fa > lpr" but no printout appears. Why? What command would you suggest he use? (1 mark) he has redirected stdout into a file named "lpr" (1 mark) cat -n fa lpr 8. Where does UNIX store the default shell to be used when a user logs in? /etc/passwd 9. (2 marks) Show an alias command that will cause ls to list hidden files (like.,..,.login, etc.) without explicitly typing a command line parameter each time. Show also how to execute ls without the parameter after the alias has been define (on a once-only basis). (1 mark) alias ls 'ls -a' (1 mark) \ls 10. (2 marks) Briefly explain the I/O redirection occurring in each of the following commands: cmd1 cmd2 cmd1 & cmd2 The first connects stdout from cmd1 to stdin for cmd2. The second connects both stdout and stderr from cmd1 to stdin for cmd2. CSC209F L5101 Midterm Test, November 6 th, 1998 Page 2 of 7

Q2: [4 marks] Why is a hard link indistinguishable from the original file itself? The "original file" is really just a hard link itself to the file's inode. If you add a new hard link, it will behave identically. (2 marks) What happens if you rm a hard link? The hard link is removed (1 mark), and if the file's (inode's) hard link count goes to 0, the file is also removed (1 mark). Why is it not possible to have a hard link to a file in a different filesystem? A hard link need to specify the inode of the linked file, and it is assumed that the inode is in the same filesystem (there is no mechanism for specifying an inode and a filesystem in the link). Q3: [4 marks] Write a code fragment which takes a string defined as char *cmdline ; and processes it to put it in the correct format for execvp(char *filename, char *argv[]); You are to use strtok(char *s, const char *delim) to break the string into tokens. You can assume that there are no more than MAX_CMDLINE_ARGS tokens to be processed. Define whatever new variables you need. int i = 0 ; char *argv[max_cmdline_args+1]; memset(argv, 0, sizeof(argv)); argv[0] = strtok(cmdline, WHITESPACE); while (argv[i]!= (char *)NULL) i++ ; argv[i] = strtok(null, WHITESPACE); CSC209F L5101 Midterm Test, November 6 th, 1998 Page 3 of 7

Q4: [5 marks] Consider the shell variable assignments: set x = ( fred barney wilma betty ) set y = ( pebbles bam-bam ) set z = ( $x $y ) 1. (1 mark) What is the output of "echo $#z"? 6 2. (1 mark) What is the output of "echo $?y"? 1 3. (1 mark) What is the output of "set a = $x; echo $a"? fred This shows the importance of parentheses around set assignments. 4. (2 marks) Show how the word "pebbles" can be removed from y using a simple assignment statement. The answer "set y = bam-bam"" is not acceptable in this case. set y = ( $y[2] ) or set y[1] = set y[1] = "" These last two will be accepted, although they do not have the desired effect on $#y, namely reducing it to 1 CSC209F L5101 Midterm Test, November 6 th, 1998 Page 4 of 7

Q5: [10 marks] 1. Write a shell script newext so that newext oldext newext searches for all the files in the current directory with extension oldext and renames them to have the extension newext. Your script should make sure a file with the new name doesn't already exist and give a warning if it does (in this case do not rename the file). #!/bin/csh -f if ( $#argv!= 2 ) then echo Usage: $0 oldext newext exit endif set files = ( *.$1 ) if ( $#files == 0 ) then echo No files found... exit endif foreach file ( $files ) set newname = $file:r.$2 if ( -e $newname ) then echo $newname already exists, $file not renamed continue else mv $file $newname endif end This is a complete, tested shell script. You do not need something this complete to get full marks. CSC209F L5101 Midterm Test, November 6 th, 1998 Page 5 of 7

Q6: [12 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 executes a UNIX utility "foo" on the named file. As each child process is created, increment a global variable to keep track of the number of child processes. Implement a signal handler that detects when a child exits by catching SIGCHLD. Your handler should print a statement as to whether the child terminated normally or not, and what exit status code was returned. It should then decrement the child process counter. The parent, after fork()-ing and exec()-ing all the child processes, will enter a while() loop which continually puts the parent to sleep for 1 second until the child process counter is 0. Recall that signal handlers are functions that have one int parameter and return void. The following function prototypes may be 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); int signal(int sig, void (*handler)(int)); WIFEXITED(status) WEXITCODE(status) WIFSIGNALED(status) WTERMSIG(status) WIFSTOPPED(status) WSTOPSIG(status) int childcount = 0 ; /* global var for child counting */ void SigChildHandler(int signo) int status, pid ; pid = wait(&status); if (WIFEXITED(status)) printf("child %d exited: return code = %d\n", pid, WEXITCODE(status)); else printf("child %d terminated abnormally.\n", pid); childcount-- ; int main(int argc, char *argv[]) int i ; signal(sigchld, SigChildHandler); /* may want error checking here */ for (i=1; i<argc; i++) switch (fork()) case 0: execlp("foo", "foo", argv[i], (char *)0); exit(i); /* need this in case execlp() fails */ break ; /* this is here out of force of habit */ CSC209F L5101 Midterm Test, November 6 th, 1998 Page 6 of 7

(Use this page for answer overflows) case -1: printf("fork() failure for %s.\n", argv[i]); break ; default: childcount++ ; break ; while(childcount) sleep(1); return ( 0 ); CSC209F L5101 Midterm Test, November 6 th, 1998 Page 7 of 7