Question

In: Computer Science

Using C Write a program that will serve as a simple shell. This shell will execute...

Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message. The shell should also provide a "quit" command that will cause the shell to terminate.

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

-------------shell.c---------------

#include <stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>

void parseCommand(char *line, char **cmd) //splits the line into words and stores in cmd array
{
   while (*line != '\0') /* if not the end of line ....... */
   {   
       while (*line == ' ' || *line == '\t' || *line == '\n')
           *line++ = '\0'; /* replace white spaces with \0 */
       *cmd++ = line; /* save the argument position */
       while (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n')
           line++; //skip till end of next argument
   }
   *cmd = '\0'; /* mark the end of argument list */
}


void executeCommand(char **argv) //calls execvp and executes the command
{
   int r = execvp(*argv, argv);
   if(r < 0)
   {  
       printf("*** ERROR: Invalid command, exec failed\n");
       exit(1);
   }
}


int main (int argc, char **argv)
{
   char line[1024]; /* the input line */
   char *cmd[64]; /*to store the command line argument */
   while (1)
   {
       int childPid;
       int status;
       printf("\n$myshell-> "); /* print a prompt*/
       fgets(line, sizeof(line), stdin);
       line[strlen(line)-1] = '\0';
       parseCommand(line, cmd); /* parse the line and store the words in cmd array */

       if (strcmp(cmd[0], "quit") == 0) /* is it a "quit"? */
           break; /* exit if it is */

       childPid = fork(); //creates child

       if (childPid == 0)
       {
           executeCommand(cmd); //calls execvp and executes the command
       }
       else
       {
           waitpid (childPid, &status, 0); //waits for the child
       }
   }  
}

--------------Screenshots--------------

--------------------Output-----------------------

--------------------------------------------------------------------------------------

Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS system. I....
Use any C program without using PTHREADS calculate time taken to execute program. Write same program...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program of question 1 using PTHREADS. Calculate time taken to execute program.(1 mark) Identify data races in above program and explain why this situation occurred with an example (1 mark) Rewrite the code to avoid data races should use any of the THREE techniques.(1.5 marks) please I need the c code.. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2,...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS...
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT