Vous êtes sur la page 1sur 6

Process

Management
Objectives:
Start and stop processes Send signals to running programs View information on running processes

Concepts
What Is a Process?
A process in simple terms is an instance of a running program. The parent of all processes, the init, is started when the operating system boots. Its process ID is 1. As other programs are started, each is assigned a unique process identifier, known as a PID. Normally, process IDs are assigned in a sequential order. As processes stop, the previously unavailable PIDs can be used again. Usually, PIDs are in the 1 to 32768 range. To see the PID assigned to your shell, look at the $ environment variable. $ echo $$ 1619 This output, 1619 is the process ID of the running command line shell. Starting a process is as simple as typing a command at the shell prompt or starting a program from a menu. The file system attributes indicate whether the file is an executable and who has permission to execute it. You can use the ls long listing (ls l) to see these file modes. The file command found on most Linux systems can also tell you if a file is an executable. For example, on Fedora 15 systems, you might issue this command: $ file /bin/ls /bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped

What Processes Are Running?


It is easy to see your own processes by running the ps (process status) command. Issued without any arguments, ps displays your own processes that are associated with a terminal. The ps tool is very useful for quickly seeing what processes you and others on your same system have running. It also can be used to see which processes are using up your memory or overworking your CPU.

$ ps PID TTY 1735 pts/1 1891 pts/1 1898 pts/1 PID: TTY: TIME: CMD:

TIME 00:00:00 00:00:00 00:00:00

CMD bash gedit ps

displays the process ID the terminal that process is attached to the accumulated user plus time for the process the command and some arguments for that process

Process States
To have ps output extra information, you can use the u argument
$ ps u

USER test test test test

PID %CPU %MEM VSZ RSS TTY 1619 0.0 0.0 5280 1800 pts/0 1735 0.0 0.0 5280 1800 pts/1 1891 0.2 1.2 132232 25004 pts/1 1923 0.0 0.0 4628 1024 pts/1

STAT Ss+ Ss Sl R+

START 05:08 05:42 05:52 06:00

TIME 0:00 0:00 0:00 0:00

COMMAND bash bash gedit ps u

As you can see, it reports the user running the process, the process ID, the percentage of the CPU the process has been using, the percentage of the real memory, the virtual memory size in kilobytes, the physical memory used, the terminal it is connected to, the states, when the process was started, the amount CPU time used by process since it was started and the command name. The following table shows some of the common process states. State Function I Idle, sleeping for more than 20 seconds D Waiting for disk or other uninterruptible wait R Runnable, active use S Sleeping for less than 20 seconds T Stopped or traced process Z Zombie process; a dead or defunct process * The small s state means it is a session leader. The plus ( + ) means that process has use of that terminal.

System Processes
By default, your system should have several running processes or in other run states. For example, an ordinary workstation might have 76 processes and 53 processes running. This is easy to see by issuing the command: $ ps ax | wc l 145 * The argument a for ps causes it to report information about processes for all users. The x argument tells ps to display information about processes without a controlling terminal.

System processes are programs running behind the scenes handling many essential maintenance aspects for your system. Normally, system processes do not have a TTY (teletype) in use. Many of these processes are often called daemons and they do routine work. The following is an example of system processes on a Linux system. $ ps ax
PID 1 2 3 6 7 8 9 10 11 12 13 14 15 16 17 18 21 22 23 24 TTY ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? STAT Ss S S S S S< S< S S< S S S< S< S< S S< S SN SN S TIME 0:05 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 COMMAND /sbin/init [kthreadd] [ksoftirqd/0] [migration/0] [watchdog/0] [cpuset] [khelper] [kdevtmpfs] [netns] [sync_supers] [bdi-default] [kintegrityd] [kblockd] [ata_sff] [khubd] [md] [kswapd0] [ksmd] [khugepaged] [fsnotify_mark]

In the example above, a terminal (TTY) is not associated with most of the system processes. This ld. This noted by a question mark in the TTY field.

Process Attributes
Each process has an environment with various attributes such as command-line arguments, user environment variables, file descriptors, working directory, file creation mask, controlling terminal (console), resource limitations and a lot more. Many of the attributes are shared with the parent process. To view process attributes, you can use the ps o switch. It is used for defining the output format. The following are commonly used output format fields: Field Definition user Effective user ID of the process pid Process ID ppid Process ID of the parent pcpu Percentage of CPU time used rss Real memory size in kilobytes pmem Percentage of rss to physical memory vsz Kilobytes of the process in virtual memory tty Controlling terminal name state ( or s ) Process state

stime time command ( or comm)

Time started Accumulated user and system CPU time Command name

To output the process IDs, parent PIDs and the size of the process in virtual memory of your own running processes: $ ps o user,pid,ppid,vsz,comm
USER test test test PID 1735 2087 2141 PPID VSZ COMMAND 1613 5280 bash 1735 373268 firefox 1735 4628 ps

Stopping Processes
Ending a process can be done in several different ways. Often, from a console-based command, sending a CTRL+C keystroke (the default interrupt character) will exit the command. But sometimes the interrupt character may be trapped or ignored. The standard tool for killing a process is kill. Technically, kill command does not kill a command, but sends a special signal to the process. Signals are used for simple communication between processes. Over 30 signals are available. The default signal for the kill command is SIGTERM (for terminate). It is possible that the software you are trying to stop is written to cleanly backup files or close down its work when it receives this TERM signal, but do not count on it. To use the kill command, just place the process ID of the process to signal as the command line argument. For example, to send the default SIGTERM signal to process ID 5432, run the command: $ kill 5432 You can also choose the signal by name by prefixing the signal name with a dash $ kill SIGTERM 5432 To list the possible signal names, run kill with the l switch.
$ kill l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12

47)

SIGRTMIN+13 48) SIGRTMAX-13 52) SIGRTMAX-9 56) SIGRTMAX-5 60) 164) SIGRTMAX

SIGRTMIN+14 49) SIGRTMIN+15 SIGRTMAX-12 53) SIGRTMAX-11 SIGRTMAX-8 57) SIGRTMAX-7 SIGRTMAX-4 61) SIGRTMAX-3

50) 54) 58) 62)

SIGRTMAX-14 51) SIGRTMAX-10 55) SIGRTMAX-6 59) SIGRTMAX-2 63) SIGRTMAX-

If you attempt to kill a process and it does not die, you can try using the ignorable SIGKILL signal. This signal cannot be handled by individual programs. The normal way to find the PID of the command you want to kill is with the ps command. The ps output can be piped through grep to list only matching processes. For example, to find the process ID of the Firefox Web Browser: $ ps aux | grep firefox test 2169 15.7 3.8 383640 79460 pts/1 Sl 07:39 0:04 /usr/lib/firefox/firefox test 2214 0.0 0.0 4436 760 pts/1 S+ 07:39 0:00 grep --color=auto firefox The grep output also shows the grep line itself. You could kill that Firefox process with the command kill 2169. Many Linux systems provide tools called pgrep, pkill and killall that can be used with command names instead of using PIDs. A safer tool is pkill. The pkill command is used like the kill command but instead of using PID as the argument to the signal, the command name is used. To kill Firefox Web Browser, you could run: $ pkill firefox The argument to pkill is a simple regular expression for matching. The pgrep tool can be used to simply list the process IDs as pkill would see them, without sending any signal.

Zombie Processes
Normally, when a child process is killed, the parent process is told via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. When a process is killed, a ps listing may still show the process with a Z state. This is a zombie or defunct process. The process is dead and not being used. In most cases in a few moments it will actually be gone and your ps output wont show it. In some rare cases, like with old Linux kernels combined with buggy software, you may find that some processes just wont die even if you send a SIGKILL signal to them. If you find that the process stubbornly refuses to die, you may need to reboot your system.

The top Command


The top command is a very useful tool for showing processes sorted by various criteria. It is an interactive diagnostic tool that updates frequently and shows information about physical and virtual memory usage, CPU usage, load averages and your busy processes.

Exercises. Please provide screenshots to support your answers.


1. Use top command to show the processes running on your machine. What process takes up the most CPU utilization? 2. Make top sort by memory usage so that the most memory-hungry processes appear on top. 3. Restrict the display to show only processes owned by you. 4. Try killing one of your processes 5. Display a list of all the processes running on the machine using ps

Vous aimerez peut-être aussi