Python os.nice() – Increase niceness of process by specified value

Python os.nice()

Python All functions in the os module raise OSError if the filename or path is invalid or inaccessible, or if other arguments are of the correct type but not accepted by the operating system.

The os.nice() method in Python is used to increase the niceness of a process by a specified value.

Nice, or a nice value, is a set of guidelines that the CPU follows when a process wants to get CPU time to perform its work. The range of process niceness is between -20 and 19 (inclusive). A process with a lower nice value is given a higher priority and more CPU time, while a process with a higher nice value is given a lower priority and less CPU time.

Note: The os.nice() method is available only on UNIX platforms. Any user can increase the niceness of a process, but only the superuser can decrease it.
A superuser is a root or administrative user who has all the privileges to run or execute any program in the operating system.

Syntax: os.nice(value)

Parameters:

value: An integer value.

The new niceness of the process is calculated as newniceness = previousniceness + specifiedvalue

Return type: This method returns an integer value representing the new niceness of the process.

Example 1

Use the os.nice() method to improve the niceness of the process

# Python program to explain os.nice() method
  
# importing os module
import os
  
  
# Get the current nice value
# of the process
niceValue = os.nice(0)
  
# Print the current nice value
# of the process
print("Current nice value of the process:", niceValue)
  
# Increase the niceness
# of the process
value = 5
niceValue = os.nice(value)
print("nNiceness of the process increased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
  
# Increase the niceness
# of the process
value=10
niceValue = os.nice(value)
print("nNiceness of the process increased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
  
# Increase the niceness
# of the process
value=10
niceValue = os.nice(value)
print("nNiceness of the process increased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
  
# The maximum possible niceness of a process
# can be 19 so if niceness to be set exceeds
# the maximum limit then it will set to
# the maximum possible niceness i.e 19

Output:

Current nice value of the process: 0

Niceness of the process increased

Current nice value of the process: 5

Niceness of the process increased

Current nice value of the process: 15

Niceness of the process increased

Current nice value of the process: 19

Example 2

Use the os.nice() method to reduce the niceness of the process

# Python program to explain os.nice() method
  
# importing os module
import os
  
  
# Note: Only a superuser can
# decrease a process's niceness
# so run the program as superuser
# to avoid permission related errors
  
# Get the current nice value
# of the process
niceValue = os.nice(0)
  
# Print the Current nice value
# of the process
print("Current nice value of the process:", niceValue)
  
# Decrease the niceness
# of the process
value = -5
niceValue = os.nice(value)
print("nNiceness of the process decreased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
# Decrease the niceness
# of the current process
value = -10
niceValue = os.nice(value)
print("nNiceness of the process decreased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
  
# Decrease the niceness
# of the current process
value = -15
niceValue = os.nice(value)
print("nNiceness of the process decreased")
  
# Print the current nice value
# of the process
print("nCurrent nice value of the process:", niceValue)
  
  
# The minimum possible niceness of a process
# can be -20 so if niceness to be set is
# lower than the minimum limit then
# it will set to the minimum possible
# niceness i.e -20

Output:

Current nice value of the process: 0

Niceness of the process decreased

Current nice value of the process: -5

Niceness of the process decreased

Current nice value of the process: -15

Niceness of the process decreased

Current nice value of the process: -20

Leave a Reply

Your email address will not be published. Required fields are marked *