At the heart of Unix lies the concept of a process. Understanding this concept will help you keep control of your login session as a user. If you are also a system administrator, the concept is even more important.
A process is an independently running program that has its own set of resources. For instance, we showed in an earlier section how you could direct the output of a program to a file while your shell continued to direct output to your screen. The reason that the shell and the other program can send output to different places is that they are separate processes.
On Unix, the finite resources of the system, like the memory and the disks, are managed by one all-powerful program called the kernel. Everything else on the system is a process.
Thus, before you log in, your terminal is monitored by a getty process. After you log in, the getty process dies (a new one is started by the kernel when you log out) and your terminal is managed by your shell, which is a different process. The shell then creates a new process each time you enter a command. The creation of a new process is called forking because one process splits into two.
If you are using the X Window System, each process starts up one or more windows. Thus, the window in which you are typing commands is owned by an xterm process. That process forks a shell to run within the window. And that shell forks yet more processes as you enter commands.
To see the processes you are running, enter the command ps. Figure 4-4 shows some typical output and what each field means. You may be surprised how many processes you are running, especially if you are using X. One of the processes is the ps command itself, which of course dies as soon as the output is displayed.
The first field in the ps output is a unique identifier for the process. If you have a runaway process that you can't get rid of through Ctrl-C or other means, you can kill it by going to a different virtual console or X window and entering:
$ kill process-id
The TTY field shows which terminal the process is running on, if any. (Everything run from a shell uses a terminal, of course, but background daemons don't have a terminal.)
The STAT field shows what state the process is in. The shell is currently suspended, so this field shows an S. An Emacs editing session is running, but it's suspended using Ctrl-Z. This is shown by the T in its STAT field. The last process shown is the ps that is generating all this input; its state, of course, is R because it is running.
The TIME field shows how much CPU time the processes have used. Because both bash and Emacs are interactive, they actually don't use much of the CPU.
You aren't restricted to seeing your own processes. Look for a minute at all the processes on the system. The a option stands for all processes, while the x option includes processes that have no controlling terminal (such as daemons started at runtime):
$ ps ax | more
Now you can see the daemons that we mentioned in the previous section.
Recent versions of the ps command have a nice additional option. If you are looking for a certain process of which you know the name or at least parts of it, you can use the option -C, followed by the name to see only the processes whose names match the name you specify:
$ ps -C httpd
And here, with a breathtaking view of the entire Unix system at work, we end this chapter (the lines are cut off at column 76; if you want to see the command lines in their full glory, add the option -w to the ps command):
kalle@owl:~ > ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 416 64 ? S Mar23 0:57 init [3] root 2 0.0 0.0 0 0 ? SW Mar23 30:05 [kflushd] root 3 0.0 0.0 0 0 ? SW Mar23 13:26 [kupdate] root 4 0.0 0.0 0 0 ? SW Mar23 11:39 [kswapd] root 5 0.0 0.0 0 0 ? SW< Mar23 0:00 [mdrecoveryd] root 9 0.0 0.0 0 0 ? SW Mar23 0:00 [khubd] bin 1512 0.0 0.0 1272 0 ? SW Mar23 0:00 [portmap] root 1530 0.0 0.0 1328 216 ? S Mar23 0:17 /sbin/syslogd root 1534 0.0 0.0 1728 180 ? S Mar23 0:03 /sbin/klogd -c 1 root 1560 0.0 0.0 1236 0 ? SW Mar23 0:00 [usbmgr] root 1570 0.0 0.0 1728 0 ? SW Mar23 0:00 [nlservd] at 1578 0.0 0.0 1388 108 ? S Mar23 0:00 /usr/sbin/atd nobody 1585 0.0 0.0 5592 24 ? S Mar23 0:00 [in.identd] nobody 1586 0.0 0.0 5592 24 ? S Mar23 0:49 [in.identd] nobody 1587 0.0 0.0 5592 24 ? S Mar23 0:00 [in.identd] nobody 1588 0.0 0.0 5592 24 ? S Mar23 0:00 [in.identd] root 1618 0.0 0.0 1676 96 ? S Mar23 0:42 /usr/sbin/rpc.mou root 1621 0.0 0.0 1696 92 ? S Mar23 0:41 /usr/sbin/rpc.nfs root 1649 0.0 0.1 2352 340 ? S Mar23 0:23 sendmail: accepti root 1657 0.0 0.1 2156 404 ? S Mar23 0:47 /usr/sbin/nmbd -D root 1664 0.0 0.0 2652 196 ? S Mar23 0:00 [smbd] root 1681 0.0 0.0 1408 164 ? S Mar23 0:15 /usr/sbin/cron root 1689 0.0 0.0 1328 32 ? S Mar23 0:00 /usr/sbin/lpd root 1713 0.0 0.1 11724 352 ? S Mar23 0:03 /usr/sbin/nscd root 1731 0.0 0.0 1280 168 ? S Mar23 0:00 /usr/sbin/inetd root 1795 0.0 0.0 1996 0 tty1 SW Mar23 0:00 [login] root 1796 0.0 0.0 1996 0 tty2 SW Mar23 0:00 [login] root 1798 0.0 0.0 1228 0 tty4 SW Mar23 0:00 [mingetty] root 1799 0.0 0.0 1228 0 tty5 SW Mar23 0:00 [mingetty] root 1800 0.0 0.0 1228 0 tty6 SW Mar23 0:00 [mingetty] kalle 2302 0.0 0.0 2584 0 tty1 SW Mar23 0:00 [bash] root 2309 0.0 0.0 2428 0 tty2 SW Mar23 0:00 [bash] root 2356 0.0 0.0 1292 56 ? S Mar23 19:32 /usr/sbin/gpm -t root 5860 0.0 0.0 2108 88 ? S Mar23 0:01 [sshd] root 21391 0.0 0.0 2004 0 tty3 SW Mar30 0:00 [login] kdeuser 21394 0.0 0.0 2548 0 tty3 SW Mar30 0:00 [bash] kalle 17613 0.0 0.0 1660 0 ? SW Mar31 0:00 [ssh-agent] kalle 20007 0.0 0.1 2628 512 tty1 S Apr03 0:00 /bin/bash kalle 27278 0.0 0.4 2384 1140 tty1 S Apr22 0:00 sh /usr/X11R6/bin kalle 27279 0.0 0.2 1552 636 tty1 S Apr22 0:00 tee /home/kalle/. kalle 27291 0.0 0.3 2728 824 tty1 S Apr22 0:00 xinit /home/kalle root 27292 25.2 7.2 45724 19080 ? S Apr22 2531:10 X :0 -auth /home kalle 27308 0.0 0.4 2376 1124 tty1 S Apr22 0:00 bash /home/kalle/ kalle 27316 0.0 0.4 2500 1296 tty1 S Apr22 0:00 bash --login /opt kalle 27374 0.0 0.4 14048 1212 ? S Apr22 0:08 kdeinit: dcopserv kalle 27376 0.0 0.6 14540 1812 ? S Apr22 0:01 kdeinit: klaunche kalle 27378 0.0 0.7 13964 1852 ? S Apr22 5:36 kdeinit: kded kalle 27381 0.1 2.3 18564 6252 ? S Apr22 12:03 kdeinit: kdesktop kalle 27388 0.0 0.6 14164 1716 ? S Apr22 0:00 kdeinit: kxmlrpcd kalle 27399 0.1 2.0 15324 5468 ? S Apr22 13:10 kdeinit: klipper kalle 27401 0.0 1.4 14316 3920 ? S Apr22 0:02 kdeinit: khotkeys kalle 27403 0.0 0.1 13608 296 ? S Apr22 0:00 kdeinit: Running. kalle 27404 0.0 1.6 14516 4204 ? S Apr22 0:01 kdeinit: kwrited kalle 27406 0.0 0.1 1440 520 pts/0 S Apr22 0:00 /bin/cat kalle 27409 0.0 2.1 13680 5664 tty1 S Apr22 0:06 knotify kalle 27413 0.0 1.8 10476 4844 tty1 S Apr22 0:03 ksmserver --resto kalle 27415 0.3 1.0 15032 2728 ? S Apr22 30:35 kdeinit: kwin -se kalle 27426 0.0 0.9 15496 2436 ? S Apr22 0:29 kdeinit: konsole kalle 27429 0.0 0.9 15456 2424 ? S Apr22 0:03 kdeinit: konsole kalle 27430 0.0 0.5 2672 1568 pts/2 S Apr22 0:00 /bin/bash kalle 27431 0.0 0.6 2908 1776 pts/1 S Apr22 0:01 /bin/bash kalle 27489 0.0 2.1 15616 5648 ? S Apr22 0:06 kdeinit: kio_uise kalle 27492 0.0 1.9 14636 5092 ? S Apr22 0:03 kdeinit: kcookiej kalle 27494 0.0 0.7 9896 1916 ? S Apr22 0:05 kdesud kalle 30812 0.0 1.1 17620 2900 pts/1 S Apr23 6:01 kicker kalle 30893 0.0 0.6 15556 1784 ? S Apr23 0:22 kdeinit: konsole kalle 30894 0.0 0.6 2680 1580 pts/3 S Apr23 0:00 /bin/bash kalle 30902 0.0 2.7 13912 7132 pts/3 S Apr23 0:26 xemacs kalle 30924 0.0 7.7 36564 20152 ? S Apr23 1:29 konqueror -mimety root 1573 0.0 1.2 4740 3320 ? S Apr23 1:07 /usr/sbin/smbd -D kalle 5812 0.0 0.9 15600 2408 ? S Apr24 1:35 kdeinit: konsole kalle 5813 0.0 0.6 2836 1720 pts/4 S Apr24 0:00 /bin/bash kalle 5820 0.1 5.3 18904 13908 pts/4 S Apr24 12:13 xemacs kalle 6100 0.0 0.1 1660 400 ? S Apr24 0:00 ssh-agent kalle 11670 0.0 6.2 26836 16376 ? S Apr25 0:16 konqueror -mimety kalle 13390 0.0 0.6 15596 1812 ? S Apr25 0:20 kdeinit: konsole kalle 13391 0.0 0.6 2684 1592 pts/5 S Apr25 0:00 /bin/bash kalle 15039 0.0 0.1 1656 396 ? S Apr25 0:00 ssh-agent kalle 15337 0.0 0.9 15836 2560 ? S Apr25 0:03 designer kalle 18660 0.0 6.4 24592 16976 pts/5 S Apr26 4:29 xemacs kalle 22903 0.2 13.2 42784 34756 ? S Apr26 11:59 kmail -caption KM kalle 8744 0.0 3.3 15336 8712 ? S 10:09 0:22 ksnapshot kalle 12077 4.3 4.4 14796 11756 ? R 20:42 0:34 xemacs kalle 12130 0.0 1.2 13692 3204 ? S 20:50 0:00 kdeinit: kio_file kalle 12132 0.0 1.2 13692 3204 ? S 20:50 0:00 kdeinit: kio_file kalle 12152 1.2 0.5 2560 1340 pts/6 S 20:55 0:00 /bin/bash -i kalle 12162 0.0 0.3 2688 988 pts/6 R 20:56 0:00 ps aux kalle@owl:~ >
Copyright © 2003 O'Reilly & Associates. All rights reserved.