Systems Programming/ C and

Alice E. Fischer

November, 2017

Alice E. Fischer Systems Programming – Lecture 10. . . 1/8 November, 2017 1 / 8 Outline

1 Jobs and

2 Foreground, Background, and Suspension

3 Detached Processes

Alice E. Fischer Systems Programming – Lecture 10. . . 2/8 November, 2017 2 / 8 Jobs and Job Control A Job is a .

We use the term job to refer to a process that is controlled by a shell. Once started, each job is either in the foreground, running in the background, or suspended. Starting a job the normal way puts it in the foreground: ∼> server2 To start a job in the background, use an ampersand: ∼> server2 & The shell will start the background job, then return immediately to you so that you can start another job that will run concurrently. The output from the background job will still come to the terminal screen. To suspend the foreground process, type ∧Z The suspended job will stop running, but can be restarted. To resume a job after suspension, type ps to find its job number, n then type fg %n or bg %n If you start a job in the background, your shell will control it.

Alice E. Fischer Systems Programming – Lecture 10. . . 3/8 November, 2017 3 / 8 Jobs and Job Control Job Control: server2 and the three clients

To prepare for this experiment, I used a shell to start the server: ∼> server2 I opened a second shell and listed the jobs it was controlling: ∼> jobs The list was empty. Then I used the same shell to start three clients at the same time, all running in the background ∼> clientM localhost & clientJ localhost & client & Typing jobs again for this shell, we see: ∼> jobs [1] + Running clientJ localhost [2] - Running clientM localhost [3] Running client localhost

Alice E. Fischer Systems Programming – Lecture 10. . . 4/8 November, 2017 4 / 8 Foreground, Background, and Suspension Foreground, Background, and Suspension

You can only send keyboard input to a foreground job. You can suspend both foreground and background jobs. To move job [2] into the foreground, use: ∼> fg %2 Control-Z suspends the foreground job and returns control to the shell. After suspending clientM we see: ∼> jobs [1] + Running clientJ localhost [2] - Suspended clientM localhost [3] Running client localhost To resume work on job [2] in the background or in the foreground use: ∼> bg %2 or ∼> fg %2

Alice E. Fischer Systems Programming – Lecture 10. . . 5/8 November, 2017 5 / 8 Foreground, Background, and Suspension Foreground and Background

A “+” in the list of jobs marks the current “default” job. If a fg or bg command is given without a job number, it affects the default job. A “-” in the list of jobs marks the prior-current job. You can manage this job using %- Output to stdout from all of the running jobs will come to the shell unless stdout was redirected when the job was started. If the shell is in the foreground, and a background job has produced some output, hit return to get a command prompt. Suppose one of your running background jobs ends with a fatal error and is waiting for you to release it. You must bring it to the foreground, then release it. It will then exit and be gone from the jobs list.

Alice E. Fischer Systems Programming – Lecture 10. . . 6/8 November, 2017 6 / 8 Detached Processes Detached Processes

We use a shell to run and control processes. Usually, this is a simple matter; but things can get complex. Suppose your shell is running a job in the background, and it is not suspended. When you close the shell window, that terminates the shell and its foreground process. But it does not terminate a running background process. That process lives on as a detached process. If it is a server, new clients can still attach to it. This can create a mess! To see the detached process, use another shell to type ps a To terminate it gracefully, use kill pid or kill -TERM pid To terminate it immediately, use kill -KILL pid .

Alice E. Fischer Systems Programming – Lecture 10. . . 7/8 November, 2017 7 / 8 Detached Processes Recovering from a Mess

Suppose you have made a mess Several jobs are controlled by your shell, Suppose one of them needs to be stopped because it is out of control and not responding to normal signals. You could stop all of the controlled processes by killing the shell. However, this may be a bad idea, because a process running in the background becomes detached when you close its shell. It is like a ! If you only want to stop one job, use the kill command. Kill it this way from the same shell window: ∼> kill -KILL %2 Kill it this way from a different shell window: ∼> ps and note the PID in the left-hand column. ∼> kill -KILL 48283 using that pid.

Alice E. Fischer Systems Programming – Lecture 10. . . 8/8 November, 2017 8 / 8