Multi-Tasking

UNIX allows you run as many programs concurrently as you wish. At any time, you can have one program running in foreground with which you can interact, and any number of others running in background.

Unlike some operating systems (eg MS Windows), UNIX gives each active program its fair slice of processor time whether it be in foreground or background. Processor time is doled out to each program in turn by the operating system. UNIX does not depend on each program obediently returning control every so often to let its neighbours have a go.

Programs are therefore written as simple single-task processes which are run, interrrupted, then continued according to a regular cycle.

Interactive programs like editors are simply parked in a frozen state when in background. This is simply because they are not receiving any stimulus from their only input source, the console keyboard when they are in background mode. Programs which respond to events from sources other than the console carry on running as normal in background mode.

To launch a program in background mode, you type your command with an & at the end:

Changing Mode

If you have started a program in foreground and want to stop it, then switch it to background, or re-start it in foreground later, you type the following:

Ctrl-Z to stop the current foreground program
fg to re-start it in foreground again
bg to re-start it in background mode

Job Control

Programs running in background are referred to as jobs. A single job can be made up of many processes, eg:

To find out what jobs are running in background, type:

jobs
[1] - Stopped (signal) elm
[2] + Stopped edit file1
[3] - Running prog1 file2 file3

The + indicates the job that was most recently-started or stopped.

bg %job restarts the job in background
fg %job restarts the job in foreground (the fg not necessary)
kill %job kills the job completely

For the word job you can substitute one of the following:

Background Programs and the Terminal

A background program cannot read from the keyboard. If it needs to do so, it stops. Its status is shown as follows:

jobs
[1] - Stopped (tty input) elm

Your options in this case are to type:

%% to pull it into foreground
kill %% to get rid of it

Full-screen programs are designed to save the screen when they are stopped and re-draw it when they are re-started.

To prevent or allow a background program overwriting the screen, type:

stty tostop to stop background programs writing to screen
stty -tostop to enable them to write to the screen.


This page's parent within this Web Site. About this Web Site. Its home page. Email its Author.