Open In App

Node.js os.setPriority() Method

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:  

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:  




// 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:  




// 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
 


Article Tags :