Processes
Now we know about files, commands, and shells. What more could UNIX possibly have to offer?
UNIX was designed at a time when computers were used largely for crunching huge amounts of numbers. One of the nicest features of UNIX is its ability to multitask - to do more than one thing at a time. A single task is called a process.
The ps Command
Whenever you are logged in, you are executing at least one process. Let's get a list of all the processes I am currently running:
PID TTY TIME CMD 16931 pts/38 0:00 bash
I'm running one process, bash. bash is my shell, so naturally it is running. The output from ps includes the following information:
- PID - the Process ID of the process. Every program that is running has a PID, and you use this number to identify them.
- TTY - the Terminal that the process is running on. This information generally is not very helpful.
- TIME - the time that the process has been running.
- CMD - The name of the command being executed.
ps has a great many options. Unfortunately, every flavor of UNIX uses a different set of options to ps. The following options are fairly standard:
- a - print all running processes, not just yours
- f - generate a "full" listing, including username and command line
Background Processes
UNIX makes a very strong distinction between processes that are running in the foreground and those that are running in the background. A foreground process can talk to your terminal, and you can talk back using the keyboard. A background process is running, but it has no way to communicate with you directly.
This is most useful when you have a large task to complete (say, calculating Pi to a million digits). We'll use the sleep command as an example. Let's try this:
The sleep command takes one argument, the number of seconds to sleep for. sleep then does nothing for that long.
It's pretty boring to sit and watch as sleep just sits there. We will now run sleep as a background process, which will allow us to go about our business.
[1] 22098
steel /N/fs1/clwolfe/Steel $ ps
PID TTY TIME CMD 16931 pts/38 0:00 bash 22098 pts/38 0:00 sleep
First of all, we use another shell meta-character, & to indicate that the command should be run in the background. You can use the & with any command.
Next, notice that the first command returns a number to us: the Process ID (PID) of the backgrounded process.
Then we use the ps command to confirm that there are now two processes running: bash and sleep.
Finally, after at least 30 seconds have gone by, the next time you execute a command or press Return, you should see somethinglike this:[1]+ Done sleep 30
This is the shell reporting that the command completed successfully.
Killing a Process
Not all processes complete successfuly. You may find yourself stuck with a process that seems to have stopped working, or perhaps you have changed your mind about running the process.
For example, we'll start a new sleep process and kill it.
[1] 23360
steel /N/fs1/clwolfe/Steel $ ps
PID TTY TIME CMD 23360 pts/38 0:00 sleep 16931 pts/38 0:00 bashsteel /N/fs1/clwolfe/Steel $ kill 23360
steel /N/fs1/clwolfe/Steel $ ps
PID TTY TIME CMD 16931 pts/38 0:00 bash [1]+ Terminated sleep 300
First, we start a new sleep process. Then we check that it is running with ps. We note the PID, then issue a kill command, passing the PID as an argument. Then we check again using ps. At the same time, the shell prints out a line indicating that our sleep process has been Terminated.
Next: Common Tasks
Prev: Files
Up: Table of Contents



