linux managing process

Upload: chs360

Post on 01-Mar-2016

212 views

Category:

Documents


2 download

DESCRIPTION

managing processes in linux tutorial

TRANSCRIPT

  • Introduction to Linux:Prev Chapter 4. Processes Next

    4.3. Managing processes4.3.1. Work for the system adminWhile managing system resources, including processes, is a task for the localsystem administrator, it doesn't hurt a common user to know something about it,especially where his or her own processes and their optimal execution areconcerned.We will explain a little bit on a theoretical level about system performance,though not as far as hardware optimization and other advanced procedures.Instead, we will study the daily problems a common user is confronted with, andactions such a user can take to optimally use the resources available. As welearn in the next section, this is mainly a matter of thinking before acting.Figure 4-2. Can't you go faster?

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    1 of 9 06/01/2015 05:37 PM

  • 4.3.2. How long does it take?Bash oers a built-in time command that displays how long a command takes toexecute. The timing is highly accurate and can be used on any command. In theexample below, it takes about a minute and a half to make this book:

    tilly:~/xml/src> time makeOutput written on abook.pdf (222 pages, 1619861 bytes).Transcript written on abook.log.

    real 1m41.056suser 1m31.190ssys 0m1.880s

    The GNU time command in /usr/bin (as opposed to the shell built-in version)displays more information that can be formatted in dierent ways. It also shows

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    2 of 9 06/01/2015 05:37 PM

  • the exit status of the command, and the total elapsed time. The same commandas the above using the independent time gives this output:

    tilly:~/xml/src> /usr/bin/time makeOutput written on abook.pdf (222 pages, 1595027 bytes).Transcript written on abook.log.

    Command exited with non-zero status 288.87user 1.74system 1:36.21elapsed 94%CPU

    (0avgtext+0avgdata 0maxresident)k0inputs+0outputs (2192major+30002minor)pagefaults 0swaps

    Refer again to the Info pages for all the information.

    4.3.3. PerformanceTo a user, performance means quick execution of commands. To a systemmanager, on the other hand, it means much more: the system admin has tooptimize system performance for the whole system, including users, allprograms and daemons. System performance can depend on a thousand tinythings which are not accounted for with the time command:

    the program executing is badly written or doesn't use the computerappropriatelyaccess to disks, controllers, display, all kinds of interfaces, etc.reachability of remote systems (network performance)amount of users on the system, amount of users actually workingsimultaneouslytime of day...

    4.3.4. LoadIn short: the load depends on what is normal for your system. My old P133running a rewall, SSH server, le server, a route daemon, a sendmail server, aproxy server and some other services doesn't complain with 7 users connected;the load is still 0 on average. Some (multi-CPU) systems I've seen were quitehappy with a load of 67. There is only one way to nd out - check the loadregularly if you want to know what's normal. If you don't, you will only be ableto measure system load from the response time of the command line, which is a

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    3 of 9 06/01/2015 05:37 PM

  • very rough measurement since this speed is inuenced by a hundred otherfactors.Keep in mind that dierent systems will behave dierent with the same loadaverage. For example, a system with a graphics card supporting hardwareacceleration will have no problem rendering 3D images, while the same systemwith a cheap VGA card will slow down tremendously while rendering. My oldP133 will become quite uncomfortable when I start the X server, but on amodern system you hardly notice the dierence in the system load.

    4.3.5. Can I do anything as a user?A big environment can slow you down. If you have lots of environment variablesset (instead of shell variables), long search paths that are not optimized (errorsin setting the path environment variable) and more of those settings that areusually made "on the y", the system will need more time to search and readdata.In X, window managers and desktop environments can be real CPU-eaters. Areally fancy desktop comes with a price, even when you can download it for free,since most desktops provide add-ons ad innitum. Modesty is a virtue if youdon't buy a new computer every year.4.3.5.1. PriorityThe priority or importance of a job is dened by it's nice number. A programwith a high nice number is friendly to other programs, other users and thesystem; it is not an important job. The lower the nice number, the moreimportant a job is and the more resources it will take without sharing them.Making a job nicer by increasing its nice number is only useful for processesthat use a lot of CPU time (compilers, math applications and the like). Processesthat always use a lot of I/O time are automatically rewarded by the system andgiven a higher priority (a lower nice number), for example keyboard inputalways gets highest priority on a system.Dening the priority of a program is done with the nice command.Most systems also provide the BSD renice command, which allows you tochange the niceness of a running command. Again, read the man page for yoursystem-specic information.

    Interactive programs

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    4 of 9 06/01/2015 05:37 PM

  • It is NOT a good idea to nice or renice an interactive program or a jobrunning in the foreground.

    Use of these commands is usually a task for the system administrator. Read theman page for more info on extra functionality available to the systemadministrator.4.3.5.2. CPU resourcesOn every Linux system, many programs want to use the CPU(s) at the sametime, even if you are the only user on the system. Every program needs a certainamount of cycles on the CPU to run. There may be times when there are notenough cycles because the CPU is too busy. The uptime command is wildlyinaccurate (it only displays averages, you have to know what is normal), but farfrom being useless. There are some actions you can undertake if you think yourCPU is to blame for the unresponsiveness of your system:

    Run heavy programs when the load is low. This may be the case on yoursystem during the night. See next section for scheduling.Prevent the system from doing unnecessary work: stop daemons andprograms that you don't use, use locate instead of a heavy nd, ...Run big jobs with a low priority

    If none of these solutions are an option in your particular situation, you maywant to upgrade your CPU. On a UNIX machine this is a job for the systemadmin.4.3.5.3. Memory resourcesWhen the currently running processes expect more memory than the system hasphysically available, a Linux system will not crash; it will start paging, orswapping, meaning the process uses the memory on disk or in swap space,moving contents of the physical memory (pieces of running programs or entireprograms in the case of swapping) to disk, thus reclaiming the physical memoryto handle more processes. This slows the system down enormously since accessto disk is much slower than access to memory. The top command can be used todisplay memory and swap use. Systems using glibc oer the memusage andmemusagestat commands to visualize memory usage.If you nd that a lot of memory and swap space are being used, you can try:

    Killing, stopping or renicing those programs that use a big chunk ofmemory

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    5 of 9 06/01/2015 05:37 PM

  • Adding more memory (and in some cases more swap space) to the system.Tuning system performance, which is beyond the scope of this document.See the reading list in Appendix A for more.

    4.3.5.4. I/O resourcesWhile I/O limitations are a major cause of stress for system admins, the Linuxsystem oers rather poor utilities to measure I/O performance. The ps, vmstatand top tools give some indication about how many programs are waiting forI/O; netstat displays network interface statistics, but there are virtually no toolsavailable to measure the I/O response to system load, and the iostat commandgives a brief overview of general I/O usage. Various graphical front-ends exist toput the output of these commands in a humanly understandable form.Each device has its own problems, but the bandwidth available to networkinterfaces and the bandwidth available to disks are the two primary causes ofbottlenecks in I/O performance.Network I/O problems:

    Network overload:The amount of data transported over the network is larger than thenetwork's capacity, resulting in slow execution of every network relatedtask for all users. They can be solved by cleaning up the network (whichmainly involves disabling protocols and services that you don't need) or byreconguring the network (for example use of subnets, replacing hubs withswitches, upgrading interfaces and equipment).Network integrity problems:Occurs when data is transferred incorrectly. Solving this kind of problemcan only be done by isolating the faulty element and replacing it.

    Disk I/O problems:per-process transfer rate too low:Read or write speed for a single process is not suicient.aggregate transfer rate too low:The maximum total bandwidth that the system can provide to all programsthat run is not enough.

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    6 of 9 06/01/2015 05:37 PM

  • This kind of problem is more diicult to detect, and usually takes extrahardware in order to re-divide data streams over buses, controllers and disks, ifoverloaded hardware is cause of the problem. One solution to solve this is aRAID array conguration optimized for input and output actions. This way, youget to keep the same hardware. An upgrade to faster buses, controlers and disksis usually the other option.If overload is not the cause, maybe your hardware is gradually failing, or notwell connected to the system. Check contacts, connectors and plugs to startwith.4.3.5.5. UsersUsers can be divided in several classes, depending on their behavior withresource usage:

    Users who run a (large) number of small jobs: you, the beginning Linuxuser, for instance.Users who run relatively few but large jobs: users running simulations,calculations, emulators or other programs that eat a lot of memory, andusually these users have accompanying large data les.Users who run few jobs but use a lot of CPU time (developers and the like).

    You can see that system requirements may vary for each class of users, and thatit can be hard to satisfy everyone. If you are on a multi-user system, it is useful(and fun) to nd out habits of other users and the system, in order to get themost out of it for your specic purposes.4.3.5.6. Graphical toolsFor the graphical environment, there are a whole bunch of monitoring toolsavailable. Below is a screen shot of the Gnome System Monitor, which hasfeatures for displaying and searching process information, and monitoringsystem resources:Figure 4-3. Gnome System Monitor

    There are also a couple of handy icons you can install in the task bar, such as adisk, memory and load monitor. xload is another small X application formonitoring system load. Find your favorite!

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    7 of 9 06/01/2015 05:37 PM

  • 4.3.5.7. Interrupting your processesAs a non-privileged user, you can only inuence your own processes. We alreadysaw how you can display processes and lter out processes that belong to aparticular user, and what possible restrictions can occur. When you see that oneof your processes is eating too much of the system's resources, there are twothings that you can do:

    Make the process use less resources without interrupting it;1. Stop the process altogether.2.

    In the case that you want the process to continue to run, but you also want togive the other processes on the system a chance, you can renice the process.Appart from using the nice or renice commands, top is an easy way of spottingthe troublesome process(es) and reducing priority.Identify the process in the "NI" column, it will most likely have a negativepriority. Type r and enter the process ID of the process that you want to renice.Then enter the nice value, for instance "20". That means that from now on, thisprocess will take 1/5 of the CPU cycles at the most.Examples of processes that you want to keep on running are emulators, virtualmachines, compilers and so on.If you want to stop a process because it hangs or is going totally berserk in theway of I/O consumption, le creation or use of other system resources, use thekill command. If you have the opportunity, rst try to kill the process softly,sending it the SIGTERM signal. This is an instruction to terminate whatever it isdoing, according to procedures as described in the code of the program:

    joe:~> ps -ef | grep mozillajoe 25822 1 0 Mar11 ? 00:34:04 /usr/lib/mozilla-1.4.1/mozilla-

    joe:~> kill -15 25822

    In the example above, user joe stopped his Mozilla browser because it hung.Some processes are a little bit harder to get rid of. If you have the time, youmight want to send them the SIGINT signal to interrupt them. If that does notdo the trick either, use the strongest signal, SIGKILL. In the example below, joestops a Mozilla that is frozen:

    joe:~> ps -ef | grep mozilla

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    8 of 9 06/01/2015 05:37 PM

  • joe 25915 1 0 Mar11 ? 00:15:06 /usr/lib/mozilla-1.4.1/mozilla-

    joe:~> kill -9 25915joe:~> ps -ef | grep 25915joe 2634 32273 0 18:09 pts/4 00:00:00 grep 25915

    In such cases, you might want to check that the process is really dead, using thegrep lter again on the PID. If this only returns the grep process, you can besure that you succeeded in stopping the process.Among processes that are hard to kill is your shell. And that is a good thing: ifthey would be easy to kill, you woud loose your shell every time you type Ctrl-Con the command line accidentally, since this is equivalent to sending a SIGINT.

    UNIX without pipes is almost unthinkableThe usage of pipes (|) for using output of one command as input of anotheris explained in the next chapter, Chapter 5.

    In a graphical environment, the xkill program is very easy to use. Just type thename of the command, followed by an Enter and select the window of theapplication that you want to stop. It is rather dangerous because it sends aSIGKILL by default, so only use it when an application hangs.

    Prev Home NextBoot process, Init andshutdown

    Up Scheduling processes

    Managing processes http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    9 of 9 06/01/2015 05:37 PM