Question
The structure of your program should look like this: 1) In a loop, prompt the user for a command and parameters. (Use fgets for this)
The structure of your program should look like this: 1) In a loop, prompt the user for a command and parameters. (Use fgets for this) 2) When a command is given, insert the command and the parameters into an array of C strings. Use strtok to tokenize the string. Your program needs to exam the first word (using strcmp) to figure out whether the command is a run, an exit, or something else. For simplicity, let's assume that users will not enter any command which is longer than 20 words. 3) Fork a new process and have the parent wait for the child to complete. Have the child execute the given command using execvp. 4) Loop until the user enters exit You will use the return value of all the system calls you use (fork, execvp, etc...) and the the global variableerrno to perform error checking and to print out suitable messages in this assignment. You can use the following examples to get you started. The first example is about using fgets and strtok, the second example is about using fork and execvp. Please note that these 2 source examples do not have any error checking code. first example
#include#include #define MAX_LINE_SIZE 255 int main() { char line[MAX_LINE_SIZE]; fgets(line, MAX_LINE_SIZE, stdin); printf("You entered: %s",line); int i = 0; char *words[5]; words[i] = strtok(line," "); while (words[i]!=NULL) { printf("words[%d] is %s ", i, words[i]); i++; words[i] = strtok(NULL," "); } return 0; }
secnd example
#include#include #include #include #include #include #include int main() { int status; pid_t result = fork(); if(result>0){ wait(&status); } else if (result == 0) { printf("CHILD: I am the child "); char *args[3]; args[0] = "ls"; args[1] = "-l"; args[2] = 0; execvp(*args, args); } return 0; }
Graduate students and undergraduate students who are taking CS 410 for master/honor credits will need to implement an extra feature for your shell: (10% of your grade) Your shell should support using; to execute multiple programs with a single command. For example: ./myshell myshel1> run ls l ; date who myshell: started child pid 19410 total 33 rwx--x--x 1 hb117 faculty 9132 Oct 24 11:17 a.out drwx--x--x 10 hb117 faculty 1024 Oct 6 20:28 code 1 hb117 faculty 1706 Oct 7 12:58 duplicatefile.c 1 hb117 faculty 476 Oct 19 13:15 exec.c rwx--x--x 1 hb117 faculty 13331 Oct 26 21:07 myshell -rw - 1 hb117 faculty 1798 Oct 24 11:18 myshel12.c 1 hb117 faculty 2839 Oct 23 21:16 myshell.c drwx-x--x 2 hb117 faculty drwx--xr-x 2 hb117 faculty 96 Sep 23 13:05 p0 96 Oct 4 15:46 pl faculty392 Oct 19 08:54 test.c myshell: started child pid 19411 Wed Sep 20 21:07:56 CDT 2017 myshell: started child pid 19412 jwb128 pts/4 mflll pts/6 tcs113 pts/8 hb117pts/7 myshel1> 2017-9-20 11:11 (:7) 2017-9-20 18:56 (:1) 2017-9-20 20:21 (10.164.71.161) 2017-9-20 20:38 (c-71-201-31-239:S.0) Graduate students and undergraduate students who are taking CS 410 for master/honor credits will need to implement an extra feature for your shell: (10% of your grade) Your shell should support using; to execute multiple programs with a single command. For example: ./myshell myshel1> run ls l ; date who myshell: started child pid 19410 total 33 rwx--x--x 1 hb117 faculty 9132 Oct 24 11:17 a.out drwx--x--x 10 hb117 faculty 1024 Oct 6 20:28 code 1 hb117 faculty 1706 Oct 7 12:58 duplicatefile.c 1 hb117 faculty 476 Oct 19 13:15 exec.c rwx--x--x 1 hb117 faculty 13331 Oct 26 21:07 myshell -rw - 1 hb117 faculty 1798 Oct 24 11:18 myshel12.c 1 hb117 faculty 2839 Oct 23 21:16 myshell.c drwx-x--x 2 hb117 faculty drwx--xr-x 2 hb117 faculty 96 Sep 23 13:05 p0 96 Oct 4 15:46 pl faculty392 Oct 19 08:54 test.c myshell: started child pid 19411 Wed Sep 20 21:07:56 CDT 2017 myshell: started child pid 19412 jwb128 pts/4 mflll pts/6 tcs113 pts/8 hb117pts/7 myshel1> 2017-9-20 11:11 (:7) 2017-9-20 18:56 (:1) 2017-9-20 20:21 (10.164.71.161) 2017-9-20 20:38 (c-71-201-31-239:S.0)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started