Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is Unix. PART1 Design and implement a C/C++ program to implement a simple shell. For a shell implementation, review APUE Ch9.9. Sample 1. The

This is Unix.

PART1

Design and implement a C/C++ program to implement a simple shell. For a shell implementation, review APUE Ch9.9.

Sample 1. The following sample program implements a simple shell to run one command.

/*

* a3p1shell1.c

* simplest shell with a SIGINT signal handler. type EOF (^D) to exit.

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

static void sig_int(int);

char *getinput(char *buffer, size_t buflen)

{

printf("$$ ");

return fgets(buffer, buflen, stdin);

}

int main(int argc, char **argv)

{

char buf[1024];

pid_t pid;

int status;

if (signal(SIGINT, sig_int) == SIG_ERR) {

fprintf(stderr, "signal error: %s ", strerror(errno));

exit(1);

}

while (getinput(buf, sizeof(buf))) {

buf[strlen(buf) - 1] = '\0';

if((pid=fork()) == -1) {

fprintf(stderr, "shell: can't fork: %s ",

strerror(errno));

continue;

} else if (pid == 0) {

/* child */

execlp(buf, buf, (char *)0);

fprintf(stderr, "shell: couldn't exec %s: %s ", buf,

strerror(errno));

exit(EX_DATAERR);

}

if ((pid=waitpid(pid, &status, 0)) < 0)

fprintf(stderr, "shell: waitpid error: %s ",

strerror(errno));

}

exit(EX_OK);

}

void sig_int(int signo)

{

printf(" Caught SIGINT! ");

}

Sample 2. The following sample program is to run two commands in pipe (cmd1 | cmd2) where the two commands are hard-coded (cmd1 and cmd2).

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

}

}

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

First, create a folder (a3part1) for this part, to have all the programs and results. You may make a zip file out of this folder to be summitted, attached together with this word document.

Task1.

A simple shell to handle one command or two Commands in Pipe

You may use the sample code provided to handle one or two commands in pipe.

Your shell will loop to do the following work: (1) read input from the standard input (keyboard), (2) parse input, (3) print the parsed result to the standard output (console), (4) run the commands in pipe, and (4) back to the loop.

Each input is one command (e.g., ls) or two commands in pipe (e.g., ls | wc -l or ls | grep ".c").

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 process waits till all the child processes are terminated, and then back to the loop for the next command from the user.

Your Program Name: a3p1shell1.c

Task2.

A simple shell to process one command, two commands in pipe, or three Commands in Pipe

You will extend your shell program (in Task1) to handle one, two, or three commands in pipe.

Each input is one command (e.g., ls) or two commands in pipe (e.g., ls | wc -l or ls | grep ".c") or three commands (ls /etc | sort | head, or cat /etc/passwd | grep "sh" | sort).

Name your Program to be a3p1shell2.c

Task3.

Many Commands in Pipe

You will extend your shell program (in Task1 or Task2) to handle many commands in pipe.

Name the function to handle one or two commands to be runNcom.

This function is a generalization to process N commands in pipe. The program is in a loop (to handle many commands in pipe). That is, the function is in a for-loop to fork a process to handle the second, to create a pipe for each pair of commands in a pipe, and to fork a child process to handle the first command in pipe. It will repeat in for-loop for all the pairs of commands are processed in pipe.

For example, an example of four commands in pipe would be: ls /etc | grep .conf | sort | head

Your Program Name: a3p1shell3.c

Provide a Makefile file to compile your program.

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

Recommended Textbook for

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

When a database is split, which objects go into the back end?

Answered: 1 week ago

Question

7-2 What are the different types of networks?

Answered: 1 week ago

Question

=+employee to take on the international assignment?

Answered: 1 week ago