Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSCI 4 1 0 0 Assignment 2 Instructions For this programming assignment you are going to implement a simple shell called smash in C .

CSCI 4100 Assignment 2
Instructions
For this programming assignment you are going to implement a simple shell called smash
in C. This shell will be able to process command lines in the following forms:
command [args]
command [args]> file (output redirected to write to file)
command [args]< file (input redirected to read from file)
command1[args1]| command2[args2](output of command1 piped into input of
command2)
The shell should do the following:
1. Display a command prompt.
2. Read a line of text from the user.
3. Break the command line into individual arguments.
4. Process the line of text and do one of the following:
(a) If the line is blank, go back to step 1.
(b) If the line is the exit command, exit the shell.
(c) Otherwise attempt to execute the command.
(d) If the execution fails, display an error message and go back to step 1.
(e) If the execution succeeds, go back to step 1 after the command has finished.
I have implemented most of the logic of the shell itself. You will need to implement the
parts that fork a child process, set up pipes and redirects, and execute the command.
The remainder of this document consists of information about the following:
Constants and function definitions.
Process management.
Console input and output.
Pipes and redirects.
Compiling your code.
Running your code.
What to hand in.
Constants and Function Definitions
I have defined the following constants:
MAX_LINE - the maximum number of characters allowed in a single line.
MAX_ARGS - the maximum number of arguments (including the command) that may
be used in a command line.
I have also implemented the following functions:
int main()
1. Displays a prompt.
2. Reads a line from the user.
3. Calls split_cmd to break the command line into the command and its arguments.
4. Calls process_args to process the command line.
5. Goes back to step 1.
int split_cmd(char *cmd, char *args[])
1. Extracts the command from cmd and stores it in args[0].
2. Extracts the arguments from cmd and stores them in args[1], args[2], etc.
3. Stores a value of NULL after the last argument.
4. Returns the number of command line arguments.
void process_args(char *args[], int count)
1. Returns if the line is blank.
2. Exits the program if the user typed exit.
3. Calls exec_cmd if there are no pipes or redirects.
4. Calls exec_cmd_red_out if there is an output redirect.
5. Calls exec_cmd_red_in if there is an input redirect.
6. Calls exectue_cmd_pipe if there is a pipe.
void exec_cmd(char *args[])
1. Creates a child process using fork.
2. The child process executes the command using execvp.
3. If the execution fails the child process displays an error and exits.
4. The parent process calls wait to wait for the child process to complete.
You will need to implement the following functions:
void exec_cmd_red_out(char *args[], char *filename)
1. Create a child process using fork.
2. The child process should do the following:
(a) Close standard output.
(b) Create a new file with the name given by filename.
(c) If the file can not be created, display an error and exit the child process.
(d) Execute the command.
(e) If the execution fails, display an error and exit the child process.
3. The parent process should wait for the child process to complete.
void exec_cmd_red_in(char *args[], char *filename)
1. Create a child process using fork.
2. The child process should do the following:
(a) Close standard input.
(b) Open a file for reading only with the name given by filename.
(c) If the file can not be opened, display an error and exit the child process.
(d) Execute the command.
(e) If the execution fails, display an error and exit the child process.
3. The parent process should wait for the child process to complete.
void exec_cmd_pipe(char *args1[], char *args2[])
1. Create a child process using fork.
2. The child process should do the following:
(a) Create a pipe.
(b) If the pipe creation fails, diplsay an error and exit the child process.
(c) Create a second child process.
(d) The second child process should redirect standard output to write to the pipe
input, execute the command, and display an error and exit if execution fails.
(e) The first child process should redirect standard input to read from the pipe
output, execute the command, and display an error and exit if execution fails.
3. The parent process should wait for the first child process to complete.
Process Management
Process management on UNIX systems relies on the following library functions specified by
the POSIX interface:
pid_t fork()
Creates a new child process identical to the calling parent process.
The return type pid_t is an integer type representing allowable values for a process
ID (PID) or an error value.
Returns 0 to the child process.
Returns the PID of the child process, a positive integer, to the parent process.
Returns a negative value

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago