C language to set the priority of the process

  • 2020-04-02 03:18:19
  • OfStack

C language setpriority() function: sets the execution priority of the program process
The header file:


#include <sys/time.h>  #include <sys/resource.h>

Definition function:


int setpriority(int which, int who, int prio);

Setpriority () can be used to set process execution priority for processes, process groups, and users. The parameter which has three values, and the parameter who has different definitions according to which value.

Which who stands for:
1. PRIO_PROCESS who is the process identification code
2. PRIO_PGRP who is the group identification code of the process
3. PRIO_USER who is the user identification number

Parameter prio is between -20 and 20. The lower the value, the higher the priority and the more frequent the execution. This priority is 0 by default, and only the root is allowed to lower this value.

The return value:
If an error occurs, the return value is -1. The reason for the error is in errno.
1. ESRCH: there may be an error in the parameter which or who, and the matching process cannot be found
2. EINVAL: parameter which value is wrong.
3. EPERM: not enough permissions to complete the setting
4. EACCES: ordinary users cannot reduce the priority

C getpriority() function: gets the execution priority of the program process
The header file:


#include <sys/time.h>  #include <sys/resource.h>

Definition function:


int getpriority(int which, int who);

Function description:
Getpriority () can be used to get process execution priority for processes, process groups, and users. The parameter which has three values, and the parameter who has different definitions according to which value.

Which who stands for:
1. PRIO_PROCESS who: is the process identification code
2. PRIO_PGRP who: is the group identification code of the process
3. PRIO_USER who: the value returned by this function is between -20 and 20, which represents the execution priority of the process. The lower the value, the higher the priority, and the more frequent the execution.

Return value: the return process executes the priority, returns -1 if an error occurs and the reason for the error resides in errno.

Note: since the return value is likely to be -1, check errno for any error reasons. It is best to clear the errno variable before calling the next function.

Error code:
1. ESRCH: there may be an error in the parameter which or who, and the matching process cannot be found.
2. EINVAL: parameter which value is wrong.

C nice() function: changes the priority of processes
The header file:


#include <unistd.h>

Definition function:


int nice(int inc);

Nice () is used to change the priority order of process execution. The greater the value of the parameter inc, the lower the priority order, the slower the process execution will be.

Return value: returns 0 on success, -1 on failure, and the reason for the failure is in errno.

Error code:
EPERM: the average user attempts to switch to a negative parameter inc value to change the process priority.


Related articles: