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

Similar documents
UNIX System Calls. Sys Calls versus Library Func

CS , Spring Sample Exam 3

Operating System Labs. Yuanbin Wu

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

OPERATING SYSTEMS: Lesson 2: Operating System Services

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

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

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Operating System Labs. Yuanbin Wu

structs as arguments

CSE 333 SECTION 3. POSIX I/O Functions

File and Directories. Advanced Programming in the UNIX Environment

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

CSE 333 SECTION 3. POSIX I/O Functions

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

Operating Systems. Processes

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Thesis, antithesis, synthesis

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

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

CSC 271 Software I: Utilities and Internals

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

OPERATING SYSTEMS: Lesson 12: Directories

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

File Systems. q Files and directories q Sharing and protection q File & directory implementation

Files and Directories

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture 23: System-Level I/O

CS 201. Files and I/O. Gerson Robboy Portland State University

Master Calcul Scientifique - Mise à niveau en Informatique Written exam : 3 hours

Parents and Children

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

OS Lab Tutorial 1. Spawning processes Shared memory

FILE SYSTEMS. Jo, Heeseung

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

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

File Types in Unix. Regular files which include text files (formatted) and binary (unformatted)

Unix Processes 1 / 31

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

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

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

Lecture 21 Systems Programming in C

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus DECEMBER 2017 EXAMINATIONS. CSC 209H1F Liu, Reid 3 hours. No Examination Aids

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

Process Creation in UNIX

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Process management 1

The course that gives CMU its Zip! I/O Nov 15, 2001

All the scoring jobs will be done by script

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

CS240: Programming in C

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

CSCE 313 Introduction to Computer Systems

Files and Directories Objectives Additional Features of the File System Properties of a File. Three major functions that return file information:

Automated Test Generation in System-Level

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

System Programming. Process Control III

CSC209H1S Day Midterm Solutions Winter 2010

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

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

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

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

System Programming. Introduction to Unix

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size);

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

CS240: Programming in C

Systems Programming/ C and UNIX

All the scoring jobs will be done by script

Files and Directories Filesystems from a user s perspective

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

File System (FS) Highlights

CS631 - Advanced Programming in the UNIX Environment

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

File Input and Output (I/O)

INTRODUCTION TO UNIX FILE SYSTEM:

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 7

Fall 2015 COMP Operating Systems. Lab #3

17: Filesystem Examples: CD-ROM, MS-DOS, Unix

File Descriptors and Piping

Chapter 1. Introduction

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

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.

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

Chapter 13 Systems Programming. Systems Programming. Error Handling. Error Handling. Example: File I/O. Example: File I/O

39. File and Directories

Transcription:

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science Date: October 26, 1999 Time: 1:10 pm Duration: 50 minutes Notes: 1. This is a closed book test, no aids are allowed. 2. Pr 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 6 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 /10 Q2 /8 Q3 /10 Q4 /12 Total CSC209F-99 Midterm, October 26 th, 1999 Page 1 of 1

Q1: [10 marks] (1 mark each unless otherwise indicated) 1. (3 marks) What are the legal return values for fork(). Explain (briefly) what each one means. -1 : fork() unable to successfully create new process 0 : normal return as seen by child PID > 0 : normal return as seen by parent 2. Show how to store the output from the command ls -l in a shell variable. set x = `ls -l` 3. What is the difference between a program and a process? A program is an executable file (script or binary) stored in the filesystem, whereas a process is an executing instance of a program. 4. What is the difference between an absolute and a relative pathname? An absolute path name starts with a '/', whereas a relative path name does not. 5. Show how to use I/O re-direction to append the output from ls to the file 'mydata'. ls >> mydata 6. How can you overide aliasing on a one-time only basis. (E.g. "rm -i" is aliased to "rm", and you wish to execute "rm" without the "-i" option.) Place '\' in front of the command, e.g. "\rm". 7. What is the difference between a local shell variable and an environment variable. A local shell variable is not passed to exec()'d processes, whereas environment variables are. CSC209F-99 Midterm, October 26 th, 1999 Page 2 of 2

Q2: [8 marks] Write a csh shell script to compute the total size of all non-directory files in a directory. The script takes one (optional) parameter, the name of the directory for which the total is to be computed. If no parameter is supplied, the total is computed for the current working directory. #!/usr/bin/csh -f # 1 mark for correct header (-f optional) if ( $#argv > 1 ) then echo Usage: $0 <directory> exit 1 end if if ( $#argv == 1 ) then # 1 mark for dealing with parameter set dir = $argv[1] else set dir = "." end if # 2 marks for call to ls set dirinfo = "`ls -alf $dir grep -v /`" shift dirinfo # gets rid of header line from ls data set total = 0 while ( $#dirinfo ) # 2 marks for loop of some sort set temp = ( $dirinfo[1] ) @ total = $total + $temp[4] # 1 mark for doing sum shift dirinfo end # 1 mark for output of result echo Total \(non-directory\) bytes in $dir is $total There are different ways to do this. For example, you could pass thr output from grep to get the size field only in dirinfo, and just do the sum that way. I don't care which field is extracted from the "ls -l" data, so long as the idea of extracting some field is present. Also, most will not have used the "` `" technique to assign whole lines to each array index. CSC209F-99 Midterm, October 26 th, 1999 Page 3 of 3

Q3: [10 marks] Write a C program named numdirs.c to count the number of subdirectories within a given directory. The program takes one optional parameter, the name of the directory for which subdirectories are to be counted. If the parameter is not specified, the subdirectories in the current working directory are counted. The program should ignore "." and ".." while doing its count. Your solution must check the return status of all system calls for errors. #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <stdio.h> #include <errno.h> You did not need to specify include files, as stated in class. main( argc, char *argv[]) { char *dirname ; DIR *dir ; /* 1 mark for declaration */ struct dirent *dirdata ; /* 1 mark for declaration */ if (argc > 2){ fprf(stderr, "Usage: %s <directory>", argv[0]); if ( argc == 2) dirname = argv[1] ; /* 1 mark for arg processing */ else dirname = "." ; if (dir = opendir(dirname)){ /* 2 marks for opendir() */ long count = 0 ; struct stat buf ; while (dirdata = readdir(dir)) /* 2 marks for readdir() */ { char filepath[256]; /* let's assume 256 is enough */ strcpy(filepath, dirname); strcat(filepath, "/"); strcat(filepath, dirdata->d_name); if (stat(filepath, &buf)!= -1) /* 1 mark for stat()/lstat() */ /* 1 mark for S_ISDIR(), plus calculation */ { if (S_ISDIR(buf.st_mode)){ count ++ ; else fprf(stderr,"unable to stat() %s!\n", filepath); closedir(dir); /* 1 mark for closedir() */ prf("total size = %ld files.\n", count); else { fprf(stderr,"unable to open %s!", dirname); return 0 ; Subtract 2 marks for failing to check return values. I don't think there are many different ways to do this. This solution ignores symbolic links for simplicity's sake. CSC209F-99 Midterm, October 26 th, 1999 Page 4 of 4

Q4: [12 marks] Write the following C program: runsafe <progsafe> <prog> [<arg1> [<arg2> [<argn>] ]] runsafe creates a child process and uses it to execute <prog> with any arguments following <prog> on the command line. If <prog> terminates normally with status code = 0, then runsafe is finished. If <prog> terminates normally with status code 0, then runsafe executes <progsafe> and reports on its termination status. If <prog> terminates abnormally or fails to run at all, runsafe reports this but does not run <progsafe>. In your solution, you must check the return status of all system calls for errors. It is assumed <prog> may reside anywhere in the search path. #include <wait.h> #include <stdio.h> #include <errno.h> main( argc, char *argv[]) { result, status ; if (argc < 3){ fprf(stderr,"usage: %s <progsafe> <prog> {<args>", argv[0]); switch (fork()) /* 2 marks - create child */ { case -1: /* error */ /* 1 marks - deal with error */ fprf(stderr,"error: %s: unable to create new process!\n", argv[0]); case 0: /* child */ execvp(argv[2], argv + 2); /* 1 marks - correct call to execvp() */ /* must have this, in case exec() fails *//* 1 mark - handle exec error */ default: result = wait(&status); /* 1 marks - wait for child */ if (result == -1){ /* 1 mark - check for error in wait() */ perror("error while waiting for child! "); if (WIFEXITED(status)){ /* 1 marks - check status returned by child */ prf("%s exited with code %d\n", argv[2], WEXITSTATUS(status)); if (WEXITSTATUS(status)) { /* error */ switch (fork())/* 2 marks - create child */ { case -1: perror("unable to create process for <progsafe>\n"); case 0: /* child */ execlp(argv[1], argv[1], (char *)0); /* 1 mark - call to execlp() */ /* 1 mark - handle execlp() failure */ default: result = wait(&status); /* 1 mark - wait for child */ if (result == -1){ /* 1 mark - check for error in wait() */ perror("error waiting for <progsafe>\n"); else { if (WIFEXITED(status)) /* 1 mark - check return status */ prf("%s exited with status = %d\n", argv[1], WEXITSTATUS(status)); else prf("%s exited abnormally\n", argv[1]); else prf("%s exited with status = 0\n", argv[2]); else { CSC209F-99 Midterm, October 26 th, 1999 Page 5 of 5

fprf(stderr,"%s exited abnormally!\n", argv[2]); exit(0); This could be done in different ways, for example you might use if-else instead of switch when invoking fork(). Given the way in which command line parameters are set up, it is pretty much necessary to use execvp() to handle the execution of <prog> (allows for easy use of existing command line parameters), and execlp() for the execution of <progsafe> since we must pass at least one parameter, arg[0]. The "p" versions of execl/v are used since we want the system to search the path for us automatically. It should be noted that the example shown here is complete and tested: you did not need something this complete to get full marks. As stated in class, you did not need to list any of the include files. CSC209F-99 Midterm, October 26 th, 1999 Page 6 of 6

Macros & Function Prototypes I/O char *fgets(char *s, n, FILE *stream) FILE *fopen(const char *file, const char *mode) close( fd) fclose(file *stream) fflush(file *stream) fileno(file *stream) fprf(file *stream, const char *format, ) fscanf(file *stream, const char *format, ) listen( soc, n) open(const char *path, oflag) sprf(char *s, const char *format, ) write( fd, void *buf, nbyte) ssize_t read( fd, void *buf, size_t nbyte) Process Management execl(const char *path, char *argv0,, (char *)0) execle(const char *path, char *argv0,, (char *)0, const char *envp[]) execlp(const char *file, char *argv0,, (char *)0) execv(const char *path, char *argv[]) execve(const char *path, char *argv[], const char *envp[]) execvp(const char *file, char *argv[]) wait( &status) waitpid( pid, *stat, options) WIFEXITED( status) WIFSTOPPED( status) WIFSIGNALLED( status) WEXITSTATUS(status) WTERMSIG( status) WSTOPSIG( status) pid_t fork(void) String Handling char *strtok(char *s, const char *delim) char *strcpy(char *dest, const char *srce) char *strncpy(char *dest, const char *srce, count) strlen(const char *s) strcmp(const char *s1, const char *s2) strncmp(const char *s1, const char *s2, count) Directory Structure DIR *opendir(const char *filename); closedir(dir *dirp); lstat(const char *path, struct stat *buf); S_ISDIR(mode); stat(const char *path, struct stat *buf); long telldir(dir *dirp); struct dirent *readdir(dir *dirp); void rewinddir(dir *dirp); void seekdir(dir *dirp, long loc); struct stat { mode_t st_mode; /* File mode (see mknod(2)) */ time_t st_atime; /* Time of last access */ time_t st_mtime; /* Time of last data modification */ time_t st_ctime; /* Time of last file status change */ off_t st_size; /* File size in bytes */ nlink_t st_nlink; /* Number of links */ uid_t st_uid; /* User ID of the file's owner */ gid_t st_gid; /* Group ID of the file's group */ ; Struct dirent { ino_t off_t unsigned short char d_ino; d_off; d_reclen; d_name[1]; CSC209F-99 Midterm, October 26 th, 1999 Aid Sheet