اقتباسShell simulation – descriptionIn this exercise you will write a shell simulation program. This shell is used to run different programs in both foreground and background modes, and to control their execution behavior.
Your assignment is to build a program 'sim' that will implement the following shell services:
• Run programs in foreground and background modes.
• Perform a set of internal commands.
• Your shell should prompt the user for commands (use "sim>>" as the prompt).
• There are two types of commands that 'sim' should perform:
o External commands: Any name of an executable file. When an external command is requested it means that the corresponding executable file should be run.
o Internal commands: A set of "built-in" commands in the 'sim' shell (specified below). For example, "bye" is an internal command that causes 'sim' to execute the exit system call.
• Execution mode - External commands:
o Foreground - Given in the following form:
sim>> <command_name> <list_of_parameters>.
For example: sim>> test.exe 1 2
In this mode 'sim' does not return the prompt until the executable file that corresponds to command_name terminates.
o Background - Given in the following form:
sim>> <command_name> <list_of_parameters> &
For example: sim>> test.exe &
In this mode 'sim' returns the prompt immediately and awaits further commands, while the file that corresponds to command_name runs in the background.
Note: sim should recognize the '&' separated or not from the last parameter (i.e. sim>> test.exe& OR sim>> test.exe &).
• Maintenance of Background processes:
o 'sim' should report the termination and termination status of background processes that finished their execution.
o The report should be presented to the user only when a foreground execution has terminated, this means that when you "catch" a termination of a child process you should remember its process ID (PID) and termination status until some foreground event is requested. At the end of that event, add the report of the termination (note: hitting the 'carriage return' / 'Enter' with no command is equivalent to executing an "empty" command in the foreground).
o The report should look as follows: <PID> [DONE] status: <termination status> where <PID> should be replaced by the relevant PID and <termination status> should be replaced by a value (integer) indicating the status.
• List of Internal commands you are required to implement:
o exit: Terminate 'sim'. All background processes should be terminated. (Note that the real shell does not do this).
o jobs: List of all background jobs in the following format: <pid> <command name>, in the order of insertion to the background list.
o kill <pid>: Terminate the process corresponding to the specified pid by sending a SIGTERM signal. This applies only to background processes and not to suspended ones.
o cd <directory_name>: Change the current working directory to <directory_name>. If it doesn't exist, a compatible message should be displayed.
o fg: Passes the most recent process running in back-ground mode to run in fore-ground mode. That means the last process to enter the background list, is extracted from it. If there is no process in the background list, a compatible message should be displayed.
You should use the following system calls (and maybe others):
o _spawn();
o _execvp();
o _cwait();
o signal();
o exit();
o _getpid();
shell simulation
مغلق
هذا الموضوع مغلق.
