CS 502 Operating Systems WPI,
Fall 2006
Hugh C. Lauer Project
1 (10 points)
Assigned: Monday, September 18, 2006 Due:
Monday, September 25, 2006
This is an individual assignment, not a team project. The project is intended to introduce you to the process manipulation facilities in operating systems such as Linux, Unix, or Windows. You are to implement the program described below so that it runs on your virtual machine or any CCC or CS Linux machine.[1]
Write a program doit that takes another command as an argument, executes that command, and prints statistics about it. For instance, executing
% doit cat /etc/motd
invokes the cat command on the file /etc/motd to print the current “message of the day.” After execution of the specified command has completed, doit should display the following statistics about the system resources the command used:–
1. the amount of CPU time used (both user and system time) in milliseconds,
2. the elapsed “wall-clock” time for the command to execute in milliseconds,
3. the number of times the process was preempted involuntarily (e.g., time quantum expired, preemption by higher priority process, etc.),
4. the number of times the process gave up the CPU voluntarily (e.g., waiting for an I/O or resource),
5. the number of page faults, and
6. the number of page faults that could be satisfied from the kernel’s internal cache (e.g., that did not require any input/output operations).
See below for how to get the information for these statistics.
You may not use the system call system() to execute the given command. Instead, you must extract the command from the input line, fork a new process, and cause that process to execute the command with its arguments via a version of exec().
Satisfactory completion of this part is worth five of the ten points of the project.
Part of the purpose of this assignment is for you to learn how to find information in the online documentation of Unix and Linux (called man pages) and, from that documentation, to learn how to invoke the various system facilities from your program.
For example, to learn about the fork() function, type
% man fork
to your favorite Unix or Linux shell. Manual pages are organized into sections. Section 1 is for commands to the shell, section 2 is for system calls, and section 3 is for library routines, etc. Some entries are contained in more than one section. For example,
% man wait
will give you the manual page for the wait command typed to a shell, while
% man 2 wait
will give you the manual page for the wait() system call.
% man man
tells you how to use the man command to view and/or print manual pages.
For this part of the assignment, the following systems calls are needed:–
· fork() — create a new process by cloning an existing one
· execvp() or one of its variants – execute a file. This is a front-end for the system call execve(), which replaces the current process with a new program to execute.
· wait() – wait for a process to terminate.
· getrusage() – get information about resource utilization.
· gettimeofday() – get current time for calculation of wall-clock time.
· strtok() – assistance in parsing strings.
Make a copy of your doit program called shell and extend it to behave like a simple command shell. Your shell should continually prompt for a command, which may have multiple arguments separated by white space, and then it should execute each command the same way doit did and print the statistics as above.
Your shell should recognize and handle two “built-in” shell commands:–
· exit — causes your shell to terminate.
· cd dir — causes your shell to change the directory to dir.
Your shell should exit if an end-of-file is detected on input, and it should complain if an illegal command is typed.
You may design your shell for lines of input containing not more than 128 characters and not more than 32 distinct arguments. However, you should check that this condition is not violated and print an error if necessary.
A sample session of your shell is given below, with comments in /*…*/. The prompt of this example is ==>, but you may use a different prompt.
% shell
==>cat /etc/motd
/* print the
current message of the day */
/* print statistics
about the cat command */
==>cd dir
/* current
directory is changed to dir */
==>ls
/* listing of files
in the current directory */
/* statistics about
this ls command */
==>exit
% /* back to the prompt of your Linux shell
*/
A helpful function for this part of the project is
· chdir() – change working directory.
Satisfactory completion of this part of the project is worth three of the ten points.
Make a copy the command shell of part 2, and extend that copy to handle background tasks. Call this copy shell2. A background task is indicated by putting an ampersand (‘&’) character at the end of an input line. When a task is run in background, your shell should not wait for the task to complete, but instead it should immediately prompt the user for another command. Note that any output from the background command will be directed to the terminal display and will intermingle with the output from your shell and from other commands.
With background tasks, you will need to modify your use of the wait() system call so that you check the process id that it returns. The returned process id might correspond to a background task rather than the currently invoked foreground task. In this case, your shell should print out the process id of the completed background task along with the command name. You also need to add the following additional built-in command to your shell:
· jobs – lists all background tasks
A sample session with background tasks is given below.
% shell2
==*/numbercrunch &
[1] 12345 /* indicate
background task #1 and process id */
==*/jobs
[1] 12345 numbercrunch
/* print process id
and command name for tasks */
==*/ls
/* listing of files
in the current directory */
/* statistics about
this ls command */
==*/cat /etc/motd
[1] 12345 Completed <
indicate background command */
/* statistics about
background command */
/* print the
current message of the day */
/* statistics about
this cat command */
==*/exit
% /* back to Unix prompt */
If the user tries to exit the shell before all background tasks have completed, then the shell should refuse the exit, print a message, and wait() for those tasks to be completed.
You should observe how this mini-shell works in comparison to a regular Linux shell. Does it have all of the same features? What limitations does it have? Include your observations and comments in your code that you turn in.
Satisfactory completion of this part of the project is worth two of the ten points.
Submit your assignment for grading as directed in class. We will use turnin, the command line tool for turning in assignments. Help information about this tool can be found on
http://web.cs.wpi.edu/Help/turnin.html
For purposes of the turnin program, this class ‘cs502’, and the assignment is ‘project1’. Therefore, the turnin command would be
/cs/bin/turnin submit cs502 project1 <your files>
Your submission should include
1. A write-up explaining your project and anything that you feel the instructor should know when grading the project. In particular, explain how you tested you programs. Write-ups in Word or PDF format are strongly preferred.
2. All of the files containing the code for all parts of the assignment.
3. One file called Makefile that can be used by the make command for building the executable programs. It should support the “make clean” command, “make all” and make individual parts of the assignment.
4. The test files or input that you use to convince yourself (and others) that your programs actually work.
5. Files that capture the input and output for running and testing the programs.
Do not put separate parts of the assignment in separate folders or specify separate makefiles for them. Do not zip everything together into a zip file.
Note that turnin only runs on CCC machines. Therefore, you will have to copy your files to your CCC account first. This is an inconvenience, but it is the least inconvenient method that is established in reliable usage.
This is an individual project, not a team project. Each student should submit his/her own work, not copies of jointly developed code.
Nevertheless, if you are puzzled or unsure of some aspect of this assignment, you should consult your friends, colleagues, or the instructor to help clarify your understand or derive an approach to the problem.
[1] There are sometimes differences between the CCC and CS compilers. The CCC runs Red Hat Linux, while CS runs SUSE Linux. As of this writing, both the Linux kernel and the C/C++ compiler are slightly later revision than those on CS computers, but the default virtual machine is the latest.