sched_setattr
Section: System Calls (2)
Updated: 2026-02-08
Index
JM Home Page
roff page
名前
sched_setattr, sched_getattr - スケジューリングポリシーと属性の設定と取得を行なう
ライブラリ
標準 C ライブラリ (libc,~-lc)
書式
#include <sched.h> /* Definition of SCHED_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
int syscall(SYS_sched_setattr, pid_t pid, struct sched_attr *attr,
unsigned int flags);
int syscall(SYS_sched_getattr, pid_t pid, struct sched_attr *attr,
unsigned int size, unsigned int flags);
Note: glibc provides no wrappers for these system calls, necessitating
the use of syscall(2).
説明
sched_setattr()
sched_setattr() システムコールは、 pid で指定された ID
を持つスレッドのスケジューリングポリシーと関連する属性を設定する。 pid が 0
の場合、呼び出したスレッド自身のスケジューリングポリシーと属性が設定される。
現在のところ、 Linux では、 以下の「通常」の (つまり、リアルタイムではない) スケジューリングポリシーが、 policy
に指定できる値としてサポートされている。
- SCHED_OTHER
-
標準の、ラウンドロビンによる時分割型のスケジューリングポリシー。
- SCHED_BATCH
-
「バッチ」形式でのプロセスの実行用。
- SCHED_IDLE
-
「非常に」低い優先度で動作するバックグラウンドジョブ用。
どの実行可能スレッドを選択するかについて、より正確な制御を必要とする 時間の制約が厳しい特別なアプリケーション用として、
いろいろな「リアルタイム」ポリシーもサポートされている。 プロセスがこれらのポリシーをいつ使用できるかを決めるルールについては、sched(7)
を参照。 policy には以下のリアルタイムポリシーを指定できる。
- SCHED_FIFO
-
ファーストイン、ファーストアウト型のポリシー。
- SCHED_RR
-
ラウンドロビン型のポリシー。
Linux では以下のポリシーも提供されている。
- SCHED_DEADLINE
-
デッドライン (応答期限) ベースのスケジューリングポリシー。詳細は sched(7) を参照。
attr 引数は、 指定したスレッドの新しいスケジューリングポリシーと属性を定義した構造体へのポインターである。 この構造体は以下の形式である。
struct sched_attr {
u32 size; /* Size of this structure */
u32 sched_policy; /* Policy (SCHED_*) */
u64 sched_flags; /* Flags */
s32 sched_nice; /* Nice value (SCHED_OTHER,
SCHED_BATCH) */
u32 sched_priority; /* Static priority (SCHED_FIFO,
SCHED_RR) */
/* For SCHED_DEADLINE */
u64 sched_runtime;
u64 sched_deadline;
u64 sched_period;
/* Utilization hints */
u32 sched_util_min;
u32 sched_util_max;
};
構造体 sched_attr のフィールドは以下の通りである。
- size
-
このフィールドには、 構造体のバイト単位のサイズを設定する。 sizeof(struct sched_attr) を指定すればよい。
指定された構造体がカーネル構造体よりも小さい場合、 追加となるフィールドは 0 とみなされる。 指定された構造体がカーネル構造体よりも大きい場合、
カーネルは追加のフィールドが 0 であるかを検査する。 0 でない場合は sched_setattr() はエラー E2BIG
で失敗するので、 size をカーネル構造体のサイズに更新する必要がある。
-
ユーザー空間の sched_attr 構造体のサイズがカーネル構造体のサイズと一致しなかった場合の上記の動作は、
このインターフェースを将来拡張できるようにするためである。 サイズが大きい構造体を渡す行儀の良くないアプリケーションも、 将来カーネルの
sched_attr 構造体のサイズが大きくなったとしてもおかしくならない。 この仕組みにより、 将来的には、 大きなユーザー空間
sched_attr 構造体があることを知っているアプリケーションで、
大きいサイズの構造体に対応していない古いカーネル上で動作しているかを判定することができる。
- sched_policy
-
このフィールドはスケジューリングポリシーを指定する。 上記のリストにある SCHED_* 値のいずれかを指定する。
- sched_flags
-
This field contains zero or more of the following flags that are ORed
together to control scheduling behavior:
-
SCHED_FLAG_RESET_ON_FORK-
Children created by fork(2) do not inherit privileged scheduling
policies. See sched(7) for details.
- SCHED_FLAG_RECLAIM (Linux 4.13 以降)
-
This flag allows a SCHED_DEADLINE thread to reclaim bandwidth unused by
other real-time threads.
- SCHED_FLAG_DL_OVERRUN (Linux 4.16 以降)
-
This flag allows an application to get informed about run-time overruns in
SCHED_DEADLINE threads. Such overruns may be caused by (for example)
coarse execution time accounting or incorrect parameter assignment.
Notification takes the form of a SIGXCPU signal which is generated on
each overrun.
-
This SIGXCPU signal is process-directed (see signal(7)) rather
than thread-directed. This is probably a bug. On the one hand,
sched_setattr() is being used to set a per-thread attribute. On the
other hand, if the process-directed signal is delivered to a thread inside
the process other than the one that had a run-time overrun, the application
has no way of knowing which thread overran.
- SCHED_FLAG_UTIL_CLAMP_MIN
-
- SCHED_FLAG_UTIL_CLAMP_MAX (両方とも Linux 5.3 以降)
-
These flags indicate that the sched_util_min or sched_util_max fields,
respectively, are present, representing the expected minimum and maximum
utilization of the thread.
-
The utilization attributes provide the scheduler with boundaries within
which it should schedule the thread, potentially informing its decisions
regarding task placement and frequency selection.
- sched_nice
-
このフィールドは、 sched_policy に SCHED_OTHER か SCHED_BATCH が指定された場合に設定される
nice 値を指定する。 nice 値は -20 (高優先度) から +19 (低優先度) の範囲の数値である。 sched(7) を参照。
- sched_priority
-
このフィールドは、 sched_policy に SCHED_FIFO か SCHED_RR
が指定された場合に設定される静的優先度を指定する。 これらのポリシーで指定できる優先度の範囲は、
sched_get_priority_min(2) と sched_get_priority_max(2) を使って判定できる。
他のポリシーでは、 このフィールドには 0 を指定しなければならない。
- sched_runtime
-
このフィールドは、 デッドラインスケジューリングの "Runtime" パラメーターを指定する。 この値はナノ秒単位で表現される。 このフィールドと次の
2 つのフィールドは SCHED_DEADLINE スケジューリングにおいてのみ使用される。 詳細は sched(7) を参照。
- sched_deadline
-
このフィールドは、 デッドラインスケジューリングの "Deadline" パラメーターを指定する。 この値はナノ秒単位で表現される。
- sched_period
-
このフィールドは、 デッドラインスケジューリングの "Period" パラメーターを指定する。 この値はナノ秒単位で表現される。
- sched_util_min
-
- sched_util_max (両方とも Linux 5.3 以降)
-
These fields specify the expected minimum and maximum utilization,
respectively. They are ignored unless their corresponding
SCHED_FLAG_UTIL_CLAMP_MIN or SCHED_FLAG_UTIL_CLAMP_MAX is set in
sched_flags.
-
Utilization is a value in the range [0, 1024], representing the percentage
of CPU time used by a task when running at the maximum frequency on the
highest capacity CPU of the system. This is a fixed point representation,
where 1024 corresponds to 100%, and 0 corresponds to 0%. For example, a 20%
utilization task is a task running for 2ms every 10ms at maximum frequency
and is represented by a utilization value of 0.2~*~1024~=~205.
-
A task with a minimum utilization value larger than 0 is more likely
scheduled on a CPU with a capacity big enough to fit the specified value. A
task with a maximum utilization value smaller than 1024 is more likely
scheduled on a CPU with no more capacity than the specified value.
-
A task utilization boundary can be reset by setting its field to
UINT32_MAX (since Linux 5.11).
flags 引数は、このインターフェースの将来の拡張のために用意されている。 現在の実装では 0 を指定しなければならない。
sched_getattr()
sched_getattr() システムコールは、 pid で指定された ID
を持つスレッドのスケジューリングポリシーと関連する属性を取得する。 pid が 0
の場合、呼び出したスレッド自身のスケジューリングポリシーと関連する属性を取得する。
size 引数には、 ユーザー空間での sched_attr 構造体の大きさを設定する。 この値は、 少なくとも初期バージョンの
sched_attr 構造体のサイズでなければならない。 そうでなかった場合、 エラー EINVAL で呼び出しが失敗する。
取得したスケジューリング属性は、 attr が指す sched_attr 構造体の各フィールドに格納される。 カーネルは
attr.size に sched_attr 構造体のサイズを設定する。
If the caller-provided attr buffer is larger than the kernel's
sched_attr structure, the additional bytes in the user-space structure
are not touched. If the caller-provided structure is smaller than the
kernel sched_attr structure, the kernel will silently not return any
values which would be stored outside the provided space. As with
sched_setattr(), these semantics allow for future extensibility of the
interface.
flags 引数は、このインターフェースの将来の拡張のために用意されている。 現在の実装では 0 を指定しなければならない。
返り値
On success, sched_setattr() and sched_getattr() return 0. On error,
-1 is returned, and errno is set to indicate the error.
エラー
sched_getattr() と sched_setattr() の両方が以下の理由で失敗する。
- EINVAL
-
attr が NULL である。 pid が負である。 flags が 0 以外である。
- ESRCH
-
ID が pid のスレッドが見つからなかった。
さらに、 sched_getattr() は以下の理由でも失敗する。
- E2BIG
-
size と attr で指定されたバッファーが小さすぎる。
- EINVAL
-
size が無効である。つまり、 最初のバージョンの sched_attr 構造体 (48 バイト) よりも小さいか、
システムのページサイズよりも大きい。
さらに、 sched_setattr() は以下の理由でも失敗する。
- E2BIG
-
size と attr で指定されたバッファーがカーネル構造体よりも大きく、 一つ以上の超過バイトが 0 でなかった。
- EBUSY
-
SCHED_DEADLINE の流入制御の失敗については sched(7) を参照。
- EINVAL
-
attr.sched_policy が認識できるポリシーではない。
- EINVAL
-
attr.sched_flags contains a flag other than SCHED_FLAG_RESET_ON_FORK.
- EINVAL
-
attr.sched_priority is invalid.
- EINVAL
-
attr.sched_policy is SCHED_DEADLINE, and the deadline scheduling
parameters in attr are invalid.
- EINVAL
-
attr.sched_flags contains SCHED_FLAG_UTIL_CLAMP_MIN or
SCHED_FLAG_UTIL_CLAMP_MAX, and attr.sched_util_min or
attr.sched_util_max are out of bounds.
- EOPNOTSUPP
-
SCHED_FLAG_UTIL_CLAMP was provided, but the kernel was not built with
CONFIG_UCLAMP_TASK support.
- EPERM
-
呼び出した元が適切な特権を持っていない。
- EPERM
-
pid で指定されたスレッドの CPU affinity マスクにシステムの全ての CPU のうち含まれていないものがある
(sched_setaffinity(2) を参照)。
標準規格
Linux.
歴史
Linux 3.14.
注意
glibc does not provide wrappers for these system calls; call them using
syscall(2).
sched_setattr() は、sched_setscheduler(2), sched_setparam(2),
nice(2) の機能および setpriority の一部機能を持つ (ただし、setpriority(2)
の、指定されたユーザーに所属するすべてのプロセスまたは指定されたグループのすべてのプロセスの優先度を設定する機能は除く)。 同様に、
sched_getattr() は sched_getscheduler(2), sched_getparam(2) の機能および
getpriority(2) の一部機能を持つ。
バグ
バージョン 3.15 までの Linux では、 sched_setattr() は、 エラーの節に書かれている E2BIG
の場合にエラーEFAULT で失敗していた。
Up to Linux 5.3, sched_getattr() failed with the error EFBIG if the
in-kernel sched_attr structure was larger than the size passed by user
space.
関連項目
chrt(1), nice(2), sched_get_priority_max(2),
sched_get_priority_min(2), sched_getaffinity(2), sched_getparam(2),
sched_getscheduler(2), sched_rr_get_interval(2),
sched_setaffinity(2), sched_setparam(2), sched_setscheduler(2),
sched_yield(2), setpriority(2), pthread_getschedparam(3),
pthread_setschedparam(3), pthread_setschedprio(3), capabilities(7),
cpuset(7), sched(7)
Index
- 名前
-
- ライブラリ
-
- 書式
-
- 説明
-
- sched_setattr()
-
- sched_getattr()
-
- 返り値
-
- エラー
-
- 標準規格
-
- 歴史
-
- 注意
-
- バグ
-
- 関連項目
-
This document was created by
man2html,
using the manual pages.