Method of setting process precedence in C language _c language

Source: Internet
Author: User

C language SetPriority () function: Set Program Process Execution priority
header file:

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

To define a function:

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

Function Description: setpriority () can be used to set process execution precedence for processes, process groups, and users. The parameter which has three kinds of numerical values, and the WHO has different definition according to which value.

which the meaning of WHO representatives:
1, prio_process Who for process identification code
2, Prio_pgrp who is the process of group identification code
3, prio_user who for user identification code

Parameter Prio between 20 and 20. Represents a process execution priority, with the lower number representing a higher priority and more frequent execution. This priority defaults to 0, and only the superuser (root) allows this value to be lowered.

return value:
Successful execution returns 0, and if there is an error, the return value is-1, and the reason for the error is in errno.
1, Esrch: parameter which or who may be wrong, and can not find a consistent process
2, Einval: parameter which value error.
3, Eperm: Insufficient permissions, unable to complete the setup
4, Eacces: The general user can not lower priority

C language GetPriority () function: Get program Process Execution priority
header file:

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

To define a function:

int getpriority (int which, int who);

Function Description:
GetPriority () can be used to obtain process execution precedence for processes, process groups, and users. The parameter which has three kinds of numerical values, and the WHO has different definition according to which value.

which the meaning of WHO representatives:
1, prio_process who: for process identification code
2, PRIO_PGRP who: Group identification code for the process
3, Prio_user who: for user identification Code The value returned by this function is between 20 and 20, representing process execution priority, the lower the number representing higher precedence, and more frequent execution.

Return value: Returns the Process execution priority, if there is an error the return value is 1 and the reason for the error is in errno.

Additional note: Because the return value is likely to be-1, you should also check whether errno has an error reason. It is best to clear the errno variable before calling the secondary function.

Error code:
1, Esrch: parameter which or who may be wrong, and can not find a consistent process.
2, Einval: parameter which value error.

C language Nice () function: changing process precedence
header file:

#include <unistd.h>

To define a function:

int Nice (int inc);

Function Description: Nice () is used to change process execution priorities. The larger the parameter Inc value is, the more the precedence is followed, which means that the slower the process executes. Only Superuser can use a negative Inc value, which represents precedence, and the process executes faster.

Return value: Returns 0 if the execution succeeds, otherwise returns-1, the reason for the failure is stored in errno.

Error code:
Eperm: The average user attempts to switch to a negative parameter Inc value to change process precedence.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.