CPSC 457: Operating Systems

Professor Carey Williamson

Winter 2010

Assignment 1 (30 marks)
Due: February 1, 2010 (11:59pm)

The purpose of this assignment is to gain experience with shell programming and with C programming in a Linux environment. For the shell programming, you can write the scripts in the shell environment of your choice (e.g., sh, csh, bsh, ksh), but please document properly so that your TA can test your scripts in the correct environment. For the C programming, you can use any suitable C compiler, such as cc or gcc. You can do all of your development and testing for this assignment in a regular Linux environment. There is no need for UML (but you can use UML if you wish to do so).

Shell Programming (20 marks)

Q1. File Counting (5 marks)

Write a Linux shell script numfiles that counts and reports the number of files in the current working directory. Only regular files should be counted, not directories or subdirectories. (The main intent is to distinguish directories from regular files. You do not need to worry about other special cases, such as hard links, soft links, and character special device files.) There is no need to recursively examine subdirectories for other files. Show the output of your script on the /etc directory of your Linux system.

Q2. File Byte Counting (5 marks)

Write a Linux shell script numbytes that counts and reports the number of bytes consumed by the files in the current working directory. Only regular files should be counted, not directories or subdirectories. There is no need to recursively examine subdirectories for other files. Show the output of your script on the /etc directory of your Linux system.

Q3. File Size Comparison (5 marks)

Write a Linux shell script larger X Y that compares the sizes of two specified files X and Y, and reports which file is larger. For example, if X is larger, the output should be "File X is larger", while if Y is larger, the output should be "File Y is larger". If the files are exactly the same size, the output should be "File X and File Y are exactly the same size". If X and/or Y are not valid names of existing files, then report a reasonable error message, such as "File X does not exist". If fewer than two command line arguments are specified, an appropriate error message should be given. Show the output of your script when comparing the size of your shell script called larger to the size of the /etc/passwd file on your Linux system.

Q4. Comparing Directories (5 marks)

Write a Linux shell script morefiles D1 D2 that compares two specified directories D1 and D2, and reports which directory contains more files. Only regular files should be counted, not directories or subdirectories. There is no need to recursively examine subdirectories for other files. Output format and error handling should be analagous to that for the previous script for file size comparison. Show the output of morefiles /etc /tmp on your Linux system.

C Programming (10 marks)

Q5. File Byte Counting (10 marks)

Write a C program bytes.c that counts and reports the number of bytes of storage space that are consumed by all files in the current working directory. Only regular files should be counted, not directories or subdirectories. There is no need to recursively examine subdirectories for other files. Show the output of your program on the /etc directory of your Linux system. Compare the output of your C program to that from your shell script for the same task. Do they agree? If not, why not? Also compare the execution time performance of your C program to the execution time performance for your shell script for the same task. Which is faster, and why?

Bonus (up to 4 marks)

Extend one or more of your programs above to support recursive processing of subdirectories (1 mark for each, except for Q3, where it makes no sense to do so).

Tips

Generic tip: Almost anything that you want to do in Linux can be done either as an application program, as a shell script, or as a kernel system call. The trick (if any) is to know what existing utilities are out there, and how to put them together in the right way to accomplish what you are trying to do.

More specific tip: For the shell programming parts of the assignment, you might want to look at the manual pages for one or more of the following utilities: ls, find, stat, awk, grep, wc, dc, bc.

Another tip: From the shell, there is a way to redirect the standard error output to get rid of unwanted error messages, if you want to. Optional.

Yet another tip: For the C programming part of the assignment, there are at least three approaches: opening the directory as if it was a regular file and parsing the data to get what you need; using the stat system call to traverse the file system and get the data that you need; or using the "system()" system call to invoke a shell command to generate the data that you need. There might be others. Difficulty varies.

The "script" command is a convenient way to record the activity of a Linux terminal session.

The "time" command is a convenient way to measure the execution time required by a Linux command.

Submitting Your Assignment

When you are finished, send your solutions directly to your assigned TA via email, using a single email attachment (e.g., gzipped tar file, including a README file, shell scripts, C source code, and sample output). Multiple repeated submissions from the same student are frowned upon, as are multiple email attachments. Please put "CPSC 457: Assignment 1" in the subject line of your email. Submissions must be received on or before the stated submission deadline, otherwise a late penalty of 10% (3 marks) per day will apply.