Previous Up Next

7  Processes

7.1  What is a process

A lot of talk in this document has gone on about “processes” and “executing programs” and so forth, so this section will give a broad and generic overview of what a “process” is and what you need to know about them.

A program is a set of instructions that lives on disk, any file that contains instructions as to how it should run is a program. A process is an image of that code in memory that is actually running at that time. So any time you run the “ls” program a process is spawned, executes the code and returns its results.

So first some information about processes, the most important things are that they have a name that is basically the command line used to run them, that includes their name and all their arguments. Secondly they have a unique PID (Process IDentification number), this number is used by all the programs that affect processes, because while there might be 10 processes called “vim” only one of them has the PID “20963”

Processes also have whats called a PPID (Parent Process IDentification number), this is the PID of the processes that spawned them, so all processes which are started from your shell will have its PID as their PPID.

Lastly they also have a UID, which was responsible for starting them, or in certain situations has accepted the running on them in the future. Usually the UID of your processes will be the same as your user name.

7.2  Running multiple jobs

While it’s technically a function of the shell this section will deal with management of multiple jobs by the tcsh shell.

If you want to see a list of the currently running jobs then it’s fairly simple, just typing:

% jobs

Will list all the currently running jobs, if it returns nothing at all then that indicates you aren’t running any other processes in the background.

Making your shell run multiple jobs at once is fairly easy. If you’re running another program, say a mailer or an editor, and you want to quickly drop to your shell to run another program (e.g. an ls, to check the name of a file) you can press “^z” (control and ‘z’) to suspended the current process. While suspended the process will stop totally if its something that is still doing some work (like a long running process, for example a number crunching program, or a long find) it won’t carry on running.

After pressing ^z you should get your prompt back, and there should be a line of text above it saying “Suspended”. At this point typing “jobs” will give you a list of the running jobs, each will have a number in square brackets at the front, and will say its status, “Suspended” has already been discussed but you might find some processes which say “Running”, this means they are running quietly in in the background.

Processes which take a long time to run are best run in the background, to do this simply type an ‘&’ (ampersand) at the end of the command line. This effect can also be achieved by typing “bg %N” where N is the number in square brackets in the listing from “jobs”.

If you can see a process on the jobs list that you want to stop running you can kill it with the “kill” command (see section 7.3) however for ease of use you don’t need to know its PID, if you look at the jobs list and find the number in the square brackets then just type:

% kill %N

Where N is the job number, the % symbol before it means it will be looked up on the jobs list, and not the process list.

Finally if you want to switch back to running a process fully in the foreground you will need the “fg” command. It accepts one argument of the job number you want in the foreground, so typing:

% fg %2

Will put the second job on the list back into the foreground.

7.3  Kill

The “kill” command is actually rather misleadingly named. It really sends a “signal” from a selection to a process. These signals can include pause, resume, redo from start and that kind of thing. However its default signal is the TERM signal, which tells a process to halt.

Use of kill to stop processes is fairly simple, first look up the PID (see section 7.4) of the process you want to kill (or its job number, see 7.2), then just use the following command:

% kill PID

Where “PID” is the PID of the process you want to kill (it will always be a number) or is % followed by the job number. Kill will normally not return anything if it runs successfully but the process that’s being asked to die will sometimes say something like “Terminated”, or “Exiting on signal 15.”, or something else appropriate.

Sometimes you will find a process you want to kill has wedged in some way that means it’s not responding to a TERM signal (which can be thought of as a polite way to ask the program to exit). In those situations you should use the following command:

% kill -9 PID

Again replacing “PID” with the actual PID of the process in question. Using the -9 option sends the signal called SIGKILL, which makes the operating system simply slay the process in question. This command is to be used only on wedged processes as it will cause them to die right away, and they may leave temporary files laying about the file system and that kind of thing.

For details of the other things that can be done with the kill command consult “man kill”.

7.4  ps

There are several commands which can be used to find out information about the processes running on the system, the most common of which is “ps”.

In its simplest form it will print out all the PIDs and names of all the processes running on that TTY, to do this just type:

% ps

And you will see a list that probably just includes your shell (tcsh). While this is fairly useful in and of itself you’ll often find that you’re logged in to two or three TTYs at once doing various things, and so you’ll need to find out more than just the processes running on that login.

To see a list of all your running processes use:

% ps -U $USER

Where $USER can be either the environment variable “$USER” (as that contains your username), your actual username, or if you want to see the processes of another user their username. Since ps defaults to only showing your processes using -U with $USER is in most situations fairly pointless.

However that list can be quite long, so its worth knowing that you can do the following command to search all of your processes for ones called a certain thing:

% ps -U $USER | grep ' command$'

This uses a pipe (see section 10.1) and the grep command to find just the processes you’re interested in, simply replace “command” with the name of any process e.g. “tcsh” to find all your shells, “emacs” to find emacs sessions, “less” for all your pagers etc.

Another option for ps that is quite useful is the “full information” flag, this can be used as shown below:

% ps -f

This will show you information about your processes with much more information including the UID, PID, PPID, TTY and the command line and all its arguments used to start the command.

If you wanted to know this about another user you could simply use:

% ps -fu username

Where “username” is the name of a user whose processes you want to see.

Lastly if you want ps to show you information about all the processes running on the system then you need to -e flag:

% ps -e 

Most of time this isn’t really needed, but sometimes you will want to be able to find out information like this. It should also be noted that the -e flag can be combined with the -f flag, to show all the processes on the machine and full information about them.

7.5  top

If you want a general overview of the processes that are running on the system at any one time then the “top” command is the one to use. Its use is simple, just type:

% top 

You will then see a fairly regularly updating list of the top 10 or so programs, ranked by basically how much system resources they are consuming. You will also get information about how much CPU and memory are being used. Once you’re finished looking then just press “q” to quit.

Of course the interesting thing is that top itself if running on a fairly loaded system can be a fairly large processes in itself. Its constant monitoring can be fairly consumptive of system resources. To get around this problem simply use the following line to print out the top 10 programs then exit.

% top -n 10 

This will also give you all the information about memory and CPU, and is just generally a friendlier thing to use on a potentially full system.


Previous Up Next