Objective – Tiny Shell (tish)
In this exercise you will write a small shell program to run different programs in foreground and background mode.
Usually when you login on to the system the program that takes control and takes care of your requests is the “shell” program. In this exercise you will implement only a small portion of the shell capabilities.
Your assignment is to build a program “tish” that will implement the following shell services:
• Run programs in foreground and background;
• List all processes that currently run in the background.
• Kill a process running in the background.
The precise requirements are as following:
• Your shell should give a user “tish>>” as a prompt;
• There are two types of commands that “tish” should understand:
o External commands: the command name is, in fact, the name of an executable file. When an external command is requested this means that the corresponding executable file should be run.
o Internal commands: commands that are “built-in” in the “tish” shell. For example, the “bye” command does not correspond to any executable file. Instead, it causes “tish” to execute the exit system call. Below we specify which internal commands you are required to implement.
• Execution mode for external commands:
o Foreground: a command is given in the following form tish>> <command_name> <list_of_parameters>. In this mode “tish” does not return the prompt until the executable file that corresponds to the command finishes.
o Background: a command is given in the same format as in the foreground mode, but the last parameter in the parameter list should be ‘&’. for example:
tish>> emacs &
• Example of running external commands:
o tish>> emacs my_file.
• List of Internal commands you are required to implement:
o bye : terminate “tish”. All background processes should be terminated. (Note that the real shell does not do this).
o jobs : list of all background jobs in the following format: <pid> <command name>, in the order of creation.
o kill :terminate the process corresponding to the specified pid by sending SIGTERM signal.
Recommendations and additional requirements:
• You are required to use fork() and execvp(const char* file, const char* argv) system calls. execvp() is a version of the execve() system call in which the path and environment does not need to be specified. The file name should not! include a ‘/’ char. If it does you should output a compatible error message. In this assignment you are required to deal with list of parameters no larger than 2. You are not required to check the type of the parameters you have received just to pass them on to the execvp() call in the right format.
• You are NOT allowed to use the library function system() instead of fork/exec. This is because system() itself forks a new shell that forks the specified command. Using system() is absolutely prohibited in this exercise.
• You may not assume that the user always specifies a correct input. For instance, the user may specify non-existent command name, or wrong parameters. Your program should print an appropriate error message. Note that empty or pure white space(s) command is not an error, and should be simply ignored and prompt should be repeated immediately
• In the foreground execution mode a running process is terminated using the “Ctrl-C” key combination. Pressing this combination generates signal SIGINT that is sent to the control terminal process group. Since both “tish” and all its children are members of this group they have to take care of this signal. It should be handled differently in the main loop of “tish” (the one that provides the prompt and reads the user’s input), and in a foreground child Namely:
o Main loop of “tish” (father): ignores SIGINT, catches SIGTERM (may be sent by an outside application, e.g., tcsh);
o Foreground child: default behavior, i.e., termination
• When a process finishes, it enters the zombie state. A process will remain in this state as long as it father is alive. Since all external commands are children of “tish” and “tish” may be kept alive very long, the process table will eventually fill up. In this exercise you can ignore the fact that the number of zombie files in the system is limited.
• “tish” command line is limited to 1024 characters
• You may not assume correctness of arguments for internal commands (letters instead of numbers, negative numbers, etc. all should be checked)
System Calls and Functions List
You may use “stdio” library functions in order to read user’s input and to print the messages. You are advised to use string library functions, such as strtok() to manipulate strings.
What to Hand In:
Email your source code files to email@example.com with the subject line of [your last name] + “Prog1”