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

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

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

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

5/8/2012. Creating and Changing Directories Chapter 7

System Programming. Introduction to Unix

PESIT Bangalore South Campus

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

CS Operating Systems Lab 3: UNIX Processes

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC 1600 Unix Processes. Goals of This Lecture

Largest Online Community of VU Students

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

CSC209 Fall Karen Reid 1

System Programming. Process Control III

SOFTWARE ARCHITECTURE 3. SHELL

Shells and Shell Programming

Shells and Shell Programming

SE350: Operating Systems

The UNIX File System

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

The UNIX File System

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Operating Systems Lab

Creating a Shell or Command Interperter Program CSCI411 Lab

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

(In columns, of course.)

CS 550 Operating Systems Spring Process III

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

A: We see the ps auxw execute and print on screen. The program holds the command in buffer then it is printed on screen.

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

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

Introduction to Supercomputing

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

File Descriptors and Piping

Operating System Structure

System Programming. Process Control II

CSE 374 Midterm Exam Sample Solution 2/6/12

Files and Directories

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

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

Unix Processes 1 / 31

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

Linux & Shell Programming 2014

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

OPERATING SYSTEMS, ASSIGNMENT 4 FILE SYSTEM

CS 550 Operating Systems Spring Process II

Part 1: Basic Commands/U3li3es

The Classical OS Model in Unix

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

Trees. (Trees) Data Structures and Programming Spring / 28

CPS 310 first midterm exam, 2/26/2014

Chapter 1 - Introduction. September 8, 2016

Programming Assignment #1: A Simple Shell

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

Links, basic file manipulation, environmental variables, executing programs out of $PATH

CSC209H1S Day Midterm Solutions Winter 2010

UNIX File Hierarchy: Structure and Commands

find Command as Admin Security Tool

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

Why files? 1. Storing a large amount of data 2. Long-term data retention 3. Access to the various processes in parallel

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

CSC209 Review. Yeah! We made it!

Last Time. on the website

File System Definition: file. File management: File attributes: Name: Type: Location: Size: Protection: Time, date and user identification:

Operating Systems Lab

Part II Processes and Threads Process Basics

Essential Linux Shell Commands

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Assignment clarifications

Project 1: Implementing a Shell

Consider the following program.

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

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

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

Midterm spring. CSC228H University of Toronto

CSE 303 Midterm Exam

Bash Programming. Student Workbook

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Files

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

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

518 Lecture Notes Week 3

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

The Online Unix Manual

A Brief Introduction to the Linux Shell for Data Science

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.

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

Lecture 2b. Pathnames, files, special characters in filenames, and file permissions. COP 3353 Introduction to UNIX, FALL 2013

Operating Systems, Unix Files and Commands SEEM

UNIX Kernel. UNIX History

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS240: Programming in C

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

Transcription:

CSC209S Midterm (L0101) Spring 1999 University of Toronto Department of Computer Science Date: February 26, 1999 Time: 12: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 40 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: Marking Student Number: Scheme CDF Username: Marks: Q1 10 Q2 5 Q3 10 Q4 10 Q5 5 Total 40 CSC209S-99 Midterm, February 26 th, 1999 Page 1 of 7

Q1: [10 marks] (1 mark each unless otherwise indicated) 1. What does "set noglob" do when typed to the shell? Turns off filename expansion 2. (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 3. Explain the difference between absolute and relative pathnames. 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) 4. What is the difference between 'mv file1 file2' and 'cp file1 file2; rm file1'? The first merely changes the name in the directory listing (no new file is created, and the inode for the file remains unchanged). The latter creates a new file (with a new inode) and then deletes the first file. 5. What is the difference between the stat() and lstat() functions? When used on a symbolic link, stat() returns the inode information for the file the link points at, whereas lstat() returns the inode information for the file containing the link information. 6. The directories for searching executables are specified both in the environment variable $PATH and in the local shell variable $path. Which is the appropriate way to add $HOME/bin to the search path? setenv $PATH $HOME/bin:$PATH or set $path = ($HOME/bin $path) Either is fine. Setting one automatically changes the other ($path is a "special shell variable"). 7. (3 marks) Explain the difference between a "hard-link" and a "soft link". Give an example of using each. A hard-link is just another directory entry which points at a file's inode. A symbolic link creates a new file with string containing the path (absolute or relative) of the "linked" file. To create a hard link: "ln file1 newlinkhard". To create a symbolic link, "ln -s file1 newlinksoft". CSC209S-99 Midterm, February 26 th, 1999 Page 2 of 7

Q2: [5 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); CSC209S-99 Midterm, February 26 th, 1999 Page 3 of 7

Q3: [10 marks] a) (2 marks) "ls -l" lists files in the format -r-xr-xr-x 3 bin 89564 May 3 1996 /bin/sh where the size of the file is the 4 th word in the line. How can you assign the size of a file to a shell variable using "ls -l"? set temp = `ls -l $filename` set size = $temp[4] or set size = `ls -l $filename awk print $4;` b) (7 marks) Write a shell script "dirsize dir" that computes the total size in bytes of all files in the directory dir. Do not count the size of the directory entries (this includes, but is not limited to, ".." and "."). @ bytes = 0 foreach file ( * ) if (! -d $file) set temp = `ls -l $file` @ bytes = $bytes + $temp[4] end if end echo "Total size = $bytes bytes." c) (1 mark) Does the result calculated accurately reflect the total bytes of disk space occupied by these files? Explain. It may not be accurate if there are hard or soft links in the directory. For example, if name1 and name2 are both hard links pointing to the same file, then that file's space will get counted twice. To avoid this have your script check inode numbers and throw out duplicates. CSC209S-99 Midterm, February 26 th, 1999 Page 4 of 7

Q4: [10 marks] Write a program that takes a single integer argument n from the command line and creates a binary tree of processes of depth n (i.e. a group of processes descended from a common ancestor in a tree-like manner). When the tree is created, each process should display the phrase "I am process x, my process ID is y and my parent's is z" where x is the number of the process (see diagram below), create any necessary child processes, wait for them to terminate, and then terminate. The nodes of the process tree should be numbered according to a breadth-first traversal (e.g. see diagram below). For example, if the user enters the command % tree 4 then the process tree would look like and the output would be (something like) I am process 1, my process ID is 465 and my parent's is 321. I am process 2, my process ID is 466 and my parent's is 465. M I am process 15, my process ID is 499 and my parent's is 492. Make sure the original parent process (#1) does not terminate until all of its children have died. int main(int argc, char *argv[]) int procnum = 1 ; int child[2] ; int numchildren = 0 ; int status ; int maxdepth ; assert(argc == 2); CSC209S-99 Midterm, February 26 th, 1999 Page 5 of 7

maxdepth = atoi(argv[1]); /* convert depth parameter */ while (numchildren < 2) if (numchildren == 0) printf("i am process %d, my process ID is %d" " and my parent's is %d.\n", procnum, getpid(), getppid()); child[numchildren] = fork(); if (child[numchildren] == 0) numchildren = 0 ; procid *= 2 ; if (! --maxdepth) exit(0); else numchildren++ ; procnum++ ; while (numchildren) wait(&status); CSC209S-99 Midterm, February 26 th, 1999 Page 6 of 7

Q5: [5 marks] Write a C function int IsPalindrome(char *s). This function returns 1 if the strings identified by s is a palindrome and 0 otherwise (a string is a palindrome if reversing it results in the same string). Assume s is null-terminated and do not make a copy of s. int IsPalindrome(char *s) char *start = s, *end = s; if (! *s) return 1; /* an empty string is a palindrome */ while (*end++); end-- ; /* position end of string */ while (*start == *end) start++ ; end-- ; if (start > end) break ; if (start > end) return 1 ; else return 0 ; CSC209S-99 Midterm, February 26 th, 1999 Page 7 of 7