Operating System代写:CSE320 SFISH Shell Part4

代写一个类似Shell功能的命令行工具,第四部分是Job control相关的逻辑实现。

Part IV: Job Control Support

Another useful feature of a shell is the ability to pause and resume the execution of a job. In the case of a long-running program, it is helpful to be able to place it in the “background”, allowing the user to issue more commands interactively while the long-running program continues execution.

Your shell should identify the special character & , which means that the entered command should be executed in the background, returning a shell prompt immediately. You will create the new builtin command jobs (specification provided below), which lists all jobs running in the background, displaying their name, PID, job number, etc., with their status (running or suspended). The shell should also print the exit status code of background jobs that have completed.

Each job can be identified by either its process id (PID) or a job ID (JID). A JID is a positive integer assigned by sfish e.g. the index of the job in the jobs list. JIDs should be denoted on the command line by the prefix % . For example, %5 denotes a job with a JID of 5 and 5 denotes a job with a PID of 5.

In addition to jobs, we will need to add a few more builtin commands to make job control useful (specification provided below). The command fg (ex: fg 3 ) should make the specified job number in your list go to the foreground (and resume execution if it is not currently running/stopped). The command bg (ex: bg 2 ) should cause the specified suspended program to run in the background. fg and bg can be called with either the JID or PID of a process.

We also need to be able to forcibly terminate a program. In bash, if you press Ctrl+C the foreground program(s) are killed. Your shell will kill the foreground program(s) using Ctrl+C . You must send SIGINT to terminate the program. Pressing Ctrl+Z causes the foreground program(s) to be suspended and added to the list of jobs. These jobs can later be resumed by calling fg or bg on the suspended job. (i.e., a SIGTSTP signal is sent to suspend the process and a SIGCONT signal is sent to resume a suspended process). Your linux shell performs these same functions. You should test them in your linux shell to understand how they function.

You can find a list of useful signals for job control with their description here. You can also find a
comprehensive list of signals and their categories here.

The following features must be implemented:

  • Add support for job control, including the & character
  • Implement following builtin commands:
    • jobs
      • List all jobs running in the background, their name, PID, job number. just like bash with their status (running or suspended). It should also print the exit status code of background jobs that just ended.
    • fg
      • This builtin command should make the specified job number in your list go to the foreground (and send a signal to resume its execution if it is not currentlyrunning/stopped).
      • Usage: fg PID|JID
    • bg
      • This builtin command should send a signal to the specified job in your job list to resume its execution in the background.
      • Usage: bg PID|JID
    • kill
      • This builtin command should send the specified signal to the process specified by PID. If no signal is specified you must send SIGTERM as default. The signal is denoted by its number ranging from 1 to 31 (inclusive).
      • Usage: kill [signal] PID|JID
    • disown
      • This builtin command should delete the specified job from the job list. If no PID is specified you must delete all jobs from the job list.
      • Note: This builtin’s functionality differs from bash.
      • Usage: disown [PID|JID]
    • Add support for the following hotkeys:
      • Ctrl+C : Kills foreground program(s) using SIGINT.
      • Ctrl+Z : Suspends foreground program(s) using SIGTSTP.

Be sure to run plenty of tests, including handling of piped applications or scripts with bg and fg .

sfish-user@machine:[/tmp]> ./infiniteloop &
[1] 9723 ./infiniteloop &
sfish-user@machine:[/tmp]> ./infiniteloop &
[2] 9724 ./infiniteloop &
sfish-user@machine:[/tmp]> jobs
[1] 9723 Running ./infiniteloop &
[2] 9724 Running ./infiniteloop &
sfish-user@machine:[/tmp]> bg %3
%3: No such job
sfish-user@machine:[/tmp]> fg %1
[1] 9723 stopped by signal 20
sfish-user@machine:[/tmp]> jobs
[1] 9723 Stopped ./infiniteloop &
[2] 9724 Running ./infiniteloop &
sfish-user@machine:[/tmp]> bg %1
[1] 9723 ./infiniteloop &
sfish-user@machine:[/tmp]> jobs
[1] 9723 Running ./infiniteloop &
[2] 9724 Running ./infiniteloop &
sfish-user@machine:[/tmp]> exit
$