قالب وردپرس درنا توس
Home / Tips and Tricks / Here's how to set process priorities with good and renice on Linux

Here's how to set process priorities with good and renice on Linux



  Bash shell on Ubuntu laptop

Commands nice and renice let you fine-tune how the kernel treats your processes by customizing its priorities. Read this tutorial to learn how to use them in Linux and Unix-like operating systems like MacOS.

It is a process process

Within your Linux or Unix-like computer, there will be many processes that are already running before you start the program you want to use. The majority of these processes are important parts of Linux itself or supporting processes for your graphic desktop environment. There is a lot behind the scenes. Of course, there are only so many system resources and CPU time to go around. The Linux kernel is the control for all these processes.

It is the core that must decide which processes get attention and resources right now and which ones have to wait. The core is continuously juggling processes and priorities to ensure that the computer runs as smoothly as possible and that all processes receive their appropriate share. Some processes receive preferential treatment. They are so important to the general operation of the computer that their needs must come first, say your browser.

The nice value

One of the criteria used to determine how the kernel treats a process is nice value. Each process has a good value. The nice value is an integer in the range of -1

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

The trick here is that the higher the nice value, the kinder the process to other processes. In other words, a high fine value tells the core that this process is happy to wait. A negative number is the opposite of being nice. The greater the negative fine value, the more is the selfish process. It tries to get as much CPU time as possible, regardless of other processes.

We can use the nice command to set the fine value when a process is launched and we can use renice to adjust the fine value of a running process.

The nice command

We can use the command nice to adjust the fine value of a program when we start it. This allows us to increase or decrease the priority of the process with the kernel in relation to other processes.

Let's assume that a programmer has written a program called ackermann . This calculates the Ackerman functions. It's CPU and memory intensive. The programmer can start the program with the following command:

  ./ ackermann 

  akkermann command in the terminal window

We can use the command to display the current program. Top

top run in a terminal We can see the details of the [program] program top . The fine value is the number in the column "NI". It is set to zero as we would expect.

Let's start over and this time makes it less demanding. We set a fine value of 15 for the program akkermann as follows. Write nice, a space, -15, another space, and then the name of the program you want to start. In our example, our fictitious programmer uses ./ ackermann .

  nice -15 ./ackermann

nice 15 command in terminal window

Be careful note "15" is not negative fifteen. It's positive fifteen. "-" is required to tell nice we pass in a parameter. To enter a negative number, you must enter two "-" characters.

If we start again again, we can see the change in the behavior of akkermann .

  

With a good value of 15, [chimney] does not consume the most CPU time. GNOME and Rhythmbox both use more. We have reined ackermann in one bit.

Now, let's do the reverse and give [akermann a negative fine value. Note the use of two "-" characters. To make an application more selfish and less enjoyable, you must use sudo . Anyone can make their application nicer, but only superusers can make one more selfish.

  sudo nice --10 ./ackermann

nice -10 command in terminal window

Let's run and see what difference has made.

  top 

  top run in a terminal

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

Renice Command

The command renice allows us to adapt the nice value of a running process. We don't have to stop it and restart it with nice . We can enter a new value on flights.

The renice command takes process ID, or PID, for the process as a command line parameter. We can either extract process ID from the column "PID" in top or we can use ps and grip to find it for us, as follows. Of course you write the name of your user instead of dave and the name of the process you are interested in instead of ackermann .

  ps-eude | grabbed the ackermann 

  ps piped through the grip in a terminal window

Now that we have PID we can use this with renice . We will put ackermann back to a nicer behavior with a good value of five. To change the fine value of a running process, you must use sudo . Note that there is no "-" on the parameter 5 . You don't need one for positive speech and you just need one, not two, for negative numbers.

  sudo renice -n 5 2339 

  renice command running in a terminal window

We receive confirmation that renice has changed the fine value. It shows us the old value and the new value.

The core usually does a good job of managing priorities and distributing CPU time and system resources. But if you have a long CPU-intensive task to run and you don't care when it finishes, it will make the computer run a little smoother if you set a higher fine value for that task. It will be nicer for everyone.




Source link