Section 5.11. Background Processing


[Page 180 (continued)]

5.11. Background Processing

If you follow a simple command, pipeline, sequence of pipelines, or group of commands by the & metacharacter, a subshell is created to execute the commands as a background process. The background process runs concurrently with the parent shell, and does not take control of the keyboard. Background processing is therefore very useful for performing several tasks simultaneously, as long as the background tasks do not require keyboard input. In windowed environments, it's more common to run each command within its own window than to run many commands in one window using the background facility. When a background process is created, the shell displays some information that may be used to control the process at a later stage. The exact format of this information is shell-specific.

In the following example, I executed a find command in the foreground to locate the file called "a.c". This command took quite a while to execute, so I decided to run the next find command in the background. The shell displayed the background process's unique process ID number and then immediately gave me another prompt, allowing me to continue my work. Note that the output of the background process continued to be displayed at my terminal, which was inconvenient. In the next few sections, I'll show you how you can use the process ID number to control the background process, and how to prevent background processes from messing up your terminal.

$ find . -name a.c -print       ...search for "a.c". ./wild/a.c ./reverse/tmp/a.c $ find . -name b.c -print &     ...search in the background. 27174                           ...process ID number $ date                          ...run "date" in the foreground. ./wild/b.c                      ...output from background "find". Wed Feb  2 18:10:42 CST 2005       ...output from date. $ ./reverse/tmp/b.c                ...more from background "find"        ...came after we got the shell prompt so we don't        ...get another one. 



[Page 181]

You may specify several background commands on a single line by separating each command by an ampersand:

$ date & pwd &             ...create two background processes. 27310                                   ...process ID of "date". 27311                                   ...process ID of "pwd". /home/glass                             ...output from "date". $ Wed Feb  2 18:37:22 CST 2005          ...output from "pwd". $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net