Ceci est une ancienne révision du document !
Many programs are available to help in identifying the reasons why a computer is running slowly. Top goes a step further and enables the user to intervene in running processes. What is a process A process is an instance of one or more connected tasks or threads which are executed by the computer. Some of them are independent, others are connected with each other. They use hardware like the processor (CPU), memory, screen, etc. The Linux kernel shares the available hardware with the running processes. With the help of control groups (cgroups), we can arrange single processes into groups, and allocate system resources to them based on the priority of the group. This is the work of the scheduler, which creates a queue that handles the processes based on their priorities. To identify, control and manipulate processes, the operating system assigns a PID (Process ID) to each of them. Usually the PID's are assigned in ascending order and remain unchanged as long the system is running.
The program ‘top’ To start top, we have to open a terminal and simply type in “top”. Now the screens fills with a lot of data. To make some sense of it, let's take a look at the first row (in the image above): top - 20:14:42 up 57 min: gives the current time and how long the computer has been in use. 1 user: number of users logged in. load average: 0.97, 1.11, 1.08: shows the load average (workload) of the CPU during the most recent 1.5 and 15 minutes. For better accuracy, divide it by the number of CPU cores so that for example 1.08 divided by 2 cores means that the work for each core is 0.54. If this number > 1, then a queue exists, and the scheduler must be invoked to allocate computing time. Tasks: 140 total, 1 running, 138 sleeping, 0 stopped, 1 Zombie: The second row displays the number of processes currently running, sleeping and stopped. The Zombie is a so-called child process, once started by a parent process, but not ended when the parent process was stopped. It's harmless, but if many are present, they may consume some memory and PID's.
The third row shows the CPU workload for different clients: “us” the user “sy” the kernel “ni” user jobs with low priority “id” the idle Mode (if it’s high, then the workload is low, and vice versa) “wa” the percentage of jobs waiting for computing time Interrupts displayed in “hi” - hardware interrupts and “si” software interrupts “st” (steal time) shows the amount of CPU power that a virtual machine is using. The fourth and the fifth rows show the usage of the memory and swap - the amount in use and the amount that’s still free.
Below the system information we find the process list. It is structured in 12 parts: • PID the process ID • USER the user who has started the task • PR process priority • NI the nice value • VIRT virtual memory • RES physical memory • SHR shared memory • S status • %CPU percentage of CPU power taken by the the process • %MEM percentage of memory used by the process • TIME+ time since the process started • COMMAND the command used to start the process There are some keyboard shortcuts to personalize the information displayed by top: • t hides/shows the second and third row in the info section (tasks, CPU usage, etc). • m hides or displays the fourth and the fifth row (memory, swap, etc). • A sorts the process list in order of resource usage. • f the user can decide what task details should be displayed by top. • o the user may configure the order of the list by his own needs.
Working with top To manipulate a process, top offers two ways: • To stop a program that’s no longer needed, or that has crashed, the k (kill) command is very useful. After we give the k command, top asks for the PID of the program we want to stop. After typing in the PID, top asks us to confirm the input of the stop signal. • To manipulate the priority of a task, we use the command r (renice). With it, it's possible to reduce or to raise the priority of a process. The ‘r’ command may be unknown to many users, so these details may help: If a system is running slowly, top may help to analyze the reason. Mostly it will be sufficient to stop one or more processes to fix the problem. If this is not possible – because none of the running tasks can be terminated – their priority can be reduced and the priority of more important process can be raised. A higher priority means more computing time from the processor, and vice versa.
Linux, based on UNIX, works with the UNIX system to determine priorities. The highest level is -20, the lowest 19. That means the more important a process is the lower is the assigned number. This number is called the ‘Nice Value’ and is displayed, as mentioned before, in the column NI in the process list. To raise a ‘nice value’, top must be started as root. To renice an owner (user) process, it is possible to do this without root power – but only to raise the priority, not to reduce it. For experimentation, it’s better to start ‘top’ in the user mode, and change the priority of only user tasks. Before changing any other tasks, please investigate what effects this will have on your system, especially for root tasks… avoid making experimental or unjustified changes. Conclusion Top is easy to understand, so everybody can use it. It has not only display functions, it also enables the user to intervene in processes.
BIO : Jürgen is the former author of the now defunct YALM (Yet Another Linux Magazine) and a Linux user since 2007.
Bio : Jurgen est l'ancien auteur de maintenant défunt YALM (Yet Another Linux Magazine - Encore un autre maganzine Linux) et un utilisateur de Linux depuis 2007.