Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 variable errno 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. image text in transcribedfirst 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; } 
.:./myshell myshell> run 1s myshell: started child pid 7973 myshell myshell.c myshell> start ls myshell: start is not a valid command myshell> run ls -1 myshell: started child pid 7980 186346-rw1 hb117 faculty 4 186336 -rw--1 hb117 faculty 368 Oct 20 15:10 myshell.h myshell>run date myshell: started child pid 7985 Tue Sep 20 16:21:32 CDT 2017 myshell> myshell> run azzxcd myshell: started child pid 8000 myshell: No such file or directory azzxcd myshell> exit 448 Oct 20 15:08 myshell .:./myshell myshell> run 1s myshell: started child pid 7973 myshell myshell.c myshell> start ls myshell: start is not a valid command myshell> run ls -1 myshell: started child pid 7980 186346-rw1 hb117 faculty 4 186336 -rw--1 hb117 faculty 368 Oct 20 15:10 myshell.h myshell>run date myshell: started child pid 7985 Tue Sep 20 16:21:32 CDT 2017 myshell> myshell> run azzxcd myshell: started child pid 8000 myshell: No such file or directory azzxcd myshell> exit 448 Oct 20 15:08 myshell

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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions