Answered step by step
Verified Expert Solution
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 Assignment
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
commandargs commandargsoutput of command piped into input of
command
The shell should do the following:
Display a command prompt.
Read a line of text from the user.
Break the command line into individual arguments.
Process the line of text and do one of the following:
a If the line is blank, go back to step
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
e If the execution succeeds, go back to step 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:
MAXLINE the maximum number of characters allowed in a single line.
MAXARGS 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
Displays a prompt.
Reads a line from the user.
Calls splitcmd to break the command line into the command and its arguments.
Calls processargs to process the command line.
Goes back to step
int splitcmdchar cmd char args
Extracts the command from cmd and stores it in args
Extracts the arguments from cmd and stores them in args args etc.
Stores a value of NULL after the last argument.
Returns the number of command line arguments.
void processargschar args int count
Returns if the line is blank.
Exits the program if the user typed exit.
Calls execcmd if there are no pipes or redirects.
Calls execcmdredout if there is an output redirect.
Calls execcmdredin if there is an input redirect.
Calls exectuecmdpipe if there is a pipe.
void execcmdchar args
Creates a child process using fork.
The child process executes the command using execvp.
If the execution fails the child process displays an error and exits.
The parent process calls wait to wait for the child process to complete.
You will need to implement the following functions:
void execcmdredoutchar args char filename
Create a child process using fork.
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.
The parent process should wait for the child process to complete.
void execcmdredinchar args char filename
Create a child process using fork.
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.
The parent process should wait for the child process to complete.
void execcmdpipechar args char args
Create a child process using fork.
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.
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:
pidt fork
Creates a new child process identical to the calling parent process.
The return type pidt is an integer type representing allowable values for a process
ID PID or an error value.
Returns 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started