Task 0: Shell Prompt & Basic Command Support The first feature you need to implement is…
- User
Task 0: Shell Prompt & Basic Command Support
The first feature you need to implement is executing programs that a user enters at the prompt. You need to use the necessary system calls appropriately to implement this feature. Of course, you need to handle a user command as input and pass all arguments correctly for execution. There are two modes of execution for external programs.
Foreground execution: In this mode, you need to execute a program in a child process and wait until the child process terminates. Background execution: In this mode, you need to execute a program in a child process but should not wait for the termination of the child process. & at the end of a user-entered command means that the command should run in the background. There are a few things to keep in mind.
For all error messages, use the macros defined in msgs.h for correct formatting.
Your shell prompt should always show the current working directory. For example, if the user is in the /home/cmpt201 folder, the prompt should be:
/home/cmpt201$ Use the getcwd() function to get the current directory. If getcwd() returns an error, display this error message: shell: unable to get current directory.
When a user enters a command, your shell needs to handle it properly. strtok_r() function might be helpful. Read the manpage for it.
For foreground execution, you need to wait for the exact process you fork and not for any other processes.
Since we do not wait for a background process to terminate, it can become a zombie process (where a child process terminates but the parent process doesn't wait on it). Thus, your shell should still wait on each background child process at some point to clean it up. There are many ways to achieve this, but for this assignment, you can make an additional waitpid() call every time a user enters a command. The following are some hints.
This waitpid() call should wait on any child process since you're cleaning up zombie processes. Use an appropriate pid for that. This waitpid() call should return immediately if no child has exited. Use an appropriate option for that. Consult the man page of waitpid() for the above two points. Remember that there can be multiple background processes launched previously. Thus, a single call will not clean up all of them. You need to use a loop to clean up all zombie processes. For input/output, use read() and write() since we need to handle a signal in the next task. Other functions like printf() do not play nicely with signals. To see the list of functions safe for use with signals, run man signal-safety.
If you encounter an error when reading a user command with read(), you need to print out this error message: shell: unable to read command.
If fork() fails, print out this error message: shell: unable to fork.
If exec fails, print out this error message: shell: unable to execute command.
If wait fails, print out this error message: shell: unable to wait for child.
- Assistant