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() # As setpriority() is also used in this program, niceVal = os.getpriority(os.PRIO_PROCESS, 0) # Increase the priority of the current process # Create a child process if retVal == 0: # Let both parent and child processes print their modified priorities |
Output:
Default priority of the parent process:0 |
Example 2:
# Example Python program that uses getpriority() import os print("From parent - process group id:%d"%os.getpgid(os.getpid())) # Fork and create a group of two processes os.setpriority(os.PRIO_PROCESS,0, -3) |
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 |