CS 502 Operating Systems WPI,
Spring 2006
Hugh C. Lauer Project
1 (10 points)
Assigned: Monday, February 6, 2006 Due:
Monday, February 20, 2006
This assignment is intended to introduce you to the process manipulation facilities in the Unix and Linux Operating Systems. You are to implement the program described below so that it runs on any CS or CCC Unix/Linux machine.
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
would invoke the cat command on the file /etc/motd, which will 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.
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 Unix commands to the shell, section 2 is for Unix system calls, and section 3 is for Unix library routines. 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
· execve() – execute a file. The library routine execvp() might also be useful.
· 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.
Extend your doit program to behave like a shell program. The program should continually prompt for a command, which may have multiple arguments separated by white space, and then it should execute each command and print the statistics as above.
Your program should handle two “built-in” shell commands:–
· exit — causes your shell to terminate.
· cd dir — causes your shell to change the directory to dir.
Your program 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.
% doit
==>cat /etc/motd
< print the
current message of the day >
< 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 Unix prompt >
A helpful function for this part of the project are
· chdir() – change working directory.
Satisfactory completion of this part of the project is worth three of the ten points of the project.
Extend the basic command shell to handle background tasks. 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 to add an additional built-in command to your shell:
· jobs – lists all background tasks
A sample session with background tasks is given below with comments in < >.
% doit
==>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 job done >
< statistics
about numbercrunch 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 and wait() for those tasks to be completed.
You should observe how this mini-shell works in comparison to a regular Unix 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 of the project.
Submit your assignment for grading as directed in class.
We will attempt to 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
This class is ‘cs502’, and the assignment is ‘project1’. Therefore, the turning command would be
/cs/bin/turning submit cs502 project1
<your files>