Sei sulla pagina 1di 6

Process Execution Priorities

Objectives:

Learn the basic techniques for managing execution priorities

Set process priorities

Change process priorities

Concepts

Operating systems such as Linux, run multiple processes. It does this by sharing the CPU and other resources among the processes. If one process can use 100% of the CPU, then other processes may become unresponsive.

The top command is used to display processes in decreasing order to their CPU usage as shown below:

in decreasing order to their CPU usage as shown below: The same information is generated by

The same information is generated by the ps command with the –l option and examining the PRI column.

When you have one or a limited number of CPUs , as a system administrator,

When you have one or a limited number of CPUs, as a system administrator, you will have to decide how to share those limited CPU resources among several competing processes. This is generally done by selecting one process for execution and letting it run for a short period (called a timeslice) or until it needs to wait for some event, such as IO to complete. To ensure that important processes do not get starved out by CPU hogs, the selection is done based on a scheduling priority. The NI column in the listing above, shows the scheduling priority or niceness of each process. Niceness generally ranges from -20 to 19, with -20 being the most favorable or highest priority for scheduling and 19 being the least favorable or lowest priority.

Using the ps command to find niceness

You can also display niceness values using the ps command by using the –l option to get a long listing (as mentioned above).

Default niceness

The default niceness at least for processes started by regular users is 0. This is usually the case of the current Linux systems. You can verify the value for your shell and system by running the nice command with no parameters.

[erwin@localhost ~]$ nice

0

Checking default niceness

Checking default niceness Depending on the speed of your system, you may have to increase the

Depending on the speed of your system, you may have to increase the count value to even see a difference in the times. This script uses lots of CPU.

your system, you may have to increase the count value to even see a difference in

Running count1 and top

Running count1 and top Using nice to Set Priorities Important things to note: • Linux systems

Using nice to Set Priorities

Important things to note:

Linux systems use a priority system with 40 priorities, ranging from -20 (highest priority) to 19 (lowest priority)

Processes started by regular users usually have priority 0

The ps command can display the priority (nice or NI level) using the –l option

The nice command displays our default priority

The nice command can also be used to start a process with a different priority. You use the –n or the ( - -adjustment ) option with a positive value to increase the priority value and a negative value to decrease it. Remember that processes with the lowest priority value run at highest scheduling priority, so think of increasing the priority value as being nice to other processes. You need to be superuser (root) to specify negative priority adjustment.

Changing Priorities - renice

If you happen to start a process and realize that it should run at a different priority, there is a way to change it after it has started, using the renice command. You can specify an absolute priority ( and not an adjustment ) for the process or processes to be changed

Examples

Run count1 at increased niceness (lower priority)

[erwin@localhost ~]$ nice –n 10 ./count 500000 gel&

Run count1 at decreased niceness (higher priority)

[erwin@localhost ~]$ nice –n -15 ./count 5000000 emac&

Set the process with pid 3812 to the maximum niceness (lowest priority)

[erwin@localhost ~]$

renice 20 3812

Set the process with pid 3598 to a lower niceness (higher priority)

[erwin@localhost ~]$

renice -15 3598

Change niceness of all bsit’s processes

[erwin@localhost ~]$

renice 15 –u bsit

Activity Exercises.

1. Start two copies of the count1 script at the same time, but give one the maximum niceness of 19. After a second use ps –l to display the process status, including niceness and add an arbitrary sleep to ensure the command sequence finishes after the two count1 script do. In this way, you will not get a new prompt while you are still waiting for the output. Show and demonstrate the whole process. (Use screenshots to support your anwer)

1.1. Which job finished first? (Use screenshots to support your anwer) 1.2. Does the priority you set took place? (Use screenshots to support your anwer)

2.

Start four copies of the count1 script at four different niceness levels (0, 6, 12, and 18) and see

what happens. Demonstrate the procedure and record your observations.

support your anwer)

(Use screenshots to

3. Create the following shell script called eternity in your directory and run it in the background.

eternity in your directory and run it in the background . 3.1. Use ps –l to

3.1. Use ps –l to check the script’s nice level. What is the script’s nice level? (Use screenshots to support your answer) 3.2. Run the script with nice and give it a niceness of 15. Try running it alongside a less nice version and see what the difference is in top. Record your observation. (Use screenshots to support your anwer) 3.3. Try using nice or renice to make a process’ niceness less than 0. What happens when you try to do so?