5386

Design and implement a C/C++ program to implement a simple shell.

Document Preview:

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: %sn”, strerror(errno)); exit(1); } while (getinput(buf, sizeof(buf))) { buf[strlen(buf) – 1] = ‘ ‘; if((pid=fork()) == -1) { fprintf(stderr, “shell: can’t fork: %sn”, strerror(errno)); continue; } else if (pid == 0) { /* child */ execlp(buf, buf, (char *)0); fprintf(stderr, “shell: couldn’t exec %s: %sn”, buf, strerror(errno)); exit(EX_DATAERR); } if ((pid=waitpid(pid, &status, 0)) 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 %dn”, pid, WEXITSTATUS(status)); } else…

Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now

Attachments:

Assignment.docx