Open In App

Node.js os.setPriority() Method

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The os.setPriority() method is an inbuilt application programming interface of the os module which is used to set the scheduling priority of the process specified by pid and priority.

Syntax: 

os.setPriority(pid, priority)

Parameters: This method has two parameters as mentioned above and described below:  

  • pid: It is an optional parameter. It specifies the process id whose scheduling priority to be set. Its default value is 0.
  • priority: It is a required parameter. It specifies the priority to be set for the process specified process id. Value of this parameter must be between -20(Highest) to 19(Lowest).

Return Value: This method doesn’t returns anything.

Note: As a priority in the Windows system is different from a UNIX system, the priority in the windows system is mapped into one of the six priority constants in os.constants.priority. So, while retrieving the value might be slightly different from the actual value. In the windows system, for setting the highest priority we need elevated users permission. so sometimes PRIORITY_HIGHEST may be changed to PRIORITY_HIGH without any warning.

Below examples illustrate the use of os.setPriority() method in Node.js:

Example 1:  

Javascript




// Node.js program to demonstrate the   
// os.setPriority() Method
  
// Allocating os module
const os = require('os');
  
// Setting priority for the current process
console.log("setting priority for"
    + " the current process to 17");
try{
    // Setting priority of current process
    os.setPriority(17);
}catch(err){
    // Printing error message if any
    console.log(": error occurred"+err);
}


Output: 

setting priority for the current process to 17

Example 2:  

Javascript




// Node.js program to demonstrate the   
// os.setPriority() Method
  
// Allocating os module
const os = require('os');
  
// Setting priority for the current process
os.setPriority(17);
  
try{
    // Printing priority of current process
    console.log(os.getPriority());
}catch(err){
    // Printing error message
    console.log(": error occurred"+err);
}


Output: 

10

Note: The above program will compile and run by using the node filename.js command.

Reference: https://nodejs.org/api/os.html#os_os_setpriority_pid_priority
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads