Multitasking





Multitasking

In this section, we will cover what is known as multitasking and how it works with Unix. Multitasking is the capability of a CPU (central processing unit), which is the brain of your Unix system, to handle more than one operation at a time. For instance, the capability to run a word processing tool and an email client at the same time without crashing your computer would be considered multitasking. Unix allows you to multitask in that it lets you run more than one process at a time. Remember, with Unix, every command or program that you use is considered a process.

Background Processes

Many times you will want something to run on your Unix system but you won't want to see or use it while it is running. Having this process in the foreground will only cause you to waste time watching something that could be taking place without your input. In this situation, you would want to run a background process. Whereas a foreground process remains in the foreground and usually takes up many of the resources available on your system, a background process can run largely without your intervention.

Making a command run in the background is simple; you simply add an ampersand (&) to the end of the line containing the command. For example, you could use & to run the cron or at commands in the background. The cron command is used for scheduling jobs to be executed regularly after a designated period of time. The at command is for executing commands once at a single specified time. These commands will be discussed in greater depth later in the lesson.

Using Ctrl+z and bg

In Unix, you can stop any process that is running by pressing Ctrl+z. Suspending a command and moving it to the background is another story. If you want to do this, you must be familiar with Ctrl+z as well as another command: the bg command.

To turn any process into a background process, first press Ctrl+z to suspend the process if it is running and you did not enter an ampersand following the command. Consider the following example:

Ctrl+z
[10]+  Stopped                 at 2

Here, the at command has been suspended and assigned job number 10.

The process ID (PID) (as shown with the ps command) is a number used by the kernel to keep track of every process running on the system. This is not the same as the job number. The job number will identify the process number locally in your session, not globally to the system.

PID Versus Job Every process running on a Unix system is assigned a unique PID. Every process you start is also assigned a job number. The job number is only unique to your current session. If you open a Terminal window and start a command, the first command will often get the job number 1. If you open a different Terminal and start another process in that, that process may also get the job number 1. Using the job number in either terminal will only affect the process started in that terminal. Thus PIDs are used globally across the entire system to keep track of processes, whereas job numbers local to the specific session in which they where started.


To move the suspended process to the background, you only need to use the bg command and add the job number directly behind it. In the case of our example, the command would look like this:

>bg %10

As you can see, the background command is easy to use and helpful if you want to run multiple jobs while still being able to control and work with Unix.

The fg Command

Just as you can send processes to the background, you can also bring them back to the foreground. This is done by using the foreground (fg) command. Let's return to our example. If you wanted to bring the at command back to the foreground, you would simply enter the following:

>fg %10

With the fg command, you can quickly bring the process you want to work on to the foreground. When you are done working with this process, you can send it back again with the bg command.

The jobs Command

When working in Unix, it is easy to forget what local processes are running. Say you need to bring a background process to the foreground but you don't know the job number you need to select. Here, the jobs command will show you what the job number is if you have forgotten it. Simply type jobs at the shell prompt and you will be shown the ID numbers for any processes you have started or stopped.

> jobs

The jobs command will also provide you with a history of sorts; using this command will list all previous process that have been started and stopped.

We now come to one of the most important commands in this lesson: the ps command.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows