Chapter 40. Setting the priority for a process with library calls


You can set the priority for a process by using library calls such as nice, setpriority, and sched_setattr.

40.1. Library calls for setting priority

Real-time processes use a different set of library calls to control policy and priority. You can use the nice and setpriority library calls to set the priority of non-real-time processes.

The nice and setpriority functions adjust the nice value of a non-real-time process. The nice value indicates to the scheduler how to order the list of ready-to-run, non-real-time processes to be run on a processor. The processes at the head of the list run before the ones further down the list.

Important

The functions require the inclusion of the sched.h header file. Ensure you always check the return codes from functions.

The scheduler policy and other parameters can be set using the sched_setscheduler() function. Currently, real-time policies have one parameter, sched_priority. This parameter is used to adjust the priority of the process.

The sched_setscheduler() function requires three parameters, in the form: sched_setscheduler(pid_t pid, int policy, const struct sched_param *sp);.

Note

The sched_setscheduler(2) man page lists all possible return values of sched_setscheduler(), including the error codes.

If the process ID is zero, the sched_setscheduler() function acts on the calling process.

The following code excerpt sets the scheduler policy of the current process to the SCHED_FIFO scheduler policy and the priority to 50:

struct sched_param sp = { .sched_priority = 50 };
int ret;

ret = sched_setscheduler(0, SCHED_FIFO, &sp);
if (ret == -1) {
  perror("sched_setscheduler");
  return 1;
}

The sched_setparam() function is used to set the scheduling parameters of a particular process. This can then be verified using the sched_getparam() function.

Unlike the sched_getscheduler() function, which only returns the scheduling policy, the sched_getparam() function returns all scheduling parameters for the given process.

Prerequisites

  • You have administrator privileges.

Procedure

  • Use the following code excerpt to read the priority of a given real-time process and increment it by two:

    struct sched_param sp;
    int ret;
    
    ret = sched_getparam(0, &sp);
    sp.sched_priority += 2;
    ret = sched_setparam(0, &sp);

    If this code were used in a real application, it would need to check the return values from the function and handle any errors appropriately.

    Important

    Be careful with incrementing priorities. Continually adding two to the scheduler priority, as in this example, might eventually lead to an invalid priority.

The sched_setattr() function sets the scheduling policy and its associated attributes for an instance ID specified in PID. When pid=0, sched_setattr() acts on the process and attributes of the calling thread.

Prerequisites

  • You have administrator privileges.

Procedure

  • Call sched_setattr() specifying the process ID on which the call acts and one of the following real-time scheduling policies:

    Real-time scheduling policies

    SCHED_FIFO
    Schedules a first-in and first-out policy.
    SCHED_RR
    Schedules a round-robin policy.
    SCHED_DEADLINE
    Schedules a deadline scheduling policy.

    Linux also supports the following non-real-time scheduling policies:

    Non-real-time scheduling policies

    SCHED_OTHER
    Schedules the standard round-robin time-sharing policy.
    SCHED_BATCH
    Schedules a "batch" style execution of processes.
    SCHED_IDLE

    Schedules very low priority background jobs. SCHED_IDLE can be used only at static priority 0, and the nice value has no influence for this policy.

    This policy is intended for running jobs at extremely low priority (lower than a +19 nice value by using SCHED_OTHER or SCHED_BATCH policies).

Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top