CS201 - System Programming: Shell Implementation Assignment

Verified

Added on Ā 2019/09/16

|2
|839
|175
Homework Assignment
AI Summary
This assignment focuses on system programming and requires the implementation of a text-based shell. The solution involves creating a shell that can execute commands, fork child processes using `fork()` and `execvp()` in Linux, or using equivalent methods in Windows or Java. The assignment includes implementing the `cd` built-in command, enabling background job execution using '&', implementing a history command to display the last 10 commands, and supporting pipes using the '|' symbol to redirect output. The student is required to submit source code files and a readme file detailing compilation and sample output. An extra credit exercise involves supporting multiple commands separated by ';'. The shell implementation covers core concepts in operating systems such as process management, input/output redirection, and command parsing.
Document Page
tmpx6sphy9n.doc 1
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 few 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.
1. 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%)
2. Implement built in command cd (change current directory) using chdir() system
call. (10%)
3. 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)
4. Implement built in command history which will show the last 10 commands
typed. (20 points)
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
tmpx6sphy9n.doc 2
5. 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 &
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]