Scheduling Priorities
All processes have assigned to them an execution priority—an integer value that is dynamically computed and updated on the basis of several different factors. Whenever the CPU is free, the scheduler selects the most favored process to resume executing. The process selected is the one with the lowest-priority number because lower numbers are defined as more favored than higher ones. Multiple processes at the same priority level are placed in the run queue for that priority level. Whenever the CPU is free, the scheduler starts the processes at the head of the lowest-numbered nonempty run queue. When the process at the top of a run queue stops executing, it goes to the end of the line and the next process moves up to the front. After a process begins to run, it continues to execute until it needs to wait for an I/O operation to complete, receives an interrupt signal, or exhausts the maximum execution time slice defined on that system. A typical time slice is 10 milliseconds.
A Unix process has two priority numbers associated with it. One of the priority numbers is its requested execution priority with respect to other processes. This value (its nice number) is set by the process's owner and by root; it appears in the NI column in a ps -l listing. The other priority assigned to a process is the execution priority. This priority is computed and updated dynamically by the operating system, taking into account such factors as the process's nice number, how much CPU time it has had recently, and other processes that are running and their priorities. The execution priority value appears in the PRI column on a ps -l listing.
Although the CPU is the most-watched resource on a system, it is not the only one. Memory use, disk use, I/O activity, and the number of processes all tie together in determining the computer's throughput. For example, suppose you have two groups, A and B. Both groups require large amounts of memory—more than is available when both are running simultaneously. Raising the priority of Group A over Group B might not help if Group B does not fully relinquish the memory it is using. Although the paging system does this over time, the process of swapping a process out to disk can be intensive and can greatly reduce performance. A better alternative might be to completely stop Group B with a signal and then continue it later, when Group A has finished.
Changing the Priority of a Time-Sharing Process with nice
The nice command is supported only for backward compatibility with previous Solaris releases. The priocntl command provides more flexibility in managing processes. The priority of a process is determined by the policies of its scheduling class and by its nice number. Each time-sharing process has a global priority that is calculated by adding the user-supplied priority, which can be influenced by the nice or priocntl commands, and the system-calculated priority.
The execution priority number of a process is assigned by the operating system and is determined by several factors, including its schedule class, how much CPU time it has used, and its nice number. Each time-sharing process starts with a default nice number, which it inherits from its parent process. The nice number is shown in the NI column of the ps report.
A user can lower the priority of a process by increasing its user-supplied priority number. Only the superuser can increase the priority of a process by lowering its nice value. This prevents users from increasing the priorities of their own processes, thereby monopolizing a greater share of the CPU.
Two versions of the nice command are available: the standard version, /usr/bin/nice, and a version that is integrated into the C shell as a C shell built-in. /usr/bin/nice numbers range from 0 to +39 and the default value is 20, while the C-shell built-in version of nice has values that range from –20 to +20. The lower the number, the higher the priority and the faster the process runs.
Use the /usr/bin/nice command as described in Table 5.13 when submitting a program or command.
Table 5.13. Setting Priorities with nice
Command |
Description |
Lowering the Priority of a Process Using /usr/bin/nice |
|
nice <process_name> |
Increases the nice number by 4 units (the default) |
nice -4 <process_name> |
Increases the nice number by 4 units |
nice -10 <process_name> |
Increases the nice number by 10 units |
Increasing the Priority of a Process |
|
nice -n -10 <process_name> |
Raises the priority of the command by lowering the nice number |
As a system administrator, you can use the renice command to change the priority of a process after it has been submitted. The renice command has the following form:
renice priority -n <value> -p <pid>
Use the ps -elf command to find the PID of the process for which you want to change the priority. The process that you want to change in the following example is named largejob:
# ps –elf|grep largejob 9 S 0 8200 4100 0 84 20 f0274e38 193 Jun 04 ? 0:00 largejob
Issue the following command to increase the priority of PID 8200:
renice -n -4 -p 8200
Issuing the ps -elf command again shows the process with a higher priority:
# ps –elf|grep largejob 9 S 0 8200 4100 0 60 16 f0274e38 193 Jun 04 ? 0:00 largejob
Changing the Scheduling Priority of Processes with priocntl
The standard priority scheme has been improved since earlier versions of Solaris as part of its support for real-time processes. Real-time processes are designed to work in application areas in which a nearly immediate response to events is required. These processes are given nearly complete access to all system resources when they are running. Solaris uses time-sharing priority numbers ranging from -20 to 20. Solaris uses the priocntl command, intended as an improvement over the nice command, to modify process priorities. To use priocntl to change a priority on a process, type this:
priocntl -s -p <new-priority> -i pid <process-id>
new-priority is the new priority for the process, and process-id is the PID of the process you want to change.
The following example sets the priority level for process 8200 to -5:
priocntl -s -p -5 -i pid 8200
The following example is used to set the priority (nice value) for every process created by a given parent process:
priocntl -s -p -5 -I ppid 8200
As a result of this command, all processes forked from process 8200 have a priority of -5.
The priority value assigned to a process can be displayed using the ps command, which was described earlier in this chapter.
The functionality of the priocntl command goes much further than what is described in this section. Consult the online manual pages for more information about the priocntl command.
Fair Share Scheduler (FSS) and the Fixed Scheduler (FX)
The Fair Share Scheduler (FSS) in Solaris 10 can be used to control the allocation of resources. The Fixed Scheduler (FX) is a fixed priority scheduler that provides an ensured priority for processes. Neither of these are objectives on the CX-310-200 exam and they are not covered in this chapter.