Linux 系统中的 CPU 调度与高级文件 I/O 技术
1. Linux 中的 CPU 调度
在 Linux 系统里,设置线程(或进程)的调度策略和优先级时,需要以 root 权限运行。现代为线程赋予特权的方式是通过 Linux 能力模型(Linux Capabilities model),具备CAP_SYS_NICE能力的线程可以随意设置自身的调度策略和优先级。
相关的 API 调用如下:
-pthread_setschedparam(3)内部调用sched_setscheduler(2)系统调用。
-pthread_getschedparam(3)调用sched_getscheduler(2)系统调用。
它们的 API 签名如下:
#include <sched.h> int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); int sched_getscheduler(pid_t pid);还有其他的 pthreads API 可以用来设置线程属性结构,例如pthread_attr_setinheritsched(3)、pthread_attr_setschedpa