| OpSys Spring 2006 - HW2 |
|   OpSys Home   |   Assignment   |   Directories   |   Threads   |   Requirements   |   Submitting   |   Resources |
- Recursive file/directory size report.
The objectives of this assignment are:
stat()system call to determine some of
the attributes of files and directories.opendir() and
readdir()).fork()) and
threads (pthread_create()).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, all directory names must end with a
'/'. 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").
NOTE: Since your program will need to open many (nested) directories, you will either need to:
chdir() to change the current working
directory before calling opendir.opendir.If your program starts in directory "home" (in picture above), then
calls opendir("joe") it will work, but if it wants to
open the subdirectory "joe/private" it must either call
opendir("joe/private")
or do something like chdir("joe"); opendir("private");.
- Using the Posix Threads Library
Your program must use the Posix threads library (pthreads) on
the CS BSD machines (freebsd.remote.cs.rpi.edu, or any of
monica, ashley or mary-kate.cs.rpi.edu).
To link your program with the pthreads library on
the CS BSD machines you need to add -lpthread 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
# use this if you use good'ole gcc
CC = gcc
# use this if you want c99 version
CC = gcc3
hw2: hw2.c
${CC} -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 another OS you may need to change the
definition of the make variable THREAD
- Project Requirements
The following are the requirements for the project:
Your program must compile and run on the CS department FreeBSD
machines (freebsd.remote.cs.rpi.edu).
Your program must provide a complete, indented output similar to the output shown in the example provided above.
The sizes and names of all files must be correct.
Every directory name must be followed by a '/'
Your program must use fork() to process each directory when the first command line argument is "-f". If the pathname provided on the command line is a directory, you do not need to create a child process to handle that directory (but you can).
Your program must use pthread_create() to process each directory when the first command line argument is "-t". If the pathname provided on the command line is a directory, you do not need to create a thread to handle that directory (but you can).
Your program must work with any valid pathname
specified on the command line. The pathname could be something like
/home or foo or foo.pdf or
even something like ../../fred.
Your submission must include a file named README that includes the following information:
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
Sample Code (discussed in lecture):
Posix Threads Resources:
Make