A program is not a process
A program is a file that contains machine code and data, such as /bin/sleep. Nothing happens when the file sits on disk. The kernel creates a process when something asks it to run that file. The process gets its own virtual memory, its own table of open file descriptors, a current directory, a set of credentials, and a unique number called the process ID, or PID.
The same program can run many times at once. Two terminals that each run sleep 100 create two processes from one file. They share the code pages of the program in memory, but each process has separate variables, separate file descriptors, and separate state. Killing one of them does not affect the other.
PID, PPID and the process tree
Every process except the very first one has a parent. The parent process ID, or PPID, is the PID of the process that created it. Processes started from your shell have your shell as their parent. Your shell was started by the terminal emulator or the login program, and the first process in the system, PID 1, is started by the kernel itself.
Because each process records its parent, the whole system forms a tree. Look at any process and you can walk up the tree to find where it came from. Look at a process you want to stop and you can find the shell or service that started it. The tree is also why a parent can clean up after its children, a point the later units in this module return to when they describe fork, wait, and zombies.
Process states
At any moment the kernel records a state for each process. The most common states are shown in the STAT column of ps, with one letter each:
- R means running or runnable: the process is on a CPU or waiting in line for one.
- S means interruptible sleep: the process is waiting for an event, such as keyboard input or a timer. Most shells and idle programs are in this state.
- D means uninterruptible sleep, usually waiting for disk I/O. A process in D state cannot be killed until the I/O finishes.
- T means stopped, for example by Ctrl-Z or SIGSTOP.
- Z means zombie: the process has exited, but its parent has not yet collected its exit status.
Extra letters follow the main one. A lowercase s means the process is a session leader, and a + means it is in the foreground process group of its terminal. A typical interactive shell shows Ss, which means a sleeping session leader.
Keep the difference between a process and its state in mind when you read the table. The process is the thing, and its state is a snapshot of what the kernel is doing with it at that instant. A shell that is waiting for your next key and a shell that is running a long pipeline are the same process in two different states. A single reading is therefore a sample, and a single sample can mislead you. When you need to know whether something is stuck or merely busy, take two or three readings a second apart and compare them.
PIDs are also reused. When a process ends, the kernel eventually gives its number to a new process, so a PID you wrote down earlier may now belong to something else. Scripts that save a PID to a file should check the command name as well as the number before they send a signal. The later lessons in this module use that check whenever they stop a job.
Keep the difference between a process and its state in mind when you read the table. The process is the thing, and its state is a snapshot of what the kernel is doing with it at that instant. A shell that is waiting for your next key and a shell that is running a long pipeline are the same process in two different states. A single reading is therefore a sample, and a single sample can mislead you. When you need to know whether something is stuck or merely busy, take two or three readings a second apart and compare them.
PIDs are also reused. When a process ends, the kernel eventually gives its number to a new process, so a PID you wrote down earlier may now belong to something else. Scripts that save a PID to a file should check the command name as well as the number before they send a signal. The later lessons in this module use that check whenever they stop a job.
Looking at your own shell
The shell variable $$ expands to the PID of the current shell. It is the quickest way to get a PID you know is alive, because the shell is running the line you are typing. The next command shows the PID, the parent, the state, and the command name for that one process.
echo $$
ps -o pid,ppid,stat,comm -p $$The -p option restricts the output to the PIDs listed after it. The -o option chooses the columns. The comm column is the short command name, usually the file name of the program, and it is limited to 15 characters. The args column shows the full command line instead.
The kernel exposes the same facts through the /proc file system. Each process has a directory named after its PID, and the file status in that directory holds a readable summary. The next command selects three of its fields from the shell itself.
grep -E "^(Name|State|PPid)" /proc/$$/statusThe Name line gives the command name, State gives the state with a description in parentheses, and PPid gives the parent. The values match what ps printed, because both tools read the same kernel data.
The state changes from moment to moment. A shell that shows S is not idle in a hidden way. It is sleeping in a read call, waiting for your next keystroke, and the kernel wakes it when the key arrives.
Hands-on: read your process tree
Run the commands below in the lab. The first prints the PID of your shell. The second prints the same shell with its parent and state. The third prints the state from /proc, which you can compare with the STAT column.
echo "shell pid is $$"
ps -o pid,ppid,stat,comm -p $$
grep State /proc/$$/statusThe PID numbers differ from run to run, so do not memorize them. The pattern is what matters. The PPID of your shell is the PID of whatever started it, and the state column for an idle shell shows S. In the next unit you will list every process on the system and find these same relationships among the others.