Home > Articles > Certification > Other IT

  • Print
  • + Share This
This chapter is from the book

Process Types

When sitting at a terminal and typing in commands, the user is typically executing foreground processes. Commands such as vi are foreground processes—they read input from the keyboard and display output to the terminal. Foreground processes maintain control of the terminal, and the user cannot do anything else in that terminal window until the execution of that command is complete.

Some processes are not interactive and don't need to run in the foreground. These are referred to as background processes or jobs. A background process gets detached from the terminal, freeing up the terminal while it is running. When a user decides to run a process in the background, you must arrange for the process to get its input from another source. In addition, you need to arrange for the process to output to a device other than the terminal, such as a file.

To run a process in the background, enter an & (ampersand) after the command:

# find . -name core -print &

After typing in this command, you're returned to a command prompt. The find command executes in the background. One problem, however, is the standard output is still on your terminal. In other words, as the find command executes, the results still are displayed on your screen, which can become quite annoying. It's best to redirect the output to a file, as follows:

# find . -name core -print  > /tmp/results &

After you put the find command in the background, the system displays two numbers associated with that process—the job number and the process ID number (PID) as follows:

    [1]    14919

You use this job number to control background processes.

The shell maintains a table containing information about processes that are currently in the background. This is referred to as the jobs table. The jobs table is unique to the user, and each user has his own jobs table. Furthermore, the jobs table contains only entries for jobs that are running in your current shell. If you start a new shell, the jobs table for the new shell is empty. Each job in the table is assigned a number that is unique to that user only. In other words, two users can each have a job numbered 1. Don't confuse this job number with a process ID number; remember, process IDs are unique, and no two share the same number. Any jobs that the user has placed in the background are displayed here by typing in the jobs command, as follows:

# jobs

The system responds with this:

[3] +  Running   find / -name bill -print > /tmp/results3 &
[2] -  Running   find / -name junk -print > /tmp/results2 &
[1]    Running   find / -name core -print > /tmp/results1 &

The jobs table contains the following information:

  • A numeric value for each job
  • A + (plus) symbol to designate the current job that user commands will operate on
  • A - (minus) symbol to designate the next job that the user commands will operate on
  • The status of the job
  • The name of the job

Each job in the job table has one of the following states:

  • Running—An active job
  • Stopped—A job that has been suspended
  • Terminated—A job that has been killed
  • Done—A completed job

When the job finishes, the following is displayed on your terminal:

[1] +  Done      find / -name core -print > /tmp/results &

Note the job number of 1 and the status of Done.

If you want to terminate a job, use the kill command followed by a % (percent sign) and then the job number, as follows:

# kill %1

If you do not enter a number following the % sign, the command acts upon the current job entry listed in the jobs table. For this example, you are going to kill job number 1, as follows:

# kill %1

The following message is displayed indicating successful termination:

[1] + Terminated   find / -name core -print > /tmp/results &

You can also bring a job back into the foreground with the fg command. Typing fg with no arguments brings the current job (the job with the + sign next to it in the jobs table) into the foreground. You can also specify the job by typing fg %<job number>, as follows:

# fg %2

This brings job 2 back into the foreground on your terminal.

In a windowing environment such as Java Desktop System, placing jobs in the background is not an issue. Typically, you start a job in one window and open another window to continue working. Therefore, placing jobs into the background has all but disappeared unless you are working on a character-based terminal.

  • + Share This
  • 🔖 Save To Your Account