Systems Programming
Major Assignment 2 – The Shell and System Calls
COLLABORATION
You should complete this assignment as a group assignment with the other members of your group as assigned on Canvas using our GitLab environment (i.e., the same group you had for Major 1). If desired, you may submit only one program per group, but all source code must be committed in GitLab. Also, make sure that you list the names of all group members who participated in this assignment for each member to get credit.
BACKGROUND
A shell provides a command-line interface for users. It interprets user commands and executes them. Some shells provide simple scripting terms, such as if or while, and allow users to make a program that facilitates their computing environment. Under the hood, a shell is just another user program. The file /bin/bash is an executable file for the bash shell. The only thing special about your login shell is that it is listed in your login record so that /bin/login (i.e., the program that prompts you for your password) knows what program to start when you log in. If you run "cat /etc/passwd", you will see the login records of the machine.
PROGRAM DESCRIPTION
GROUP COLLABORATIVE PORTION
In this assignment, you will implement the shell “engine” as the “group” component, where all members are responsible for the following functionality:
• A Command-Line Interpreter, or Shell
Your shell should read the line from standard input (i.e., interactive mode) or a file (i.e., batch mode), parse the line with command and arguments, execute the command with arguments, and then prompt for more input (i.e., the shell prompt) when it has finished.
1. Interactive Mode
In interactive mode, you will display a prompt (any string of your choosing) and the user of the shell will type in a command at the prompt.
2. Batch Mode
In batch mode, your shell is started by specifying a batch file on its command line. The batch file contains the list of commands that should be executed. In batch mode, you should not display a prompt, but you should echo each line you read from the batch file back to the user before executing it.
You will need to use the fork()and exec()family of system calls. You may not use the system() system call as it simply invokes the system’s /bin/bash shell to do all of the work.
You may assume that arguments are separated by whitespace. You do not have to deal with special characters such as ', ", \, etc. However, you will need to handle the redirection operators (< and >) and the pipeline operator (|), which will be specified in the “individual” portion of this assignment.
Each line (either in the batch file or typed at the prompt) may contain multiple commands separate with the semicolon (;) character. Each command separated by a ; should be run sequentially (i.e. when you use semicolons, each command/program will run regardless if the preceding one fails), but the shell should not print the next prompt or take more input until all of these commands have finished executing (the wait() or waitpid() system calls may be useful here).
You may assume that the command-line a user types is not longer than 512 bytes (including the '\n'), but you should not assume that there is any restriction on the number of arguments to a given command.
INDIVIDUAL PORTION
In this assignment, each member of the group will implement the following components as defined below. This means that the individual group member responsible for each portion MUST commit in GitLab the code that supports their responsible area.
• Built-In Commands
Every shell needs to support several built-in commands, which are functions in the shell itself, not external programs. Shells directly make system calls to execute built-in commands, instead of forking a child process to handle them. Each group member is expected to implement 1 of the following built-in commands.
Note that the expectation for this assignment assumes that a group contains 4 students, but if, for some reason, a team has only 3 students, then only 3 of the following built-in commands would need to be supported (i.e., 1 for each group member).
1. Add a new built-in cd command that accepts one optional argument, a directory path, and changes the current working directory to that directory. If no argument is passed, the command will change the current working directory to the user’s HOME directory. You may need to invoke the chdir() system call.
2. Add a new built-in exit command that exits from the shell itself with the exit() system call. It is not to be executed like other programs the user types in. If the exit command is on the same line with other commands, you should ensure that the other commands execute (and finish) before you exit your shell.
These are all valid examples for quitting the shell:
prompt> exit
prompt> exit; cat file1 prompt> cat file1; exit
3. Add a new built-in path command that allows users to show the current pathname list, append one pathname, or remove one pathname. In your shell implementation, you may keep a data structure to deal with the pathname list. If you do not use execle() or execve() that allows you to execute with your own environment variables, you will need to add it to the “real” PATH environment variable for executables in the path to work correctly. The initial value of path within your shell shall be the pathname list contained in the PATH environment variable. Implement the path command as follows:
• path (without arguments) displays the pathnames currently set. It should show pathnames separated by colons. For example, "/bin:/user/bin".
• path + ./bin appends the pathname to the path variable. You may assume that only one pathname is added at a time.
• path - ./bin removes the pathname to the path variable. You may assume that only one pathname is removed at a time.
You may assume that there are no duplicate pathnames present, being added, or being deleted. You will restore your PATH environment variable to its original state (i.e., before your shell was invoked) when the user exits your shell.
4. Add a new built-in myhistory command that lists the shell history of previous commands run in your shell (not the bash shell). Note that this does not have to work with the up-arrow key as in bash, but only with a new myhistory command run inside your shell. You may not make use of the history builtin command, but instead keep track of your history of commands in some sort of data structure. Your myhistory built-in command should support a history of 20 most recent commands (i.e., this means that the 21st command will overwrite the 1st command, for example). Your myhistory command should support a couple flags: -c to clear your myhistory list by deleting all entries, and -e <myhistory_number> to execute one of your twenty myhistory commands in your list.
• Redirection, Pipelining, Signal Control, and Alias Support:
1. Extend your shell with I/O redirection (mandatory for teams of 3 or 4)
When you start a command, there are always three default file streams open: stdin (maps input from the keyboard by default), stdout (maps output to the terminal by default), and stderr (maps error messages to the terminal by default). These and other open files may be redirected, or mapped, to files or devices that users specify.
Modify your shell so that it supports redirecting stdin and stdout to files.
You do not need to support redirection for your shell built-in commands (i.e.,
cd, exit, path, and myhistory). You do not need to support stderr redirection or appending to files (e.g., cmd3 >> out.txt). You may assume that there will always be spaces around the special characters < and >. Be aware that the "< file" or "> file" are not passed as arguments to your shell program.
Some redirection examples include:
$ cmd1 < in.txt
executes cmd1, using in.txt as the source of input, instead of the keyboard.
$ cmd2 > out.txt
executes cmd2 and places the output to file out.txt.
You will need to understand Linux file descriptors and use the open(), close(), and dup()/dup2() family of system calls. This portion of the project should only require implementing single input redirection and single output redirection (not both redirection or working with pipes).
2. Extend your shell with pipelining (mandatory for teams of 3 or 4)
The command
$ cmd1 | cmd2 | cmd3
connects the standard output of cmd1 to the standard input of cmd2, and again connects the standard output of cmd2 to the standard input of cmd3 using the pipeline operator '|'.
You will need to use the pipe() system call. Your shell should be able to handle up to three commands chained together with the pipeline operator (i.e., your shell should support up to two pipes pipelined together). This portion of the project should only require implementing support for a pipeline of 2 pipes/3 commands (no working with redirection).
Your shell does not need to handle built-in commands implemented above (i.e., cd, exit, path, and myhistory) in pipeline.
3. Support Signal Handling and Terminal Control (mandatory for teams of 3 or 4)
Many shells allow you to stop or pause processes with special keystrokes, such as Ctrl-C or Ctrl-Z, that work by sending signals to the shell’s subprocesses. If you try these keystrokes in your shell, the signals would be sent directly to the shell process itself. This is not what we want since, for example, attempting to Ctrl-Z a subprocess of your shell will also stop the shell itself. Instead, we want to have the signals affect only the subprocesses that our shell creates. To help you accomplish this, you might find the following helpful:
a. Process Groups
Every process has a unique process ID (i.e., pid). Every process also has a possibly non-unique process group ID (i.e., pgid) which, by default, is the same as the pgid of its parent process. Processes can get and set their process group ID with the system calls getpgid(), setpgid(), getpgrp(), or setpgrp().
Keep in mind that, when your shell starts a new program, that program might require multiple processes to function correctly. All these processes will inherit the same process group ID of the original process. So, it may be a good idea to put each shell process into its own process group for simplicity. When you move each subprocess into its own process group, the pgid should be equal to the pid.
b. Foreground Terminal
Every terminal has an associated foreground process group ID. When you type Ctrl-C, your terminal sends a signal to every process inside the foreground process group. You can change which process group is in the foreground of a terminal with tcsetpgrp(int fd, pid_t pgrp). The fd should be 0 for standard input stdin. You may want to perform a man tcsetpgrp command for additional help and implications on using this library call.
In your shell, you can use kill –XXX pid, where XXX is the human-friendly suffix of the desired signal, to send any signal to the process with process ID pid. Since you can use the signal function to change how signals are handled by the current process, your shell should basically ignore most of these signals, whereas your shell’s subprocesses should respond with the default action. Be aware that forked processes will inherit the signal handlers of the original process. You may want to check out man 2 signal and man 7 signal for
more information on this. You want to ensure that each program you start is in its own process group. When you start a process, its process group should be placed in the foreground. Stopping signals should only affect the foreground program(s), not the background shell.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of