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: 3858368 • 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.

Hint. You may use dup or dup2 to redirect the file input or output for the given command ("ls") which a child process will execute (with exec).

            if file redirection is specified for input "< filename"

then for child process to do:    

                fid = open(filename, O_RDONLY);

                      close(STD_INPUT);

                      dup(fid);

                      close(fid);

Explanation / Answer

#include <stdlib.h>

#include <stdio.h>

char *cmd1[] = { "/bin/ls", 0 };

char *cmd2[] = { "/bin/wc", "-l", 0 };

void run2com();

int main(int argc, char **argv)

{

       int pid, status;

       int fd[2];

       pipe(fd);

       pid = fork();

       if (pid == 0) {

              run2com(fd);

              exit(0);

       } else if (pid > 0) {

              while ((pid = wait(&status)) != -1)

              fprintf(stderr, "process %d exits with %d ", pid, WEXITSTATUS(status));

       } else {

              perror("fork");

              exit(1);

       }

       exit(0);

}

void run2com(int pfd[])

{

       int pid;

       pid = fork();

       if (pid ==0) {

              dup2(pfd[0], 0);

              close(pfd[1]);

              execvp(cmd2[0], cmd2);

              perror(cmd2[0]);

       } else if (pid > 0) {

              dup2(pfd[1], 1);

              close(pfd[0]);

              execvp(cmd1[0], cmd1);

              perror(cmd1[0]);

       } else {

              perror("fork");

              exit(1);

       }

}

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