Question: Design and implement a C/C++ program to implement a simple shell. Sample 1. The following sample program implements a simple shell to run one command.
Design and implement a C/C++ program to implement a simple shell.
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); } } |
Task1.
A simple shell to handle one command or two Commands in Pipe
Name your Program: a3p1shell1.c
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").
Task2.
A simple shell to process one command, two commands in pipe, or three Commands in Pipe
Name your Program to be a3p1shell2.c
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).
Task3.
Many Commands in Pipe
Your Program Name: a3p1shell3.c
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). It will repeat in for-loop for all the pairs of commands are processed in pipe.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
