Chapter 9. Multitasking

Mac OS X 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 Mac OS X, like most Unix systems, also lets 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.

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 also 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 the next section) to check on the processes you have running. Some shells write a status line to your screen when the background process finishes.

In tcsh, 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:

(command1; command2) &

The examples above work on all shells. Mac OS X Unix shells also have a feature we mentioned earlier called job control. You can use suspend character (usually Control-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 &
% pine [email protected]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.66