Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Simple Shell with Pipes. please use C language for the question and donot use chatGpt please. Your shell should read lines of user input, then
Simple Shell with Pipes. please use C language for the question and donot use chatGpt please. Your shell should read lines of user input, then parse and execute the commands by forkingcreating new processes. For each command, your shell should call fork followed by execvp Following each command, your shell should wait for its child process to complete, and then print the child PID and the return result from the child process. The user should be able to specify the command to execute by giving a path to the executable file egbinls or by using path expansion to locate the executable file ie searching each directory in the PATH environment variableNote that the execvp function perform this processing automatically; you do not need to program it yourself.
If your shell encounters an error while reading a line of input it should report the error and exit. If your shell encounters EOF while reading a line of input, it should exit gracefully without reporting an error. Ensure that you do not overflow a byte buffer when fetching the line of input functions that do not accept the size of your buffer are not able to prevent overflows whereas functions that do accept a size generally do; be sure to check the manpage of any function you use carefully You do not need to report an error if the user's input line is larger than the byte buffer; just use the truncated input as the command.
You can test the EOF by running make run commands.txt or from the prompt enter CTRLD
Before your shell forks a new process to call execvp it should parse the input string and separate it into a collection of substrings representing the executable file and any commandline arguments. If the user entered an empty line, report an error and fetch a new line of input. Your code must handle at least four commandline arguments in addition to the name of the executable file itself for each command.
You should store pointers to the substrings in an array similar to the "argv" array passed to main and pass this array of arguments to execvp Note that the number of commandline arguments is variable; this is indicated in the array by including a NULL pointer in the array after the last substring. This means that if the user specifies N substrings, your array must hold N pointers where the last pointer is NULL. If the user enters the exitcommand, your shell should terminate returning to the regular shell
Note: your shell does not need to support cd change directory
Piping You shell must also support piping. An example of using pipes in the Linux shell would be cat myfile.txt wc l which would copy the file myfile.txt to standard output which is redirected as the standard input to wc wordcount which will then display how many lines were in the file myfile.txt The pipe character seperates different commands and the output stdout of the program on the left becomes the input stdin for the program on the right. There is no limit to the number of commands that can be piped together. Just limited by the command line length specified above
Hint: get your shell working without pipes first, then go back and add pipes. While this may require a little more work to change your code to support pipes, it ensures you do the rest of the shell correctly so you are not trying to debug multiple things at once. Some functions you will need include pipe and dup
Your program must also accept a command line argument which is the prefix prompt. If no value is specified use as the prompt.
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