OpSys Fall 2005 - HW2

Creating processes and threads (and the stat() system call)

Due Date: Wed, Sep 28th by 11:59PM

Submit to WebCT drop box labeled HW2

- Recursive file/directory size report.

The objectives of this assignment are:

  1. Use the stat()system call to determine some of the attributes of files and directories.
  2. Learn how to process directories ( opendir() and readdir()).
  3. Begin to explore creating processes (fork()) and threads (pthread_create()).
  4. Deal with general systems programming issues including checking for, and handling of errors.

You are to write a program that accepts two command line parameters. Your program assumes the first parameter is either "-t" (meaning use threads), or "-f" (meaning use fork). The second parameter the path name of a file or directory. If your program gets the path of a file, it should print out the size of the file in bytes. If your program gets the path of a directory, it should print out the directory name, then process (in the same way) all the entries in the directory except "." (the directory itself) and ".." (the parent directory). If your program is given a directory, it must display the entire hierarchy rooted at the specified directory.

Below is a sample session. The output of the program is shown in blue, the command line typed by a human user is in black. This output corresponds to the directory structure shown in the diagram below.

> ./hw2 -t home

home/
  dave/
    hw2.c       2563
    check.c     640
  joe/
    projects    274
    public/
      pictures/
        pic1.jpg        2716
        pic2.jpg        9569
        small.gif       511
    private/
      email/
        msg1    228
        msg2    152
        msg3    76
  sally/
    opsys/
      hw2.c     2563
      Makefile  181
    proglang/
      projects/
        proj1/
          assignment    54
          solution.c    62
        proj2/
          solution.c    62
          assignment    248
          notes 186
    modcomp/

Note that output shown above is indented to reflect the hierarchy, this is a project requirement! Your output must include the name of each directory visited by the program, and all entries within that directory must be indented. Each file shown must include the file name (not the entire path, just the basename) and the size of the file in bytes. Your program should ignore anything that is not a regular file or a directory.

When your program is run, it will be given either "-t" or "-f" as the first command line parameter. If "-t" is given, then your program must create a new thread to handle each directory it finds. The new thread must process an entire directory in the same way, so if it find subdirectories each must be handled by yet another thread. When using threads, you should use joinable threads to make sure that only one thread is active at any time (we are not processing directories in parallel).

If your program is given "-f" as the first argument, it should create a new process to handle each directory (using fork()). The child process should handle the directory, creating it's own child processes whenever it finds a subdirectory. As with threads, there should never be more than one active process - so whenever you call fork() the parent should wait for the child to complete before continuing on (the parent should call waitpid).

For example, using the directory structure shown above, and assuming that "-f" is specified, the program would first find the subdirectory named "dave" and create a child process to print out the hierarchy rooted at the directory "dave". The child process should not call exec, it should handle the directory dave itself. In this case it finds and prints our information about the two files in directory "dave". Once this is complete, the child process exits and at this point wait will return in the parent process. The parent now finds the directory named "joe", creates a child to handle "joe", and so on. Since there is a total of 13 directories below "home" in the above example, there should be a total of 13 calls to fork.

Note that from the output of the program there is no way to tell if the program uses processes or threads.

- Reading Directories

Your program must be able to determine the contents of a directory. You can open a directory with the opendir system call, and read the names of items in a directory with the readdir function. Both of these have man pages ("man opendir", "man readdir").

- Using the Posix Threads Library

Your program must use the Posix threads library (pthreads) on the CS BSD machines. To link your program with the pthreads library on the CS BSD machines you need to add -lpthreads to your compile command. Your compilation should look something like this:

gcc -o hw2 -Wall hw2.c -lpthread

Below is a sample Makefile that will compile a file named "hw2.c" and create and executable named "hw2" including the threads library:

all: hw2

# ON CS BSD machines uncomment the next line
THREAD=-lpthread

# For use under Linux, use this one instead
#THREAD=-pthread


hw2: hw2.c
        gcc -o hw2 -Wall hw2.c ${THREAD}

Remember that if you copy/paste from the above, it won't include the tab character needed in the rule for building hw2!

The Makefile above will work as is on the BSD machines, if you are developing on a Linux machine you need to change the definition of the make variable THREAD

- Project Requirements

The following are the requirements for the project:

Grading: Grades will be based on the formula below. Note that to get full credit we must be able to understand your code (it must be commented!)

30%Proper output given any valid pathname
30%Use fork() correctly.
30%Use threads correctly
10%Code quality (comments, organization, how hard is it to understand ?).

You can get partial credit for any part (for example if you don't get all the commands working properly).

If you code does not compile and run under FreeBSD on the CS machines, you will lose at least 50% (the remaining 50% partial credit will be awarded based on visual inspection of the code).

- How to Submit

Log in to WebCT at webct.rpi.edu using your RCS id and password. Once you get to MyWebCT click on "Operating Systems", and from there go to the homework drop boxes. Submit your files (individually, zipped or tarred) to the drop box labeled HW2

-Resources