Process Execution Priorities
Process Execution Priorities
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:
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, 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.
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
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.
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.
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
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. (Use screenshots to support your anwer) 3. Create the following shell script called eternity in your directory and run it in the background.
3.1. Use ps l to check the scripts nice level. What is the scripts 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?