Due: TBA
This assignment should be done in groups of 2 or 3 people.
The OS command interpreter is the program that people interact with in order to launch and control programs. On UNIX systems, the command interpreter is usually called the shell: it is a user-level program that gives people a command-line interface to launching, suspending, and killing other programs. sh, ksh, csh, tcsh, bash, ... are all examples of UNIX shells. (It might be useful to look at the manual pages of these shells, for example, type: "man csh".)
Every shell is structured as the following loop:
Although most of the commands people type on the prompt are the name of other UNIX programs (such as ls or more), shells recognize some special commands (called internal commands) which are not program names. For example, the exit command terminates the shell, and the cd command changes the current working directory. Shells directly make system calls to execute these commands, instead of forking a child process to handle them.
This assignment consists of two parts. In the first, you will design and implement an extremely simple shell that knows how to launch new programs, and also recognizes three internal commands (exit, cd, and execcounts), which we will describe below. The first two internal commands will work by calling existing system calls (exit and chdir); the third internal command will work by calling a new system call that you will design and implement. So, in the second part of this assignment, you will design and implement the execcounts system call. This will involve making changes to the Linux kernel source code. The semantics of the execcounts system call, and some hints on how to go about implementing it are also described below.
Part 1: Build a new
shell
Write a shell program in C which has the following features:
Part 2: Add a new system call
There are four system calls in Linux related to creating new
processes: fork, vfork, execve, and clone. (The man pages will describe for
you the differences among them.) Instrument the kernel to keep track of
the number of times each of these four system calls has been invoked and write
a user-level C program that will print counts of the number of times each of
them has been invoked (by any process on the system).
To do this requires three things:
Modify the kernel to keep track of this information.
Design and implement a new system call that will get this data back to the user application.
Write the user application.
We'd also like to be able to reset these statistics periodically. So we need a way to clear the request information we've tracked so far. This requires either parameterizing the above system call to add a clear option, or adding another system call.
There are several different ways to approach this problem. It is your job to analyze them from an engineering point-of-view, determine the trade-offs, and to explain the implementation you select.
Warning 1: Remember that the Linux kernel should be allowed to access any memory location, while the calling application should be prevented from causing the kernel to unwittingly read/write addresses other than those in its own address space. Details about this are here.
Warning 2 (Hint 0): Remember that it's inconceivable that this problem (warning 1) has never before been confronted in the existing kernel.
Warning 3: Remember that the kernel must never, ever trust the application to know what it's talking about when it makes a request, particularly with respect to parameters passed in from the application to the kernel.
Warning 4: Remember that you must be sure not to create security holes in the kernel with your code.
Warning 5: Remember that the kernel should not leak memory.
You should be using the C language whenever you alter or add to the Linux kernel.
You can't just make system calls directly from C. Instead, you need to use the syscall function and pass it the number of your new system call. The following code fragment show you how to do that:
/*
* Set the features included by the linux libc to have the BSD extensions
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
#define _NR_execcounts something
#include <unistd.h>
...
int ret = syscall(__NR_execcounts, ...);
Here’s a suggested set of incremental steps for completing this assignment:
Now implement a parameter-less system call, whose body is just a printk() call. Write a user-level routine that invokes it. Check to make sure it was invoked.
Now write the full implementation.
Part 3: Integrate the
system call into the shell
Now that you have a working shell and an implementation of your new system call, it's time to integrate them; this should be very simple. Add a new internal command to your shell, called execcounts. The execcounts command should invoke the system call that you build in Part 2, and print out:
Part 4: Some
additional questions
Answer these additional questions and include
them with your write-up:
1) What is "asmlinkage" as it occurs
in the Linux kernel source, what does it do (give a short description)?
2) gotos
are generally considered bad programming style, but these are used frequently
in the Linux kernel, why could this be? This is a thinking question, so
justification is more important than your answer.
3) What is the difference between the "clone" and "fork"
system calls?
4) How could you extend your shell to support multiple simultaneous processes
(foreground and background...)?
5) How long does your new system call take (time it using gettimeofday
and give an approximate answer)? Explain your timing methodology.
What to Turn In
You should turn in the following:
Describe how you found the information needed to complete this project. Did it have the information you needed? Did you consult with any humans? If so, what did you try first and who did you consult with?
Explain the calling sequence that makes your system call work. First, a user program calls <.....>. Then, <.....> calls <.....>. ... and so on. You can explain this using either text or a rough (less than 15 minutes) diagram.
Why do you think the designers of Linux implemented system calls the way they did? What were they trying to achieve? What were they trying to avoid?
Give (in 1-2 sentences) an alternative idea for implementing system calls. State one way your idea would be better or worse than the way it is currently done.
Do not underestimate the importance of the write-up. Your project grade depends significantly on how well you understood what you were doing, and the write-up is the best way for you to demonstrate that understanding.
The grade on the project will be calculated as follows:
Submission instructions: We will be using the submit program.