Menu

(Solved) : Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572594 . . .

ALL INPUT/TEST CASES NEED TO BE HARD CODED IN

Note. You should use the sample program (provided below) as yourbase code to implement this part.

For C/C++ in a Unix Environment

Design and implement a C/C++ program to handle 3 commands inpipe.

Warning: Do not use any”system(…)” function call throughout this assignment.

Your test case is hard-coded in the program.

char *cmd1[] = { “/bin/ls”, “-l”,”/etc”, 0 };

char *cmd2[] = { “/bin/head”, 0 };

char *cmd3[] = { “/bin/wc”, “-l”, 0};

Provide a Makefile file to compile your program.

Your Program Name: a3part2.c

Sample Program.

The following sample program is to run two commands in pipe(cmd1 | cmd2) where the two commands are hard-coded (cmd1 andcmd2), to be used for your base code to handle three commands(hard-coded here).

/* sample shell to handle two commands with pipe: ls | wc -l*/

#include

#include

#include

#include

char *cmd1[] = { “/bin/ls”, “-l”, “/etc”, 0 };

char *cmd2[] = { “/bin/head”, 0 };

char *cmd3[] = { “/bin/wc”, “-l”, 0 };

void run2com();

void run3com();

int main(int argc, char **argv)

{

         int pid;

int status;

         int fd[10];

int N = 2;

         pipe(fd);

         pid =fork();

         if (pid == 0){

    if (N == 2) {

        run2com(fd);

              exit(0);

    } else if (N == 3) {

        run3com(fd);

              exit(0);

    }  

         } else if (pid> 0) {

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

                   fprintf(stderr, “process1 %d exits with %dn”,

                         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);

         }

}

void run3com(int pfd[])

{   

       // your code here

}

Expert Answer


Answer to Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572594 . . .

OR