Using Priority Scheduling and Control

Every process cannot make use of the system's resources (CPU, memory, disk access, and so on) as it pleases. After all, the kernel's primary function is to manage the system resources equitably. It does this by assigning a priority to each process so that some processes get better access to system resources and some processes might have to wait longer until their turn arrives. Priority scheduling can be an important tool in managing a system supporting critical applications or in a situation in which CPU and RAM usage must be reserved or allocated for a specific task. Two legacy applications included with Ubuntu are the nice and renice commands. (nice is part of the GNU sh-utils package, whereas renice is inherited from BSD UNIX.)

The nice command is used with its -n option, along with an argument in the range of -20 to 19, in order from highest to lowest priority (the lower the number, the higher the priority). For example, to run the xosview client with a low priority, use the nice command like this:

The nice command is typically used for disk- or CPU-intensive tasks that might be obtrusive or cause system slowdown. The renice command can be used to reset the priority of running processes or control the priority and scheduling of all processes owned by a user. Regular users can only numerically increase process priorities (that is, make tasks less important) using this command, but the root operator can use the full nice range of scheduling (-20 to 19).

System administrators can also use the time command to get an idea about how long and how much of a system's resources will be required for a task, such as a shell script. (Here, time is used to measure the duration of elapsed time; the command that deals with civil and sidereal time is the date command.) This command is used with the name of another command (or script) as an argument, as follows:

/proc/sys/net/core real 1.20 user 0.14 sys 0.71

Output of the command displays the time from start to finish, along with the user and system time required. Other factors you can query include memory, CPU usage, and file system input/output (I/O) statistics. See the time command's man page for more details.

Nearly all graphical process-monitoring tools include some form of process control or management. Many of the early tools ported to Linux were clones of legacy UNIX utilities. One familiar monitoring (and control) program is top. Based on the ps command, the top command provides a text-based display of constantly updated console-based output showing the most CPU-intensive processes currently running. It can be started like this:

$ sudo top

After you press Enter, you will see a display as shown in Figure 16.1. The top command has a few interactive commands: pressing h displays the help screen; pressing k prompts you to enter the PID

of a process to kill; pressing n prompts you to enter the PID of a process to change its nice value. The top man page describes other commands and includes a detailed description of what all the columns of information top can display actually represent; have a look at top's well-written man page.

Figure 16.1. You can use the top command to monitor and control processes. Here, we are prompted to renice a process.

[View full size image]

JjS__G^ftjjl^j

Edit ierrrsinal Tafcj yelp top - 1 i:44: ii; up JA ir:n, 2 users, ".oad average: C.fca, 0.66, 0.67 ±

Tasks-. 96 total, 1 running, 95 sleeping, 0 stopped, 0 zorfcie cpuis): 6.4% us, 5.0* sy, 26.5\ m, 44.7** id, wa, o.5\ hi, o. l^ si

Mem; 511164k total, 4BH2i6k used, 22ssgk free, io36isk buffers swap; IJgmOOJk total, 21576k used, 1472423k free, 171120k cached PID to renice:

PID USER PR NI YIRT RES ShTI S %CPU TIH&- COfWND

3523

andreirf

15

0

10554

1172

BSQ

p

2,0

0,2

0:

00,02

top

*

root

16

0

2636

564

476

s

0.0

0-1

0:

00- M

init

2

rûot

KT

0

0

0

0

s

0,0

0,0

C:

OC,00

nu gr ati on/0

3

root

34

19

0

0

0

s

0,0

0,0

01

00,00

ksoftirqd/O

root

Ht

0

0

0

0

£

0,0

0,0

o:

00,00

watchdog/0

5

root

10

5

0

0

0

0-0

0-0

0:

00.11

events/0

6

root

10

-5

0

0

0

s

0,0

0,0

0:

00,01

khelper

7

root

10

■5

0

0

0

s

0.0

0.0

o:

00.00

kthread

10

root

10

-5

0

0

0

£

0,0

0,0

o;

ÛO,

kbUekd/O

il

root

10

"5

0

0

0

s

0-0

0,0

0:

oo.oo

kacpid

147

root

15

0

0

0

0

s

0,0

0,0

0;

00,02

pdflush

Ma

root

15

0

0

0

0

s

0.0

0,0

C ;

00.1&

pdflush

ISO

root

11

■5

0

0

0

s

0,0

0,0

o;

00,00

310/0

root

15

0

0

0

0

%

0-0

0,0

0;

00.36

kswapdO

7JO

root

10

-5

0

0

0

s

0,0

0,0

0:

00,09

kseriod

1378

root

10

■5

0

0

0

s

0,0

0,0

o:

00.00

khut-d

1B99

root

IS

0

0

0

0

%

0,0

0,0

0:

ûo,oo

khpsbpkt

The top command displays quite a bit of information about your system. Processes can be sorted by PID, age, CPU or memory usage, time, or user. This command also provides process management, and system administrators can use its k and r keypress commands to kill and reschedule running tasks, respectively.

The top command uses a fair amount of memory, so you might want to be judicious in its use and not leave it running all the time. When you've finished with it, simply press q to quit top.

Was this article helpful?

0 0

Post a comment