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

Design and implement a C/C++ program (a3part3.c [or a3part3.cpp]) to implement a

ID: 3866909 • Letter: D

Question

Design and implement a C/C++ program (a3part3.c [or a3part3.cpp]) to implement a simple shell.

Warning: you should not use any "system" call throughout this assignment (and thereafter).

Task1. One to Three Commands in Pipe

You may use the sample code provided to handle two commands in pipe, to make a shell.

Your shell handles one command, or two or three commands in pipe ("ls | wc -l" or "ls | grep ".c" | sot").

For example, if there are two commands in pipe, the parent gets the command (for example, "ls | wc -l") to recognize that there are two commands piped together. Thus it will have a pipe to be shared, and then fork a child process (to do "ls") which will fork its child process (to do "wc -l") with the pipe shared (so that the output of one process doing "ls" will be input to the input of the other process doing "wc -l"). Meanwhile the parent waits till all the child processes are terminated, and then back to the loop for the next command from the user. Extending the shell for two commands in pipe, your shell should handle up to "three commands in pipe" (for example, "ls | grep ".c" | sort").

Task2. A Command with File Redirection (< input, > output, >> append).

Continuing Task 1, your shell program should handle one command ("ls > out1.txt") or two commands in pipe (for example, "ls | sort > out2.txt").

Task3. Provide a Makefile file to compile your program.

Explanation / Answer

Task1. One to Three Commands in Pipe

------------

int main (int argc, char ** argv) {

int a;

for( a=1; a<argc-1; a++)

{

int pd[2];

pipe(pd);

if (!fork()) {

dup(pd[1], 1); // send output back to parent

execlp(argv[a], argv[a], NULL);

perror("exec");

abort();

}

dup(pd[0], 0);

close(pd[0]);

close(pd[1]);

}

execlp(argv[a], argv[a], NULL);

perror("exec");

abort();

}

Task2. A Command with File Redirection (< input, > output, >> append).

------------

Answer of task2 is more like task1. I am suggesting below change we need to implement file redirection.

int main (int argc, char *argv[])

{

char buffer[BUFFERSIZE]; // define BUFFERSIZE 500

fgets(buffer, BUFFERSIZE , stdin);

printf("Read: %s", buffer);

return 0;

}

Task3. Provide a Makefile file to compile your program.

--------------

As we know for a compile the c/c++ program by using file of type Makefile.

$ make a3part3

cc a3part3.c -o a3part3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote