Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <readline/re

ID: 3852533 • Letter: #

Question

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
/* Simple example of using gnu readline to get lines of input from a user.
Needs to be linked with -lreadline -lcurses
add_history tells the readline library to add the line to it's
internal history, so that using up-arrow (or ^p) will allows the user
to see/edit previous lines.
*/
int main(int argc, char **argv) {
char *s;
while (s=readline("prompt> ")) {
add_history(s); /* adds the line to the readline history buffer */
free(s); /* clean up! */
}
return(0);
}

Task 5: Pipelined Instructions
Your shell must support pipelined instructions - this means that each command line could include a number of
external commands. Note that you do not need to support pipes with internal commands. For each pipe in the
command line you need to take care of connecting stdout of the left command to stdin of the command
following the "|". For example, if the user types "ls -al | sort", you need to arrange things so that the ls
command is run with stdout directed to a Unix pipe, and that the sort command is run with stdin coming from
that same pipe.
A pipe is basically a buffer held by the kernel, having two file descriptors, one for reading and one for writing.
The pipe maintains the order of the bytes, so that the first byte written to the pipe is the first byte read from
the pipe. The pipe system call creates a pipe and generates two file descriptors. In order to create a pipe
between two commands, your shell will need to create a pipe and then fork a child process for each
command. In one process, you will redirect from stdout into the writing end of the pipe and in the other you
will redirect from stdin to the reading end of the pipe.
For this assignment, you will be required to pipeline two commands together. Pipelining and redirection
should work together. For example, you should support ls -al| sort > output.txt.

Explanation / Answer

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>