Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help in Unix bash you should not use any system call throughout this assignment. Design and implement a C/C++ program (a3part3.c) Your program will run

Help in Unix bash

you should not use any "system" call throughout this assignment.

Design and implement a C/C++ program (a3part3.c)

Your program will run in a loop: (1) get the input from the user, (2) parse it, (3) for each command in pipe, set it for pipe, set each file-redirection specified for the command, fork a process to run it (via exec), and (4) back to the loop for next command.

If the command is "exit", then the program terminates.

Task1. Three or More 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, for example,

ls /etc | grep "passwd" | sort

ls /etc | grep "*.c" | sort > a3out.txt

ls | grep ".c" | sort | wc l > a3out.txt

or more commands in pipe

For example, if there are three commands in pipe, the parent gets the command (for example, "ls /etc | grep passwd | sort") to recognize that there are three commands piped together. Thus the parent forks a child (to do sort the last command). The child will create a pipe to be shared, and then fork a child process (to do grep the command in the middle of the command-pipe) which will fork its child process (to do ls the first command) with the pipe shared (so that the output of one process doing "ls" will be the input to the process doing grep command). Meanwhile the parent waits for the child processes (doing the last command in the pipe sequence) to be terminated, and then back to the loop for the next command from the user.

You will have a main and a function named runNcommands to receive the input string (for example, " ls /etc | grep passwd | sort ". The function will parse the commands to be run in pipe.

Do not enumerate a code segment for each case (case-by-case, that is, to write a code segment for 2 commands, a code segment for 3 commands, and for 4 commands, to handle max 4 commands) but to make your code generalized to handle any number of commands in pipe. You should have a for-loop to loop n times for n-commands in pipe to generate (fork) n-processes where each process creates a pipe to be shared with its child process.

Program run and output with 3 test cases.

Here is some sample code

/* a sample code for pipe and output redirection */

int main(int argc, char **argv)

{

int pid, status;

int fdout; /* new file descriptor for output */

char *command[] = { "ls", "-l", ">", "output.txt" };

if ((fdout = open(command[3], O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) {

perror(command[3]); /* open failed */

exit(1);

}

printf("writing output of the command %s to \"%s\" ", command[0], command[3]);

dup2(fdout, 1);

execlp(command[0], command[0], command[1], (char *)0);

perror(command[0]); /* execvp failed */

exit(1);

}

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

#include

#include

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is telephone etiquette?

Answered: 1 week ago