home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the Unix Operating SystemSearch this book

Chapter 7. Multitasking

Unix can do many jobs at once, dividing the processor's time between the tasks so quickly that it looks as if everything is running at the same time. This is called multitasking.

With a window system, you can have many applications running at the same time, with many windows open. But most Unix systems also let you run more than one program inside the same terminal. This is called job control. It gives some of the benefits of window systems to users who don't have windows. But, even if you're using a window system, you may want to use job control to do several things inside the same terminal window. For instance, you may prefer to do most of your work from one terminal window, instead of covering your desktop with multiple windows.

Why else would you want job control? Suppose you're running a program that will take a long time to process. On a single-task operating system such as MS-DOS, you would enter the command and wait for the system prompt to return, telling you that you could enter a new command. In Unix, however, you can enter new commands in the "foreground" while one or more programs are still running in the "background."

When you enter a command as a background process, the shell prompt reappears immediately so that you can enter a new command. The original program will still run in the background, but you can use the system to do other things during that time. Depending on your system and your shell, you may even be able to log off and let the background process run to completion.

7.1. Running a Command in the Background

Running a program as a background process is most often done to free a terminal when you know the program will take a long time to run. It's used whenever you want to launch a new window program from an existing terminal window--so that you can keep working in the existing terminal, as well as in the new window.

To run a program in the background, add the "&" character at the end of the command line before you press the RETURN key. The shell then assigns and displays a process ID number for the program:

$ sort bigfile > bigfile.sort &
[1] 29890
$

(Sorting is a good example because it can take a while to sort huge files, so users often do it in the background.)

The process ID (PID) for this program is 29890. The PID is useful when you want to check the status of a background process, or if you need to, cancel it. You don't need to remember the PID, because there are Unix commands (explained in later sections of this chapter) to check on the processes you have running. Some shells write a status line to your screen when the background process finishes.

Here's another example. If you're using a terminal window, and you'd like to open another terminal window, you can probably click a button or choose a menu item to do that. But, if you occasionally want to specify command-line options for that new window, it's much easier to type the options on a command line in an existing window. (Most menus and buttons don't give you the flexibility to choose options each time you open a new window.) For instance, by default, an xterm window saves 64 lines of your previous work in its "scrollback buffer." If you'll be doing a lot of work that you'll want to review with the scrollbar, you might want to open a new window with a 2000-line scrollback buffer. You could enter the following command in an existing xterm window:

$ xterm -sl 2000 &
[1] 19283

A new xterm window should pop open--where you'll be able to scroll almost forever.

In the C shell, you can put an entire sequence of commands separated by semicolons (;) into the background by putting an ampersand at the end of the entire command line. In other shells, enclose the command sequence in parentheses before adding the ampersand. For instance, you might want to sort a file, then print it after sort finishes. The syntax that works on all shells is:

(command1; command2) &

The examples above work on all shells. On many systems, the shells have the feature we mentioned earlier called job control. You can use the suspend character (usually CTRL-Z) to suspend a program running in the foreground. The program pauses and you get a new shell prompt. You can then do anything else you like, including putting the suspended program into the background using the bg command. The fg command brings a suspended or background process to the foreground.

For example, you might start sort running on a big file, and, after a minute, want to send email. Stop sort, then put it in the background. The shell prints a message, then another shell prompt. Send mail while sort runs.

$ sort hugefile1 hugefile2 > sorted
	>time goes by<
CTRL-Z Stopped
$ bg
[1]    sort hugefile1 hugefile2 > sorted &
$ mail eduardo@nacional.cl
	...



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.