Question: Problem Statement - Design and implement a basic shell interface in C that supports the execution of other programs and a series of built-in functions,
Problem Statement - Design and implement a basic shell interface in C that supports the execution of other programs and a series of built-in functions, as specified below. The shell should be robust (e.g., it should not crash under any circumstance beyond machine failure).
A: General Shell Structure The shell (command line) is just a program that continually asks for user input, perhaps does something on the users behalf, resets itself, and again asks for user input. Here is an example: while(1) { */ Get user input */ */ Exit? */ */ Do something with input */ }
B: The Prompt At this point, the prompt should indicate that the shell is ready to accept input from the user. Often times, it also shows useful information, such as the name of the user running the shell and the current directory. For now, you just need to implement a simple prompt. The prompt should look like the following: o prompt$ There should be a space after the dollar sign so that the user input does not visually run into the prompt.
C: Command Line Parsing Before the shell can begin executing commands, it needs to extract the command name and the arguments into tokens. It might be nice to store these tokens into an array so that you can then parse each one in order. In our shell, the first token will always be the name of the program we wish to execute, and all remaining tokens (perhaps including the first token) will be arguments to that program. The function strtok() will be helpful to do this, but it is a bit tricky to use. Be sure to look at the class notes. Take note of the following assumptions: No leading whitespace One space separates the command line tokens. No trailing whitespace You can assume that each token is no longer than 80 characters. You can assume that a command will have at most 10 space-separated tokens Make sure that you can successfully print out your array of tokens through different iterations of your shell loop before moving on. If you see garbage in any of your commands or arguments, try using the C library call memset() or bzero() to clear out your input string and token array before and/or after you are done using them. The C library call fgets() can gather user input from the screen and save it into a string (C character array). See the man pages for strtok, fgets, memset, or bzero for more information.
D: Command Execution Once the shell understands what commands to execute it is time to implement the execution of simple commands. Since the execution of another program involves creating another process, you will have to use the fork() system call to create another process. Once you have created the new child process, that process must use the execvp() system call to execute the program. Finally, the parent (shell) process must wait for the child process to complete before releasing the childs resources using the waitpid() system call. However, the execvp() system call may return if there is an error. If it does, your shell should print an error, reset, and prompt for new input. Here is an example: prompt$ lalala -a Error: Command could not be executed prompt$
E: Built-ins Not all commands are actually programs, and your shell must implement two built-in commands. In other words, if you encounter any of these two commands, do not execute them using fork(), exec(), and waitpid(). Instead, your shell should call a subroutine that implements the following functionality.: exit terminates your running shell process and prints 'exit'. prompt$ exit exit (shell exits) cd [PATH] Changes the present working directory. You will need to use the chdir() system call and update the PWD environmental variable with setenv(). prompt$ pwd /user/diesburg/os/project1 prompt$ cd .. prompt$ pwd /user/diesburg/os prompt$ cd project1 prompt$ pwd /usr/diesburg/os/project1 showpid shows the last 5 child process IDs created by your shell. prompt$ showpid 4987 4992 5001 5002 5004
F: Better Prompt Lets make your shell more usable. Modify the prompt so that it displays the current working directory before the $ sign: o /home/xkcd/$ Make the prompt a different color. Optionally, you can make other parts of your shell different colors as well.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
