• Node.js Video Tutorials

Node.js - os.setPriority() Method



The Node.js os.setPriority() method will allow setting the scheduling priority of a process specified by the process ID (pid).

The priority is a whole number which specifies the scheduling process priority we want to set. The input priority must be in between −20 (high priority) and to 19 (low priority).

Because POSIX systems priority levels differ from those of Windows systems, Node.js provided us with constants that have different levels of priorities for processes and they are in os.constants.priority constants. Elevated privileges are required to set the process priority to the PRIORITY_HIGHEST on Windows. In case the user that's running the program doesn't have the privileges, then it will be reduced to PRIORITY_HIGH

Syntax

Following is the syntax of the Node.js os.setPriority() method

os.setPriority([pid, ] priority)

Parameters

  • pid − (optional) This is the process ID, a whole number that identifies the running process for which the priority should be set. The default value is 0. This is an optional parameter.

  • priority − (required) The second argument is the priority, a whole integer that represents the scheduling of the process priority that we wish to set. The range goes from −20 for the highest priority to 19, which is the lowest. This is a required parameter.

Return value

This method doesn't return anything instead, it set the scheduling priority of the specified by pid.

Below are the priority constants that are provided by Node.js −

  • PRIORITY_LOW − This is the lowest process scheduling priority. On Windows, this corresponds to IDLE PRIORITY CLASS, and on all other platforms, it has a nice value of 19.

  • PRIORITY_BELOW_NORMAL − On windows, this corresponds to BELOW_NORMAL_PRIORITY_CLASS, and on all other platforms, it has a nice value of 10.

  • PRIORITY_NORMAL − This is the default process scheduling priority and this corresponds to NORMAL_PRIORITY_CLASS on windows. 0 is a nice value on all other platforms.

  • PRIORITY_ABOVE_NORMAL − This corresponds to ABOVE_NORMAL_PRIORITY_CLASS on windows and all other platforms, it has a nice value of -7.

  • PRIORITY_HIGH − On windows, this corresponds to HIGH_PRIORITY_CLASS, and on all other platforms, it has a nice value of -14.

  • PRIORITY_HIGHEST − This is the highest process scheduling priority and this corresponds to REALTIME_PRIORITY_CLASS on windows. −20 is a nice value on all other platforms.

Example

In the following example, we are trying to set the priority for the current process as 0.

const os = require('os');
os.setPriority(0);
console.log(os.getPriority());

Output

/home/cg/root/63a002c52763b/main.js:3
os.setPriority(0);
   ^

TypeError: os.setPriority is not a function
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:4)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the setPriority() method sets the priority as 0 to the current process. As setPriority() doesn't return anything, we logged the getPriority() method and it prints the priority of the current process.

0

Example

In the example below, we are trying to set the priority for the current process as 19.

const os = require('os');
os.setPriority(19);
console.log(os.getPriority());

Error

home/cg/root/63a002c52763b/main.js:3
os.setPriority(19);
   ^

TypeError: os.setPriority is not a function
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:4)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − To get the accurate result, better execute the above code in local.

After executing the above program, 19 sets as a scheduling priority to the pid. Then the. getPriority() method prints the scheduling priority of the current process.

19

Example

Note − In case the user that's running the program doesn't have the privileges, then it will be reduced to PRIORITY_HIGH.

In the following example, we are setting the priority for the current process as -20 by using setPriority() method.

const os = require('os');
os.setPriority(-20);
console.log(os.getPriority());

Error

/home/cg/root/63a002c52763b/main.js:3
os.setPriority(-20);
   ^

TypeError: os.setPriority is not a function
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:4)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the user that's running the above program doesn't have the privileges, and it slowly reduced to PRIORITY_HIGH (-14).

-14
nodejs_os_module.htm
Advertisements