鍍金池/ 教程/ Linux/ sched_setscheduler()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
getpeername()函數(shù) Unix/Linux
io_setup()函數(shù) Unix/Linux
mknod()函數(shù) Unix/Linux
arch_prctl()函數(shù) Unix/Linux
inotify_add_watch()函數(shù) Unix/Linux
chmod()函數(shù) Unix/Linux
_exit()函數(shù) Unix/Linux
epoll_ctl()函數(shù) Unix/Linux
Unix/Linux系統(tǒng)調(diào)用
futimesat()函數(shù) Unix/Linux
oldolduname()函數(shù) Unix/Linux
sched_rr_get_interval()函數(shù) Unix/Linux
oldstat()函數(shù) Unix/Linux
pselect()函數(shù) Unix/Linux
close()函數(shù) Unix/Linux
add_key()函數(shù) Unix/Linux
clone()函數(shù) Unix/Linux
rename()函數(shù) Unix/Linux
msgop()函數(shù) Unix/Linux
getrlimit()函數(shù) Unix/Linux
linkat()函數(shù) Unix/Linux
ioprio_set()函數(shù) Unix/Linux
insw()函數(shù) Unix/Linux
munlock()函數(shù) Unix/Linux
getgid()函數(shù) Unix/Linux
chdir()函數(shù) Unix/Linux
rmdir()函數(shù) Unix/Linux
ioctl()函數(shù) Unix/Linux
lchown()函數(shù) Unix/Linux
execve()函數(shù) Unix/Linux
epoll_wait()函數(shù) Unix/Linux
fstatat()函數(shù) Unix/Linux
truncate()函數(shù) Unix/Linux
lock()函數(shù) Unix/Linux
outsb()函數(shù) Unix/Linux
chroot()函數(shù) Unix/Linux
kexec_load()函數(shù) Unix/Linux
afs_syscall()函數(shù) Unix/Linux
dup2()函數(shù) Unix/Linux
_newselect()函數(shù) Unix/Linux
inotify_rm_watch()函數(shù) Unix/Linux
recv()函數(shù) Unix/Linux
msgsnd()函數(shù) Unix/Linux
getdtablesize()函數(shù) Unix/Linux
ioperm()函數(shù) Unix/Linux
munmap()函數(shù) Unix/Linux
getpriority()函數(shù) Unix/Linux
sched_get_priority_min()函數(shù) Unix/Linux
outw_p()函數(shù) Unix/Linux
kill()函數(shù) Unix/Linux
lookup_dcookie()函數(shù) Unix/Linux
accept()函數(shù) Unix/Linux
obsolete()函數(shù) Unix/Linux
pread()函數(shù) Unix/Linux
getgroups()函數(shù) Unix/Linux
personality()函數(shù) Unix/Linux
recvfrom()函數(shù) Unix/Linux
putpmsg()函數(shù) Unix/Linux
getgroups()函數(shù) Unix/Linux
remap_file_pages()函數(shù) Unix/Linux
request_key()函數(shù) Unix/Linux
mount()函數(shù) Unix/Linux
sched_setscheduler()函數(shù) Unix/Linux
mkdirat()函數(shù) Unix/Linux
nfsservctl()函數(shù) Unix/Linux
getuid()函數(shù) Unix/Linux
prof()函數(shù) Unix/Linux
exit_group函數(shù) Unix/Linux
io_cancel()函數(shù) Unix/Linux
mmap()函數(shù) Unix/Linux
mremap()函數(shù) Unix/Linux
getitimer()函數(shù) Unix/Linux
unimplemented()函數(shù) Unix/Linux
read()函數(shù) Unix/Linux
inl()函數(shù) Unix/Linux
posix_fadvise()函數(shù) Unix/Linux
open()函數(shù) Unix/Linux
listen()函數(shù) Unix/Linux
io_destroy()函數(shù) Unix/Linux
acct()函數(shù) Unix/Linux
fcntl()函數(shù) Unix/Linux
recvmsg()函數(shù) Unix/Linux
statfs()函數(shù) Unix/Linux
oldlstat()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
getpagesize()函數(shù) Unix/Linux
sched_yield()函數(shù) Unix/Linux
outw()函數(shù) Unix/Linux
inw_p()函數(shù) Unix/Linux
setpgid()函數(shù) Unix/Linux
quotactl()函數(shù) Unix/Linux
prctl()函數(shù) Unix/Linux
fattach()函數(shù) Unix/Linux
readahead()函數(shù) Unix/Linux
msgget()函數(shù) Unix/Linux
gethostname()函數(shù) Unix/Linux
semctl()函數(shù) Unix/Linux
munlockall()函數(shù) Unix/Linux
iopl()函數(shù) Unix/Linux
fdetach()函數(shù) Unix/Linux
getuid()函數(shù) Unix/Linux
gettid()函數(shù) Unix/Linux
pwrite()函數(shù) Unix/Linux
isastream()函數(shù) Unix/Linux
get_thread_area()函數(shù) Unix/Linux
mpx()函數(shù) Unix/Linux
io_getevents()函數(shù) Unix/Linux
lstat()函數(shù) Unix/Linux
readv()函數(shù) Unix/Linux
exit()函數(shù) Unix/Linux
inw()函數(shù) Unix/Linux
fchmod()函數(shù) Unix/Linux
mq_getsetattr()函數(shù) Unix/Linux
get_robust_list()函數(shù) Unix/Linux
_llseek()函數(shù) Unix/Linux
gettimeofday()函數(shù) Unix/Linux
nice()函數(shù) Unix/Linux
security()函數(shù) Unix/Linux
sched_get_priority_max()函數(shù) Unix/Linux
outb_p()函數(shù) Unix/Linux
intro()函數(shù) Unix/Linux
alarm()函數(shù) Unix/Linux
putmsg()函數(shù) Unix/Linux
fork()函數(shù) Unix/Linux
getpgrp()函數(shù) Unix/Linux
link()函數(shù) Unix/Linux
getcwd()函數(shù) Unix/Linux
insl()函數(shù) Unix/Linux
getsockname()函數(shù) Unix/Linux
dup()函數(shù) Unix/Linux
connect()函數(shù) Unix/Linux
select()函數(shù) Unix/Linux
futex()函數(shù) Unix/Linux
outsw()函數(shù) Unix/Linux
query_module()函數(shù) Unix/Linux
reboot()函數(shù) Unix/Linux
create_module()函數(shù) Unix/Linux
adjtimex()函數(shù) Unix/Linux
mlock()函數(shù) Unix/Linux
statvfs()函數(shù) Unix/Linux
gtty()函數(shù) Unix/Linux
epoll_create()函數(shù) Unix/Linux
bind()函數(shù) Unix/Linux
inl_p()函數(shù) Unix/Linux
select_tut()函數(shù) Unix/Linux
mincore()函數(shù) Unix/Linux
getresuid()函數(shù) Unix/Linux
getpmsg()函數(shù) Unix/Linux
getcontext()函數(shù) Unix/Linux
killpg()函數(shù) Unix/Linux
olduname()函數(shù) Unix/Linux
openat()函數(shù) Unix/Linux
pause()函數(shù) Unix/Linux
alloc_hugepages()函數(shù) Unix/Linux
ioctl_list()函數(shù) Unix/Linux
readlinkat()函數(shù) Unix/Linux
mprotect()函數(shù) Unix/Linux
getdomainname()函數(shù) Unix/Linux
readlink()函數(shù) Unix/Linux
madvise()函數(shù) Unix/Linux
bdflush()函數(shù) Unix/Linux
ipc()函數(shù) Unix/Linux
ptrace()函數(shù) Unix/Linux
getrusage()函數(shù) Unix/Linux
msync()函數(shù) Unix/Linux
faccessat()函數(shù) Unix/Linux
modify_ldt()函數(shù) Unix/Linux
get_kernel_syms()函數(shù) Unix/Linux
getpid()函數(shù) Unix/Linux
fsync()函數(shù) Unix/Linux
msgctl()函數(shù) Unix/Linux
break未實(shí)現(xiàn) Unix/Linux
sched_setparam()函數(shù) Unix/Linux
fchown()函數(shù) Unix/Linux
cacheflush()函數(shù) Unix/Linux
fdatasync()函數(shù) Unix/Linux
flock()函數(shù) Unix/Linux
pivot_root()函數(shù) Unix/Linux
inotify_init()函數(shù) Unix/Linux
fchmodat()函數(shù) Unix/Linux
keyctl()函數(shù) Unix/Linux
llseek()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
pipe()函數(shù) Unix/Linux
io_submit()函數(shù) Unix/Linux
ppoll()函數(shù) Unix/Linux
multiplexer()函數(shù) Unix/Linux
access()函數(shù) Unix/Linux
sched_getparam()函數(shù) Unix/Linux
chown()函數(shù) Unix/Linux
readdir()函數(shù) Unix/Linux
inb_p()函數(shù) Unix/Linux
outsl()函數(shù) Unix/Linux
nanosleep()函數(shù) Unix/Linux
alloc_hugepages()函數(shù) Unix/Linux
sbrk()函數(shù) Unix/Linux
move_pages()函數(shù) Unix/Linux
fchdir()函數(shù) Unix/Linux
renameat()函數(shù) Unix/Linux
getppid()函數(shù) Unix/Linux
getsid()函數(shù) Unix/Linux
oldfstat()函數(shù) Unix/Linux
lseek()函數(shù) Unix/Linux
stat()函數(shù) Unix/Linux
path_resolution()函數(shù) Unix/Linux
getunwind()函數(shù) Unix/Linux
getsockopt()函數(shù) Unix/Linux
mmap2()函數(shù) Unix/Linux
getdents()函數(shù) Unix/Linux
fchownat()函數(shù) Unix/Linux
mkdir()函數(shù) Unix/Linux
perfmonctl()函數(shù) Unix/Linux
open()函數(shù) Unix/Linux
idle()函數(shù) Unix/Linux
poll()函數(shù) Unix/Linux
brk()函數(shù) Unix/Linux
sched_setaffinity()函數(shù) Unix/Linux
mlockall()函數(shù) Unix/Linux

sched_setscheduler()函數(shù) Unix/Linux

sched_setscheduler, sched_getscheduler - 設(shè)置和獲取調(diào)度算法/參數(shù)

內(nèi)容簡(jiǎn)介

#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 pid. If pid equals zero, the scheduler of the calling process will be set. The interpretation of the parameter param depends on the selected policy. Currently, the following three scheduling policies are supported under Linux:SCHED_FIFOSCHED_RRSCHED_OTHER, and SCHED_BATCH; their respective semantics are described below.

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.

調(diào)度策略

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 sched_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 value, and sched_priority can 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 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.

SCHED_FIFO:先入先出調(diào)度

SCHED_FIFO can only be used with static priorities higher than 0, which means that when a SCHED_FIFO processes becomes runnable, it will always immediately preempt any currently running SCHED_OTHER or SCHED_BATCH process. SCHED_FIFO is a simple scheduling algorithm without time slicing. For processes scheduled under theSCHED_FIFO policy, the following rules are applied: A SCHED_FIFO process 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 SCHED_FIFO process becomes runnable, it will be inserted at the end of the list for its priority. A call to sched_setscheduler() or sched_setparam() will put the SCHED_FIFO (or SCHED_RR) process identified by pid at 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 sched_yield() will be put at the end of the list. No other events will move a process scheduled under the SCHED_FIFO policy in the wait list of runnable processes with equal static priority. A SCHED_FIFO process runs until either it is blocked by an I/O request, it is preempted by a higher priority process, or it calls sched_yield().

SCHED_RR:輪循調(diào)度

SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above forSCHED_FIFO also applies to SCHED_RR, except that each process is only allowed to run for a maximum time quantum. If a SCHED_RR process 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 SCHED_RR process 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 sched_rr_get_interval(2).

SCHED_OTHER:默認(rèn)的Linux分時(shí)調(diào)度

SCHED_OTHER can only be used at static priority 0. SCHED_OTHER is 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 nice(2) or setpriority(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 SCHED_OTHER processes.

SCHED_BATCH:調(diào)度批處理

(Since Linux 2.6.16.) SCHED_BATCH can only be used at static priority 0. This policy is similar to SCHED_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).

權(quán)限和資源限制

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 SCHED_OTHER policy, and this can only be done if the effective user ID of the caller of sched_setscheduler() matches the real or effective user ID of the target process (i.e., the process specified by pid) whose policy is being changed.

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.

響應(yīng)時(shí)間

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.

雜項(xiàng)

Child processes inherit the scheduling algorithm and parameters across a fork(). The scheduling algorithm and parameters are preserved across execve(2).

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>.

返回值

On success, sched_setscheduler() returns zero. On success, sched_getscheduler() returns the policy for the process (a non-negative integer). On error, -1 is returned, anderrno is set appropriately.

錯(cuò)誤

標(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.

遵循于

POSIX.1-2001. The SCHED_BATCH policy 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 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.

另請(qǐng)參閱

Programming for the real world - POSIX.4 by Bill O. Gallmeister, O’Reilly & Associates, Inc., ISBN 1-56592-074-0