#include <sched.h> int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); int sched_getscheduler(pid_t pid); struct sched_param { ... int sched_priority; ... }; |
sched_getscheduler() queries the scheduling policy currently applied to the process identified by pid. If pid equals zero, the policy of the calling process will be retrieved.
SCHED_OTHER is the default universal time-sharing scheduler policy used by most processes. SCHED_BATCH is intended for "batch" style execution of processes.SCHED_FIFO and SCHED_RR are intended for special time-critical applications that need precise control over the way in which runnable processes are selected for execution.
Processes scheduled with SCHED_OTHER or SCHED_BATCH must be assigned the static priority 0. Processes scheduled under SCHED_FIFO or SCHED_RR can have a static priority in the range 1 to 99. The system calls sched_get_priority_min() andsched_get_priority_max() can be used to find out the valid priority range for a scheduling policy in a portable way on all POSIX.1-2001 conforming systems.
All scheduling is preemptive: If a process with a higher static priority gets ready to run, the current process will be preempted and returned into its wait list. The scheduling policy only determines the ordering within the list of runnable processes with equal static priority.
Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling on an unprivileged process’s priority for the SCHED_RR and SCHED_FIFO policies. If an unprivileged process has a non-zero RLIMIT_RTPRIO soft limit, then it can change its scheduling policy and priority, subject to the restriction that the priority cannot be set to a value higher than the RLIMIT_RTPRIO soft limit. If the RLIMIT_RTPRIO soft limit is 0, then the only permitted change is to lower the priority. Subject to the same rules, another unprivileged process can also make these changes, as long as the effective user ID of the process making the change matches the real or effective user ID of the target process. See getrlimit(2) for further information on RLIMIT_RTPRIO. Privileged (CAP_SYS_NICE) processes ignore this limit; as with older older kernels, they can make arbitrary changes to scheduling policy and priority.
Memory locking is usually needed for real-time processes to avoid paging delays, this can be done with mlock() or mlockall().
As a non-blocking end-less loop in a process scheduled under SCHED_FIFO or SCHED_RRwill block all processes with lower priority forever, a software developer should always keep available on the console a shell scheduled under a higher static priority than the tested application. This will allow an emergency kill of tested real-time applications that do not block or terminate as expected.
POSIX systems on which sched_setscheduler() and sched_getscheduler() are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
標(biāo)簽 | 描述 |
---|---|
EINVAL | The scheduling policy is not one of the recognized policies, or the parameter param does not make sense for the policy. |
EPERM | The calling process does not have appropriate privileges. |
ESRCH | The process whose ID is pid could not be found. |
Standard Linux is not designed to support hard real-time applications, that is, applications in which deadlines (often much shorter than a second) must be guaranteed or the system will fail catastrophically. Like all general-purpose operating systems, Linux is designed to maximize average case performance instead of worst case performance. Linux’s worst case performance for interrupt handling is much poorer than its average case, its various kernel locks (such as for SMP) produce long maximum wait times, and many of its performance improvement techniques decrease average time by increasing worst-case time. For most situations, that’s what you want, but if you truly are developing a hard real-time application, consider using hard real-time extensions to Linux such as RTLinux (http://www.rtlinux.org) or RTAI (http://www.rtai.org) or use a different operating system designed specifically for hard real-time applications.