Description
System Programming: A text- based shell is a program that users often interact with. It accepts commands from the users and carries them out, often by issuing corresponding requests to the underlying operating system. There are quite a f ew different shell program existed, such as sh, bash, csh, tcsh, ksh etc. Bash has been the default shell on many Linux system. The name bash stands for “Bourne again shell” where “Bourne shell” was the name of the original sh shell program for Unix. The bash shell uses much of the same syntax as the original sh shell, but adds a number of useful features.
Bash shell (or any other shell) is simply an executable program that is started when you open a terminal window(e.g press Ctrl-Alt-T in ubuntu. It can also be executed by simply typing it (e.g. bash) from command line in another shell program. It first prints a prompt, then the user types a command and presses Enter. Soon, the program runs. Usually the shell can be terminated by typing exit from command line. Though each shell may have different styles and strengths, they all serve the same purpose and generally provide three main functions: run programs, manage input and output, and be programmed. Most commands used in the shell are just regular executable programs. Some commands are built-in programs that can be found in /bin directory, such as ls, cp, rm. Others may be the external programs, such as emacs, firefox. The shell loads the programs into memory and runs them. Besides using standard input/output such as keyboard/console, most shell can also attach input/output to files or other processes by using <, > and |. The shell is also a programming language with variables and flow controls. Shell script provides a convenient and powerful way to run commands with special environments. Open a terminal by pressing Ctrl+Alt+T, run the following commands (separated by comma) each in a separate line and check the results: ps, pwd, bash, ps, ls, ls /bin, firefox &, ps aux | grep firefox, ls /bin > builtincmds, less builtincmds, history, gedit.
In this assignment, you are required to extend the very basic shell I provided to you or rewrite it from scratch. You can use C or C++ or even Java.
- The basic shell that accepts a single command with parameters from keyboard, fork a child process and run it. In Linux, you shall use fork() and execvp() to create new process and execute the external commands. You shall NOT use system(). In windows, you shall use win32 API on process creation such as CreateProcess(). In Java, you need use Runtime class and corresponding methods such as exec() to create a new process and execute it. (40%)
- Implement built in command cd (change current directory) using chdir() system call. (10%)
- Enable a command to be executed as a background job by using &. That means the shell can proceed to accept another command without waiting the current command to finish. (Be aware that there can be space before & too, such as emacs& or emacs &). (10% point)
- Implement built in command history which will show the last 10 commands typed. (20 points)
- Implement a pipe using symbol “|”. A pipe is a one-way data channel in the kernel. It connects the standard output of the first process and the standard input of the second process to a pipe. For example: type ls -s | sort -n (or without space ls -s|sort -n). It will sort the content of current directory based on file size and display it. A pipe connects the standard output of command ls and the standard input of command sort. (20 points)
What to submit: please submit all source code files and a simple readme file to describe how to compile your code and also include the sample output. Please zip all files into a single zip file and submit it through blackboard.
Extra-Credit Exercise: System Programming (10%)
Extend the previous program in problem 6 to support multiple commands be executed in a single line separated by “;” , such as ls -s >lsoutput; sort -n<lsoutput; emacs &
Homework Feedback (1 points)
- How long do you take to complete this homework? Is it too hard, too easy or OK?
- Do you have any other feedback?
- How well do you understand “Process and Thread”? Any questions?