Multitasking: Background Process and Foreground Process

Linux logo
Linux

As a multitasking operating system, Linux supports the execution of many processes—basically, programs or commands or similar tasks—in the background while you continue to work in the foreground.

Foreground Processes

A foreground process is any command or task you run directly and wait for it to complete. Some foreground processes show some type of user interface that supports ongoing user interaction, whereas others execute a task and "freeze" the computer while it completes that task.

 

From the shell, a foreground process begins by typing a command at the prompt. For example, to see a simple listing of the files in the active directory, type:

$ ls

You'll see the list of files. While the computer is preparing and printing that list, you cannot do anything else from the command prompt.

Background Process

Unlike with a foreground process, the shell does not have to wait for a background process to end before it can run more processes. Within the limit of the amount of memory available, you can enter many background commands one after another. To run a command as a background process, type the command and add a space and an ampersand to the end of the command. For example:

$ command1 &

When you issue a command with the concluding ampersand, the shell will execute the work, but instead of making you wait for the command to finish, you'll immediately be returned to the shell, and you'll see the shell prompt (% for the C Shell, and $ for the Bourne Shell and the Korn Shell) return.

At this point, you can enter another command for either foreground or background process. Background jobs are run at a lower priority to the foreground jobs.

You will see a message on the screen when a background process is finished running.

Switching Between Processes

If a foreground process is taking too much time, stop it by pressing CTRL + Z.

A stopped job still exists, but its execution is suspended. To resume the job, but in the background, type bg to send the stopped job to background execution.

To resume a suspended process in the foreground, type fg and that process will take over the active session.

To see a list of all suspended processes, use the jobs command, or use the top command to show a list of the most CPU-intensive tasks so that you can suspend or stop them to free up system resources.

Shell vs. GUI

Multitasking works differently depending on whether you're working from the shell or a graphical user interface. Linux from the shell supports just one active foreground process per virtual terminal. However, from the practical perspective of the user, a windowed environment (e.g., Linux with a desktop, not from a text-based shell) supports several active windows that effectively serve as multiple simultaneous foreground processes. In practice, Linux behind the scenes adjusts the priority of processes in a GUI to promote system stability and support end-user processing.

Was this page helpful?