内容简介
#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_setscheduler() sets both the scheduling policy and the associated parameters for the process identified by%uA0pid. If%uA0pid%uA0equals zero, the scheduler of the calling process will be set. The interpretation of the parameter%uA0paramꃞpends on the selected policy. Currently, the following three scheduling policies are supported under Linux:SCHED_FIFO,%uA0SCHED_RR,%uA0SCHED_OTHER, and%uA0SCHED_BATCH their respective semantics are described below.sched_getscheduler() queries the scheduling policy currently applied to the process identified by%uA0pid. If%uA0pid%uA0equals zero, the policy of the calling process will be retrieved.
调度策略
The scheduler is the kernel part that decides which runnable process will be executed by the CPU next. The Linux scheduler offers three different scheduling policies, one for normal processes and two for real-time applications. A static priority value%uA0sched_priorityis assigned to each process and this value can be changed only via system calls. Conceptually, the scheduler maintains a list of runnable processes for each possiblesched_priority%uA0value, and%uA0sched_priorityꃊn have a value in the range 0 to 99. In order to determine the process that runs next, the Linux scheduler looks for the non-empty list with the highest static priority and takes the process at the head of this list. The scheduling policy determines for each process, where it will be inserted into the list of processes with equal static priority and how it will move inside this list.SCHED_OTHER%uA0is the default universal time-sharing scheduler policy used by most processes.%uA0SCHED_BATCH%uA0is intended for "batch" style execution of processes.SCHED_FIFO%uA0and%uA0SCHED_RR%uA0are intended for special time-critical applications that need precise control over the way in which runnable processes are selected for execution.
Processes scheduled with%uA0SCHED_OTHER%uA0or%uA0SCHED_BATCH%uA0must be assigned the static priority 0. Processes scheduled under%uA0SCHED_FIFO%uA0or%uA0SCHED_RRꃊn have a static priority in the range 1 to 99. The system calls%uA0sched_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.
SCHED_FIFO:先入先出调度
SCHED_FIFOꃊn only be used with static priorities higher than 0, which means that when a%uA0SCHED_FIFO%uA0processes becomes runnable, it will always immediately preempt any currently running%uA0SCHED_OTHER%uA0or%uA0SCHED_BATCH%uA0process.%uA0SCHED_FIFO%uA0is a simple scheduling algorithm without time slicing. For processes scheduled under theSCHED_FIFO%uA0policy, the following rules are applied: A%uA0SCHED_FIFO%uA0process that has been preempted by another process of higher priority will stay at the head of the list for its priority and will resume execution as soon as all processes of higher priority are blocked again. When a%uA0SCHED_FIFO%uA0process becomes runnable, it will be inserted at the end of the list for its priority. A call to%uA0sched_setscheduler() or%uA0sched_setparam() will put the%uA0SCHED_FIFO%uA0(or%uA0SCHED_RR) process identified by%uA0pid%uA0at the start of the list if it was runnable. As a consequence, it may preempt the currently running process if it has the same priority. (POSIX.1-2001 specifies that the process should go to the end of the list.) A process calling%uA0sched_yield() will be put at the end of the list. No other events will move a process scheduled under the%uA0SCHED_FIFO%uA0policy in the wait list of runnable processes with equal static priority. A%uA0SCHED_FIFO%uA0process runs until either it is blocked by an I/O request, it is preempted by a higher priority process, or it calls%uA0sched_yield().SCHED_RR:轮循调度
SCHED_RR%uA0is a simple enhancement of%uA0SCHED_FIFO. Everything described above forSCHED_FIFO%uA0also applies to%uA0SCHED_RR, except that each process is only allowed to run for a maximum time quantum. If a%uA0SCHED_RR%uA0process has been running for a time period equal to or longer than the time quantum, it will be put at the end of the list for its priority. A%uA0SCHED_RR%uA0process that has been preempted by a higher priority process and subsequently resumes execution as a running process will complete the unexpired portion of its round robin time quantum. The length of the time quantum can be retrieved using%uA0sched_rr_get_interval(2).SCHED_OTHER:默认的Linux分时调度
SCHED_OTHERꃊn only be used at static priority 0.%uA0SCHED_OTHER%uA0is the standard Linux time-sharing scheduler that is intended for all processes that do not require special static priority real-time mechanisms. The process to run is chosen from the static priority 0 list based on a dynamic priority that is determined only inside this list. The dynamic priority is based on the nice level (set by%uA0nice(2) or%uA0setpriority(2)) and increased for each time quantum the process is ready to run, but denied to run by the scheduler. This ensures fair progress among all%uA0SCHED_OTHER%uA0processes.SCHED_BATCH:调度批处理
(Since Linux 2.6.16.)%uA0SCHED_BATCHꃊn only be used at static priority 0. This policy is similar to%uA0SCHED_OTHER, except that this policy will cause the scheduler to always assume that the process is CPU-intensive. Consequently, the scheduler will apply a small scheduling penalty so that this process is mildly disfavoured in scheduling decisions. This policy is useful for workloads that are non-interactive, but do not want to lower their nice value, and for workloads that want a deterministic scheduling policy without interactivity causing extra preemptions (between the workload’s tasks).权限和资源限制
In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) processes can set a non-zero static priority. The only change that an unprivileged process can make is to set the%uA0SCHED_OTHER%uA0policy, and this can only be done if the effective user ID of the caller of%uA0sched_setscheduler() matches the real or effective user ID of the target process (i.e., the process specified by%uA0pid) whose policy is being changed.Since Linux 2.6.12, the%uA0RLIMIT_RTPRIO%uA0resource limit defines a ceiling on an unprivileged process’s priority for the%uA0SCHED_RR%uA0and%uA0SCHED_FIFO%uA0policies. If an unprivileged process has a non-zero%uA0RLIMIT_RTPRIO%uA0soft 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%uA0RLIMIT_RTPRIO%uA0soft limit. If the%uA0RLIMIT_RTPRIO%uA0soft 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%uA0getrlimit(2) for further information on%uA0RLIMIT_RTPRIO. Privileged (CAP_SYS_NICE) processes ignore this limit as with older older kernels, they can make arbitrary changes to scheduling policy and priority.
响应时间
A blocked high priority process waiting for the I/O has a certain response time before it is scheduled again. The device driver writer can greatly reduce this response time by using a "slow interrupt" interrupt handler.杂项
Child processes inherit the scheduling algorithm and parameters across a%uA0fork(). The scheduling algorithm and parameters are preserved across%uA0execve(2).Memory locking is usually needed for real-time processes to avoid paging delays, this can be done with%uA0mlock() or%uA0mlockall().
As a non-blocking end-less loop in a process scheduled under%uA0SCHED_FIFO%uA0or%uA0SCHED_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%uA0sched_setscheduler() and%uA0sched_getscheduler() are available define%uA0_POSIX_PRIORITY_SCHEDULING%uA0in <unistd.h>.
返回值
On success,%uA0sched_setscheduler() returns zero. On success,%uA0sched_getscheduler() returns the policy for the process (a non-negative integer). On error, -1 is returned, anderrno%uA0is set appropriately.错误
标签 | 描述 |
---|---|
EINVAL | The scheduling%uA0policy%uA0is not one of the recognized policies, or the parameter%uA0param%uA0does not make sense for the%uA0policy. |
EPERM | The calling process does not have appropriate privileges. |
ESRCH | The process whose ID is%uA0pid%uA0could not be found. |
遵循于
POSIX.1-2001. The%uA0SCHED_BATCH%uA0policy is Linux specific.注意
Standard Linux is a general-purpose operating system and can handle background processes, interactive applications, and soft real-time applications (applications that need to usually meet timing deadlines). This man page is directed at these kinds of applications.Standard Linux is%uA0notꃞsigned 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.