Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need assistance with designing a mini bash shell that runs a few developed commands (exit, cd, status), as well as sending a SIGINT signal

I need assistance with designing a mini bash shell that runs a few developed commands (exit, cd, status), as well as sending a SIGINT signal with ctrl c and a SIGTSTP signal with z when prompted on the command line. If this assignment seems relatively large to be asking as a question, I would still really appreciate if someone were to give me valid insight into how I should be thinking about starting this shell design. Thank you!

image text in transcribed

image text in transcribed

image text in transcribed

In this assignment you will write your own shell in C, similar to bash. No other languages, including C++, are allowed, though you may use any version of C you like, such as C99. The shell will run command line instructions and return the results similar to other shells you have used, but without many of their fancier features. In this assignment you will write your own shell, called smallsh. This will work like the bash shell you are used to using, prompting for a command line and running commands, but it will not have many of the special features of the bash shell. Your shell will allow for the redirection of standard input and standard output and it will support both foreground and background processes (controllable by the command line and by receiving signals). Your shell will support three built in commands: exit, cd, and status . It will also support comments, which are lines beginning with the # character. Your shell does not need to support any quoting; so arguments with spaces inside them are not possible. We are also not implementing the pipe "|" operator. Your shell must support command lines with a maximum length of 2048 characters, and a maximum of 512 arguments. You do not need to do any error checking on the syntax of the command line. Finally, your shell should allow blank lines and comments. Any line that begins with the #character is a comment line and should be ignored (mid-line comments, such as the C-style //, will not be supported). A blank line (one without any commands) should also do nothing. Your shell should just re-prompt for another command when it receives either a blank line or a comment line. Command Execution You will use fork(), exec(), and waitpid() to execute commands. From a conceptual perspective, consider setting up your shell to run in this manner: let the parent process (your shell) continue running. Whenever a non-built in command is received, have the parent fork() off a child. This child then does any needed input/output redirection before running exec() on the command given. Note that when doing redirection, that after using dup2() to set up the redirection, the redirection symbol and redirection destination/source are NOT passed into the following exec command (i.e., if the command given is ls > junk, then you handle the redirection to "junk" with dup2() and then simply pass ls into exec()). Note that exec() will fail, and return the reason why, if it is told to execute something that it cannot do, like run a program that doesn't exist. In this case, your shell should indicate to the user that a command could not be executed (which you know because exec() returned an error), and set the value retrieved by the built-in status command to 1. Make sure that the child process that has had an exec() call fail terminates itself, or else it often loops back up to the top and tries to become a parent shell. This is easy to spot: if the output of the grading script seems to be repeating itself, then you've likely got a child process that didn't terminate after a failed exec(). Your shell should use the PATH variable to look for non-built in commands, and it should allow shell scripts to be executed. If a command fails because the shell could not find the command to run, then the shell will print an error message and set the exit status to 1. Your shell will support three built-in commands: exit , cd, and status . You do not have to support input/output redirection for these built in commands and they do not have to set any exit status. These three built-in commands are the only ones that your shell will handle itself - all others are simply passed on to a member of the exec() family of functions (which member is up to you) as described above. If the user tries to run one of these built-in commands in the background with the & option, ignore that option and run it in the foreground anyway (i.e. don't display an error, just run the command in the foreground). The exit command exits your shell. It takes no arguments. When this command is run, your shell must kill any other processes or jobs that your shell has started before it terminates itself. The cd command changes the working directory of your shell. By itself - with no arguments - it changes to the directory specified in the HOME environment variable (not to the location where smallsh was executed from, unless your shell executable is located in the HOME directory, in which case these are the same). This command can also take one argument: the path of a directory to change to. Your cd command should support both absolute and relative paths. When smallsh terminates, the original shell it was launched from will still be in its original working directory, despite your use of chdir() in smallsh. Your shell's working directory begins in whatever directory your shell's executible was launched from. The status command prints out either the exit status or the terminating signal of the last foreground process (not both, processes killed by signals do not have exit statuses!) ran by your shell. If this command is run before any foreground command is run, then it should simply return the exit status 0. These three built-in shell commands do not count as foreground processes for the purposes of this built-in command - i.e., status should ignore built-in commands. In this assignment you will write your own shell in C, similar to bash. No other languages, including C++, are allowed, though you may use any version of C you like, such as C99. The shell will run command line instructions and return the results similar to other shells you have used, but without many of their fancier features. In this assignment you will write your own shell, called smallsh. This will work like the bash shell you are used to using, prompting for a command line and running commands, but it will not have many of the special features of the bash shell. Your shell will allow for the redirection of standard input and standard output and it will support both foreground and background processes (controllable by the command line and by receiving signals). Your shell will support three built in commands: exit, cd, and status . It will also support comments, which are lines beginning with the # character. Your shell does not need to support any quoting; so arguments with spaces inside them are not possible. We are also not implementing the pipe "|" operator. Your shell must support command lines with a maximum length of 2048 characters, and a maximum of 512 arguments. You do not need to do any error checking on the syntax of the command line. Finally, your shell should allow blank lines and comments. Any line that begins with the #character is a comment line and should be ignored (mid-line comments, such as the C-style //, will not be supported). A blank line (one without any commands) should also do nothing. Your shell should just re-prompt for another command when it receives either a blank line or a comment line. Command Execution You will use fork(), exec(), and waitpid() to execute commands. From a conceptual perspective, consider setting up your shell to run in this manner: let the parent process (your shell) continue running. Whenever a non-built in command is received, have the parent fork() off a child. This child then does any needed input/output redirection before running exec() on the command given. Note that when doing redirection, that after using dup2() to set up the redirection, the redirection symbol and redirection destination/source are NOT passed into the following exec command (i.e., if the command given is ls > junk, then you handle the redirection to "junk" with dup2() and then simply pass ls into exec()). Note that exec() will fail, and return the reason why, if it is told to execute something that it cannot do, like run a program that doesn't exist. In this case, your shell should indicate to the user that a command could not be executed (which you know because exec() returned an error), and set the value retrieved by the built-in status command to 1. Make sure that the child process that has had an exec() call fail terminates itself, or else it often loops back up to the top and tries to become a parent shell. This is easy to spot: if the output of the grading script seems to be repeating itself, then you've likely got a child process that didn't terminate after a failed exec(). Your shell should use the PATH variable to look for non-built in commands, and it should allow shell scripts to be executed. If a command fails because the shell could not find the command to run, then the shell will print an error message and set the exit status to 1. Your shell will support three built-in commands: exit , cd, and status . You do not have to support input/output redirection for these built in commands and they do not have to set any exit status. These three built-in commands are the only ones that your shell will handle itself - all others are simply passed on to a member of the exec() family of functions (which member is up to you) as described above. If the user tries to run one of these built-in commands in the background with the & option, ignore that option and run it in the foreground anyway (i.e. don't display an error, just run the command in the foreground). The exit command exits your shell. It takes no arguments. When this command is run, your shell must kill any other processes or jobs that your shell has started before it terminates itself. The cd command changes the working directory of your shell. By itself - with no arguments - it changes to the directory specified in the HOME environment variable (not to the location where smallsh was executed from, unless your shell executable is located in the HOME directory, in which case these are the same). This command can also take one argument: the path of a directory to change to. Your cd command should support both absolute and relative paths. When smallsh terminates, the original shell it was launched from will still be in its original working directory, despite your use of chdir() in smallsh. Your shell's working directory begins in whatever directory your shell's executible was launched from. The status command prints out either the exit status or the terminating signal of the last foreground process (not both, processes killed by signals do not have exit statuses!) ran by your shell. If this command is run before any foreground command is run, then it should simply return the exit status 0. These three built-in shell commands do not count as foreground processes for the purposes of this built-in command - i.e., status should ignore built-in commands

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

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions