36.5. 显示进程的时间片


SCHED_RR (round-robin)策略与 SCHED_FIFO (first-in、first-out)策略稍有不同。SCHED_RR 在循环轮转中分配具有相同优先级的并发进程。这样,每个进程都会被分配一个时间片。sched_rr_get_interval () 函数报告分配给每个进程的时间片。

注意

虽然 POSIX 要求这个功能只能用于配置为使用 SCHED_RR 调度程序策略运行的进程,但 sched_rr_get_interval () 函数可以检索 Linux 上任何进程的时间片长度。

timeslice 信息作为 timespec 返回。这是自 00:00:00 GMT 基础时间 1 月 1 日以来的秒数和纳秒:

struct timespec {
  time_t tv_sec;  /* seconds */
  long tv_nsec;   /* nanoseconds */
}

流程

  1. 创建 sched_timeslice.c 源文件,并在文本编辑器中打开该文件。

    $ {EDITOR} sched_timeslice.c
  2. 将以下行添加到 sched_timeslice.c 文件中。

    #include <stdio.h>
    #include <sched.h>
    
    int main()
    {
       struct timespec ts;
       int ret;
    
       /* real apps must check return values */
       ret = sched_rr_get_interval(0, &ts);
    
       printf("Timeslice: %lu.%lu\n", ts.tv_sec, ts.tv_nsec);
    
       return 0;
    }
  3. 保存文件并退出编辑器。
  4. 编译程序。

    $ gcc sched_timeslice.c -o sched_timeslice
  5. 使用不同策略和优先级运行程序。

    $ chrt -o 0 ./sched_timeslice
    Timeslice: 0.38994072
    $ chrt -r 10 ./sched_timeslice
    Timeslice: 0.99984800
    $ chrt -f 10 ./sched_timeslice
    Timeslice: 0.0
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部