Objective - Tiny Shell (tish) In this exercise you will write a small shell prog
ID: 3865849 • Letter: O
Question
Objective - Tiny Shell (tish)
In this exercise you will write a small shell program to run different programs in foreground and background mode.
Assignment
Your assignment is to build a program “tish” that will implement the following shell services:
Run programs in foreground and background;
List all processes that currently run in the background.
Kill a process running in the background.
The precise requirements are as following:
Your shell should give a user “tish>>” as a prompt;
There are two types of commands that “tish” should understand:
External commands: the command name is, in fact, the name of an executable file. When an external command is requested this means that the corresponding executable file should be run.
Internal commands: commands that are “built-in” in the “tish” shell. For example, the “bye” command does not correspond to any executable file. Instead, it causes “tish” to execute the exit system call. Below we specify which internal commands you are required to implement.
Execution mode for external commands:
Foreground: a command is given in the following form tish>> <command_name> <list_of_parameters>. In this mode “tish” does not return the prompt until the executable file that corresponds to the command finishes.
Background: a command is given in the same format as in the foreground mode, but the last parameter in the parameter list should be ‘&’. for example:
tish>> emacs &
Example of running external commands:
tish>> emacs my_file.
List of Internal commands you are required to implement:
bye : terminate “tish”. All background processes should be terminated. (Note that the real shell does not do this).
jobs : list of all background jobs in the following format: <pid> <command name>, in the order of creation.
kill :terminate the process corresponding to the specified pid by sending SIGTERM signal.
Explanation / Answer
I've recently come to like setsid. It starts off looking like you're just running something from the terminal but you can disconnect (close the terminal) and it just keeps going.
This is because the command actually forks out and while the input comes through to the current terminal, it's owned by a completely different parent (that remains alive after you close the terminal).
An example:
I'm also quite partial to disown which can be used to separate a process from the current tree. You use it in conjunction with the backgrounding ampersand:
I also just learnt about spawning subshells with parenthesis. This simple method works:
And of course there's nohup as you mentioned. I'm not wild about nohup because it has a tendency to write to ~/nohup.out without me asking it to. If you rely on that, it might be for you.
And for the longer-term processes, there are things like screen and other virtual terminal-muxers that keep sessions alive between connections. These probably don't really apply to you because you just want temporary access to the terminal output, but if you wanted to go back some time later and view the latest terminal activity, screen would probably be your best choice.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.