قالب وردپرس درنا توس
Home / Tips and Tricks / How to set process priorities with nice and renice on Linux

How to set process priorities with nice and renice on Linux



  Bash Shell on Ubuntu Laptop

The commands and allow you to fine-tune how the kernel handles your processes by adjusting their priorities. Read this tutorial to learn how to use it on Linux and Unix-like operating systems like macOS.

It's all a matter of process

Many processes are already running in your Linux or Unix-like computer You start the application you want to use. Most of these processes are important elements of Linux itself or supporting processes for your graphical desktop environment. There is a lot going on behind the scenes. Of course, there are only so many system resources and CPU time. The Linux kernel is the controller for all these processes.

It is the kernel that has to decide which processes are getting attention and resources and which ones have to wait. The kernel constantly juggles processes and priorities to ensure that the computer runs as smoothly as possible and that all processes receive their corresponding share. Some processes are preferred. They are so important to the overall operation of the computer that their needs must come first in front of your browser.

The nice Value

One of the criteria that determines how the kernel handles a process is its good value. Every process has a good value. The nice value is an integer in the range of -1

9 to 20. All standard processes are started with a good value of zero.

The trick here is that the higher the nice value, the nicer the process is on the other processes. In other words, a high value tells the kernel that this process is waiting. A negative number is the opposite of being nice. The greater the negative Nice value, the more the process is selfish . It tries to get as much CPU time as possible, regardless of the other processes.

We can use the command nice to commit the nice value of a process is started and we can use renice to the nice value of an ongoing process adjust.

The nice command

We can use the command nice to adjust the nice value for a program when we start it. This allows us to increase or decrease the priority that the kernel gives to the process compared to the other processes.

Let's say a programmer wrote a program called ackermann . This calculates the Ackerman functions. It is CPU and memory intensive. The programmer can start the program with the following command:

  ./ ackermann 

  ackermann command in the terminal window

We can use the command above to display the running program. 19659013] top

  top runs in a terminal

We can see the details of the program ackermann in above . The nice value is the number in the "NI column". It was set to zero as expected.

Restart it and make it less exhausting this time. We will set a nice value of 15 for the program ackermann as follows. Type nice, a space, -15, another space, and the name of the program you want to start. In our example, our fictitious programmer uses the command ./ackermann .

  nice -15 ./ackermann[1965902115commandintheterminalwindow" width="644" height="55" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

15 "is not negative 15. It is positive fifteen. The "-" is required to tell nice that we are passing a parameter. To display a negative number, you must enter two "-" characters.

If we start again above we can observe the behavioral change of ackermann .

  top 

  top runs in a terminal

With a good value of 15, ackermann does not consume the most CPU time. GNOME and Rhythmbox both use more. We have forced ackermann something.

Now we do the opposite and give ackermann a negative nice value. Note the use of two "-" characters. To make an application more selfish and less nice, you must use sudo . Everyone can make their application more beautiful, but only superusers can make you selfish.

  sudo nice --10 ./ackermann[196590293nice-10InstructionTerminalwindow"width="644"height="75"src="/pagespeed_static/1JiBnMqyl6Sgif"onload="pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"onerror="thisonerror=null;pagespeedlazyLoadImagesloadIfVisibleAndMaybeBeacon(this);"/>

Let's run above and see what difference that has made.

  top 

  top runs in one Terminal

This time ackermann has a good value of -10. It is back in the top line and consumes more CPU time than before.

The command renice

With we can adjust the nice value of a running process to the command renice We do not have to stop it and restart with nice We can set a new value immediately

The command renice [19459005 takes the process ID (PID) of the process as a command line parameter n either extract the process ID from the column "PID" in above or we can find ps and grep as follows for us. Of course, instead of dave enter the name of your user and instead of ackermann the name of the process you are interested in.

  ps -eu dave | grep ackermann 

  ps whipped through grep in a terminal window

Now that we have the PID, we can use it with renice . We will post ackermann on a better behavior with a nice value of five. To change the value for a running process, you must use sudo . Note that parameter 5 does not contain "-". You do not need positive numbers and you only need one, not two for negative numbers.

  sudo renice -n 5 2339 

  renice command executed in a terminal window

We receive a confirmation that Renice changed the beautiful value. It shows us the old value and the new value.

The kernel usually handles priorities and spends CPU time and system resources. However, if you need to do a long, CPU-intensive task and you do not care when it's done, your computer will run a little smoother if you set a higher value for this task. This will be nice for everyone.




Source link