Getpriority() function of os module in Python

Function Name:

getpriority()

Overview:

  • The getpriority() function from the os module of Python returns the scheduling priority of a process.

  • The priority values range between -20 and 20. The default priority of a process is zero. Higher values denote lower priority and the lower values denote higher priority.

Function Signature:

os.getpriority(which, who)

Parameters:

which selects one or more processes.
        If the value passed is os.PRIO_PROCESS the selection is a single process.

        If the value passed is os.PRIO_PGRP the scope of selection is a group of processes.

        If the value passed is os.PRIO_USER the scope of selection includes all the processes of a user.

who - Process id when os.PRIO_PROCESS is the first parameter. If process id is zero it denotes the current process.

         Process group id when os.PRIO_PGRP is the first parameter. If the value sent is zero it denotes the process group of the

         current process.

            The user id when os.PRIO_USER is the first parameter. If the value sent is zero it denotes the user id of the

         current process.

Example 1:

# Example Python program that uses the function getpriority()
# of the os module to retrieve the scheduling priority of
# a process.

# As setpriority() is also used in this program,
# depending upon the OS settings the program needs to be run
# as a privileged user.
# e.g., sudo nice_val.py
import os

niceVal = os.getpriority(os.PRIO_PROCESS, 0)
print("Default priority of the parent process:%d"%niceVal)

# Increase the priority of the current process
os.setpriority(os.PRIO_PROCESS, 0, -1)

# Create a child process
retVal = os.fork()

if retVal == 0:
    childPID = os.getpid()
    # Assign default priority to the new process.
    # Commenting out the setpriority() below will make
    # the child process to inherit the parent priority
    os.setpriority(os.PRIO_PROCESS, childPID, 0)
    print("Process id of the child %d"%childPID)

# Let both parent and child processes print their modified priorities    
niceVal = os.getpriority(os.PRIO_PROCESS, 0)
print("pid:%d priority:%d"%(os.getpid(), niceVal))

Output:

Default priority of the parent process:0
pid:82364 priority:-1
Process id of the child 82365
pid:82365 priority:0

Example 2:

# Example Python program that uses getpriority()
# to get the highest priority amongst the processes
# of a process group

import os

print("From parent - process group id:%d"%os.getpgid(os.getpid()))
print("From parent - process id:%d"%os.getpid())
# Change the priortiy of the parent process
os.setpriority(os.PRIO_PROCESS, 0, -5)

# Fork and create a group of two processes
retVal = os.fork()
if retVal == 0:
    print("From child - process group id:%d"%os.getpgid(os.getpid()))    
    print("From child - process id:%d"%os.getpid())
    # Change the priortiy of the parent child process

    os.setpriority(os.PRIO_PROCESS,0, -3)
    # Returns the highest priority
    print("From child - highest priority in group:%d"%os.getpriority(os.PRIO_PGRP, 0))
else:
    print("From parent - highest priority in group:%d"%os.getpriority(os.PRIO_PGRP, 0))

Output:

From parent - process group id:3480
From parent - process id:3481
From parent - highest priority in group:-5
From child - process group id:3480
From child - process id:3482
From child - highest priority in group:-5

 


Copyright 2024 © pythontic.com