Node.js os.setPriority() Method
Last Updated :
13 Oct, 2021
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
const os = require( 'os' );
console.log( "setting priority for"
+ " the current process to 17" );
try {
os.setPriority(17);
} catch (err){
console.log( ": error occurred" +err);
}
|
Output:
setting priority for the current process to 17
Example 2:
Javascript
const os = require( 'os' );
os.setPriority(17);
try {
console.log(os.getPriority());
} catch (err){
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
Share your thoughts in the comments
Please Login to comment...