Recent Changes - Search:
 Welcome to the Cisco Academy for Vision Impaired Linux Wiki.

PmWiki

edit SideBar

ProcessesAndJobs

Multitasking Basics

Linux is both multi-user and multi-tasking. The multi-user part is easy to understand: multiple users, each with their own account can log in to the system to work. Each user has his own home directory and can create his own processes as needed.

Multi-tasking means that the system can run more than one process at a time. And in fact the multiple users can each be running multiple tasks. A job, task or process are all terms for the same thing.

In graphical environments, multiple windows are open to show the user interface for each process. In one job, the user might be composing a document. In a second process, he might be calculating in a spreadsheet. A third process might be running a long program and a fourth task might be downloading some files over the internet. The user switches between each of these task windows to interact with the running process, or simply to see what's happening, for example, is the download finished yet.

Linux, with its Unix heritage is more suited to the multi-user multi-tasking user paradigm. With its roots in the earlytime-sharing operating systems of the 1970s, Linux is set up to have multiple character-based user accounts. Each user can run his own graphical environment on top of the console-based environment as well. In this course, we focus only on working in the character, or console-based environment.

In Linux, under its graphical environment, the task switching resembles working in Windows or on a Mac. It is also possible in the text-based (console) environment to use screen or multiple consoles to move between different windows running different tasks. In screen, you'd type CTRL-A C to create a new window; or CTRL-A N to switch to the next window. Screen is covered more fully on another wiki page.

In multiple consoles, which can only be accessed if you are physically connected to the hardware running Linux, using its keyboard, you'd type Alt-F1 to go to console 1, or Alt-F8 to go to console 8. Hold the left alt and press a function key on the hardware keyboard to go to its corresponding console. This again will not work in a terminal session connected via ssh or a serial port.

But at the command line prompt, interacting with the shell, multi-tasking is handled a bit differently. Multiple jobs can still run, but with only one terminal at his disposal the user needs a way to interact with the different jobs.

So if a command is typed, followed by the ampersand (&), the shell begins running that task in the background, while immediately displaying the prompt to tell the user it is ready in the foreground for another command. The task that's been put in to the background has a process ID. This is a new number assigned to each newly-started job, and it will remain active until the process is killed or the system is restarted. That process ID is displayed by the shell when the task is moved from the foreground, but in fact the process ID was assigned as soon as the process was created. A job number is also assigned, also created when the job was started. But the job numbers begin at 1 and increment during your current shell session only. If you open a new shell, you start with job number 1 again.

A user can continue to type commands terminated by ampersands to put all of them in the background. Though only one task, usually the shell will be running in the forground, multiple tasks can live in the background.

The problem withis approach is that if a process has lots of data to display, its output will still be printed on the terminal, interfering with output from other processes. It is best to background a process after either giving it a parameter like -q for quiet, to tell it to avoid displaying output, or to redirect its output. For example, if you wanted to search your system for a file using find, you could redirect the results of find to a file called results.

Many programs also let you suspend them when they are running, effectively moving them in to the background. Typically you type CTRL-Z to do this. A job in the background can either be stopped, which is the same thing as suspended, or it can be running.

The command to bring a job back to the foreground is fg. This brings the last thing you sent to the background back in to the foreground.

When you log in to Linux, you are typically running a shell -- sometimes called a command interpreter -- which gives you a prompt and accepts command lines. From the shell prompt, you type in a command, followed by parameters, sometimes called arguments. You press the ENTER key, sometimes called RETURN, and the command executes.

The command you type is the name of a program. That program starts out by running in the foreground. It is assigned both a process ID and a job number.

There are several ways to put that command in the background.

First, you can simply append an ampersand (&) to the end of the command line. This forces the command in to the background and immediately displays another shell prompt, telling you that the shell is the active foreground program. As the program backgrounds itself, the shell also displays its process ID and job number for reference.

Second, you can wait until the program is running, and type an exclamation point (!) or a control-Z keystroke to suspend the program. What you type will depend on the running program's user interface. When the program is suspended in this way, it is frozen and no longer running. If you background a program with the ampersand, it does continue to run. A suspended program has its state frozen.

A user who runs programs in the background with ampersand usually works with programs that either send their output to files by default or that output is redirected. A program that prints data to the screen will continue to do so when run in the background, so it is impractical to do this using a program that sends lots of output to the screen. If you suspend a program like an editor however, it stops running, so nothing from its user interface is being displayed.

There are several ways to examine the processes, sometimes called tasks that are suspended or running. Sighted users often work with the command top, which updates continuously. This program is impractical for screen access users.

A better way for us to examine the processes is the ps command, which has a variety of parameters. ps displays the Process ID, a number which identifies the process to the operating system, the user running that process, and the amount of memory it is consuming. Ps can also display other details as documented in its man page.

To see a list of tasks started during your current session, with your current shell, you can type jobs. Jobs, unlike PS, is a command that displays job numbers, which are assigned by the shell to each task you've started since you logged in. Job numbers are specific to the particular running shell. The jobs command lets you suspend a job as well as shows you wich jobs are running and which are suspended. The terminology for a suspended job is "stopped" so a "stopped job" is actually a suspended job whose state is frozen.

As mentioned above, the command to put a task in to the background is bg and the command to put a task in to the foreground is fg. Typing fg 3 puts the process with job number 3 in to the foreground. Typing bg by itself puts the current foreground job in the background. Typing fg by itself brings the last job that was placed in the background back to the foreground. And job numbers continue to increment until your session with a particular shell is closed.

Though process IDS increment, they increment from when the system was last started. Job numbers being specific to the running shell are easier to track programatically. So if the user wants to bring to the foreground the fourth job he started, he can type fg 4.

Only one job runs in the foreground at a time, but multiple jobs can run in the background.

To end a task, the command is kill. You follow kill with the process ID. Kill can also send signals to a process. Signals tell the process to do a variety of things based on external conditions, for example, suspend.

killall -list shows a list of all "signals" that the shell can send to a process. A signal can tell a process to suspend itself or to terminate. A signal can simply tell a process to update its screen output.

Examples of signals include:

  • Hang up the modem.
  • Terminate -- prepare to be ended by tidying up memory, flushing buffers, closing files, etc.
  • Terminate meaning the process will be killed now.
    • Break -- CTRL-C was pressed signifying the user wants to interupt.

You can experiment with processes and job control using the following simple script:
while ((1))
echo moo
sleep 10
done
name the script moo.sh. use chmod to make it executable
chmod 755 moo.sh
and try running it in the background with./moo.sh &
while it runs, try suspending it with ctrl-z. Try using fg and bg to move it to the foreground and background respectively. Try using the ps and jobs command to see the status of tasks. Try running multiple instances of moo.sh. Try running it under screen. Try redirecting its output:./moo.sh > moo.txt

Try killing one of your moo processes. Try renaming moo.sh to something else and running that. Try creating multiple versions of moo.sh and having them echo different things and try running them all at once.

Working With Top

Using Top

The premiere process exploration tool for console-based Linux is Top. Unfortunately it is less useful for a screen access user, because, by default it updates the screen every five seconds.

However if you are assisting others or troubleshooting a process problem, being familiar with Top is highly recommended.

Top is an interactive command; once running, it will continue until you exit by typing q for quit.

There are many other single-letter commands you can issue while it runs. These are the most useful for a user with a screen reader:
Change the refresh delay -- type s followed by the number of seconds as in s20 to update every twenty seconds
Show processes only for a specific user -- type u followed by user's name
Toggle memory usage display -- m
Toggle load average display --l
Toggle the totals --t
Toggle Idle mode (show only running processes) -- i
Change the sort order of columns in the display --o or O
Write default options to disk --W (note uppercase W)
When you have things set the way you want, the capital W will save your configuration so top will always display for you the same way you've set it now.

The sort keys are fully explained in the man page for Top.

For the toggles, the first press will turn them off, and the second press turns them back on again.

Reminders

Commands like top and ps display all processes, or can be told with various parameters to display only particular processes.

The command jobs shows only the current tasks that were started during the current shell. Jobs is part of the bash shell, and is useful because its list is so much shorter, when all you want to do is kill or detach from a recently started job.

Edit - History - Print - Recent Changes - Search
Page last modified on June 13, 2012, at 06:55 AM