Next: , Previous: , Up: Priority   [Contents][Index]


22.3.3 Basic Scheduling Functions

This section describes functions in the GNU C Library for setting the absolute priority and scheduling policy of a process.

Portability Note: On systems that have the functions in this section, the macro _POSIX_PRIORITY_SCHEDULING is defined in <unistd.h>.

For the case that the scheduling policy is traditional scheduling, more functions to fine tune the scheduling are in Traditional Scheduling.

Don’t try to make too much out of the naming and structure of these functions. They don’t match the concepts described in this manual because the functions are as defined by POSIX.1b, but the implementation on systems that use the GNU C Library is the inverse of what the POSIX structure contemplates. The POSIX scheme assumes that the primary scheduling parameter is the scheduling policy and that the priority value, if any, is a parameter of the scheduling policy. In the implementation, though, the priority value is king and the scheduling policy, if anything, only fine tunes the effect of that priority.

The symbols in this section are declared by including file sched.h.

Data Type: struct sched_param

This structure describes an absolute priority.

int sched_priority

absolute priority value

Function: int sched_setscheduler (pid_t pid, int policy, const struct sched_param *param)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function sets both the absolute priority and the scheduling policy for a process.

It assigns the absolute priority value given by param and the scheduling policy policy to the process with Process ID pid, or the calling process if pid is zero. If policy is negative, sched_setscheduler keeps the existing scheduling policy.

The following macros represent the valid values for policy:

SCHED_OTHER

Traditional Scheduling

SCHED_FIFO

First In First Out

SCHED_RR

Round Robin

On success, the return value is 0. Otherwise, it is -1 and ERRNO is set accordingly. The errno values specific to this function are:

EPERM
  • The calling process does not have CAP_SYS_NICE permission and policy is not SCHED_OTHER (or it’s negative and the existing policy is not SCHED_OTHER.
  • The calling process does not have CAP_SYS_NICE permission and its owner is not the target process’ owner. I.e., the effective uid of the calling process is neither the effective nor the real uid of process pid.
ESRCH

There is no process with pid pid and pid is not zero.

EINVAL
  • policy does not identify an existing scheduling policy.
  • The absolute priority value identified by *param is outside the valid range for the scheduling policy policy (or the existing scheduling policy if policy is negative) or param is null. sched_get_priority_max and sched_get_priority_min tell you what the valid range is.
  • pid is negative.
Function: int sched_getscheduler (pid_t pid)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the scheduling policy assigned to the process with Process ID (pid) pid, or the calling process if pid is zero.

The return value is the scheduling policy. See sched_setscheduler for the possible values.

If the function fails, the return value is instead -1 and errno is set accordingly.

The errno values specific to this function are:

ESRCH

There is no process with pid pid and it is not zero.

EINVAL

pid is negative.

Note that this function is not an exact mate to sched_setscheduler because while that function sets the scheduling policy and the absolute priority, this function gets only the scheduling policy. To get the absolute priority, use sched_getparam.

Function: int sched_setparam (pid_t pid, const struct sched_param *param)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function sets a process’ absolute priority.

It is functionally identical to sched_setscheduler with policy = -1.

Function: int sched_getparam (pid_t pid, struct sched_param *param)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns a process’ absolute priority.

pid is the Process ID (pid) of the process whose absolute priority you want to know.

param is a pointer to a structure in which the function stores the absolute priority of the process.

On success, the return value is 0. Otherwise, it is -1 and ERRNO is set accordingly. The errno values specific to this function are:

ESRCH

There is no process with pid pid and it is not zero.

EINVAL

pid is negative.

Function: int sched_get_priority_min (int policy)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the lowest absolute priority value that is allowable for a process with scheduling policy policy.

On Linux, it is 0 for SCHED_OTHER and 1 for everything else.

On success, the return value is 0. Otherwise, it is -1 and ERRNO is set accordingly. The errno values specific to this function are:

EINVAL

policy does not identify an existing scheduling policy.

Function: int sched_get_priority_max (int policy)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the highest absolute priority value that is allowable for a process that with scheduling policy policy.

On Linux, it is 0 for SCHED_OTHER and 99 for everything else.

On success, the return value is 0. Otherwise, it is -1 and ERRNO is set accordingly. The errno values specific to this function are:

EINVAL

policy does not identify an existing scheduling policy.

Function: int sched_rr_get_interval (pid_t pid, struct timespec *interval)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the length of the quantum (time slice) used with the Round Robin scheduling policy, if it is used, for the process with Process ID pid.

It returns the length of time as interval.

With a Linux kernel, the round robin time slice is always 150 microseconds, and pid need not even be a real pid.

The return value is 0 on success and in the pathological case that it fails, the return value is -1 and errno is set accordingly. There is nothing specific that can go wrong with this function, so there are no specific errno values.

Function: int sched_yield (void)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function voluntarily gives up the process’ claim on the CPU.

Technically, sched_yield causes the calling process to be made immediately ready to run (as opposed to running, which is what it was before). This means that if it has absolute priority higher than 0, it gets pushed onto the tail of the queue of processes that share its absolute priority and are ready to run, and it will run again when its turn next arrives. If its absolute priority is 0, it is more complicated, but still has the effect of yielding the CPU to other processes.

If there are no other processes that share the calling process’ absolute priority, this function doesn’t have any effect.

To the extent that the containing program is oblivious to what other processes in the system are doing and how fast it executes, this function appears as a no-op.

The return value is 0 on success and in the pathological case that it fails, the return value is -1 and errno is set accordingly. There is nothing specific that can go wrong with this function, so there are no specific errno values.


Next: , Previous: , Up: Priority   [Contents][Index]