Go to the previous, next section.

Running several jobs at once

Using job control, you can have several jobs running at once. Only one job can be in the foreground at one time, but any number can be in the background. Background jobs can remain stopped or idle until you return them to the foreground, or they can be made to continue running concurrently in the background.

Background jobs that require terminal input will automatically stop running and wait until you move them to the foreground. You should keep a highly interactive program in the foreground and put slower, compute-bound programs in the background. You can also use job control to switch among several active jobs as they move from computation to interactive dialog.

A common use of job control, especially during program development, is to switch between the editor and the compiler. Suspending and continuing an editor is faster than running it every time. The speed increase is best if you edit large files or you switch back and forth between files frequently. As an added convenience, this puts you back into your file where you last were editing. You may find that you like this feature so much that you edit several files at once, using ctrl-z to suspend one editing session and the fg command to continue another one.

You will need to know how to specify which job to continue. If you don't specify a particular job, fg or bg continues the last job you suspended. This won't always be the job you want. To find out what jobs you have running or suspended, use the jobs command. The number listed next to each job is the number you specify with fg or bg to continue that job in the foreground or background. Consider the following example:

[1]  Stopped  vi myprogram.c
[2]  Stopped  vi myotherprogram.c
[3]  Stopped  vi yetanother.c
[4]  Stopped  cc canonical.c

In this example, the user wants to continue compiling the C program `canonical.c' in the background and resume editing `myotherprogram.c' in the foreground. To find out which jobs he has suspended, he types the jobs command. It lists each suspended job with an identifying number. He puts the compile job (#4) in the background using the bg command, and then he uses the jobs command to verify that it's running. He then brings the edit job (#2) to the foreground using the fg command.

A useful hint: you can use the following procedure to switch back and forth between a compile job and an edit job. Use your editor to write out your changes to a source file, and then type ctrl-z to suspend the editor. Then run the compiler and direct its error messages to a listing file. Then switch between editing your source file and reading the listing file, trying to fix as many errors in your source file as you can after each compile.

Remember, the compiler compiles the disk file, not the copy the editor has in its memory, so be sure to write out your file before you suspend the editor. Similarly, when you go back to editing your file, be sure to continue your suspended editing session. Don't start another editing session every time you want to return to your file! If you're not sure if you suspended or exited the editor, use the jobs command to show your suspended jobs.

Go to the previous, next section.