pthread 调度策略与优先级设置详解与示例 SCHED_FIFO SCHED_IDLE

pthread 调度与优先级设置

pthread_attr_setschedpolicy

  • 函数原型:

    int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
    
    • attr:指向线程属性对象的指针。
    • policy:要设置的调度策略。支持的调度策略为 SCHED_FIFO,SCHED_RR,SCHED_OTHER。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于指定线程的调度策略。

  • 为了使 pthread_attr_setschedpolicy 生效,调用者必须在调用 pthread_create 之前调用pthread_attr_setinheritschedattr 的继承属性设置为 PTHREAD_EXPLICIT_SCHED

pthread_attr_setschedparam

  • 函数原型:

    int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
    
    • attr:指向线程属性对象的指针。
    • param:是一个指向 sched_param 结构体的指针,该结构体包含了要设置的优先级。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于指定线程的调度参数。

  • 为了使 pthread_attr_setschedparam 生效,调用者必须在调用 pthread_create 之前调用pthread_attr_setinheritschedattr 的继承属性设置为 PTHREAD_EXPLICIT_SCHED

pthread_attr_setinheritsched

  • 函数原型:

    int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
    
    • attr:指向线程属性对象的指针。
    • inheritsched:用于指定是否继承父线程的调度策略和优先级,可以使用以下两个值:
      • PTHREAD_INHERIT_SCHED:表示线程将继承创建它的线程的调度策略和优先级。
      • PTHREAD_EXPLICIT_SCHED:表示线程将使用自己在线程属性对象中设置的调度策略和优先级。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于设置线程属性对象是否继承父线程的调度策略和优先级。

pthread_setschedparam

  • 函数原型:

    int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
    
    • thread:目标线程的标识符。
    • policy: 要设置的调度策略,支持的调度策略为 SCHED_FIFO,SCHED_RR,SCHED_OTHER,SCHED_BATCH,SCHED_IDLE。
    • param:是一个指向 sched_param 结构体的指针,该结构体包含了要设置的优先级。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于设置指定线程的调度参数,包括线程的优先级和调度策略。

pthread_setschedprio

  • 函数原型:

    int pthread_setschedprio(pthread_t thread, int prio);
    
    • thread:目标线程的标识符。
    • prio:要设置的线程优先级。
    • 返回值:成功返回 0,失败返回错误代码。
  • 用于设置指定线程的优先级。

示例 1

  • 以 SCHED_FIFO 的方式调度线程,并设置其优先级为 5。

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <sched.h>
    
    void *thread_func(void *arg)
    {
        usleep(10000);
        return NULL;
    }
    
    int main()
    {
        // 初始化线程属性对象
        pthread_attr_t attr;
        pthread_attr_init(&attr);
    
        // 设置线程的调度策略
        pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    
        // 设置线程的优先级
        struct sched_param param;
        param.sched_priority = 5;
        pthread_attr_setschedparam(&attr, &param);
    
        // 设置线程不继承父进程调度策略以及优先级
        pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
    
        // 创建线程
        pthread_t thread;
        pthread_create(&thread, &attr, thread_func, NULL);
    
        // 销毁线程属性对象
        pthread_attr_destroy(&attr);
    
        // 等待线程结束
        pthread_join(thread, NULL);
        return 0;
    }
    

示例 2

  • 以 SCHED_IDLE 的方式调度线程。

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <sched.h>
    
    void *thread_func(void *arg)
    {
        usleep(10000);
        return NULL;
    }
    
    int main()
    {
    
        // 创建线程
        pthread_t thread;
        pthread_create(&thread, NULL, thread_func, NULL);
    
        // 设置线程的调度策略
        struct sched_param param;
        param.sched_priority = 0;
        pthread_setschedparam(thread, SCHED_IDLE, &param);
    
        // 等待线程结束
        pthread_join(thread, NULL);
        return 0;
    }
    
以下是使用C语言实现SCHED_FIFO算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sched.h> #include <errno.h> #define STACK_SIZE 1024*1024 void* thread_function(void* arg); int main() { int thread_id; pthread_t thread; pthread_attr_t attr; struct sched_param param; int policy; void* stack; // 分配线程栈 stack = malloc(STACK_SIZE); if (stack == NULL) { perror("malloc"); exit(EXIT_FAILURE); } // 初始化线程属性 if (pthread_attr_init(&attr) != 0) { perror("pthread_attr_init"); exit(EXIT_FAILURE); } // 设置线程栈 if (pthread_attr_setstack(&attr, stack, STACK_SIZE) != 0) { perror("pthread_attr_setstack"); exit(EXIT_FAILURE); } // 设置调度策略SCHED_FIFO policy = SCHED_FIFO; if (pthread_attr_setschedpolicy(&attr, policy) != 0) { perror("pthread_attr_setschedpolicy"); exit(EXIT_FAILURE); } // 设置线程优先级 param.sched_priority = 50; if (pthread_attr_setschedparam(&attr, &param) != 0) { perror("pthread_attr_setschedparam"); exit(EXIT_FAILURE); } // 创建线程 if (pthread_create(&thread, &attr, &thread_function, NULL) != 0) { perror("pthread_create"); exit(EXIT_FAILURE); } // 等待线程结束 if (pthread_join(thread, NULL) != 0) { perror("pthread_join"); exit(EXIT_FAILURE); } // 销毁线程属性 if (pthread_attr_destroy(&attr) != 0) { perror("pthread_attr_destroy"); exit(EXIT_FAILURE); } free(stack); exit(EXIT_SUCCESS); } void* thread_function(void* arg) { // 打印线程信息 printf("Thread running.\n"); printf("Thread priority: %d\n", sched_get_priority_min(SCHED_FIFO)); printf("Thread policy: %d\n", sched_getscheduler(0)); // 让线程休眠5秒钟 sleep(5); printf("Thread finished.\n"); return NULL; } ``` 在以上示例中,我们使用了pthread库来创建线程,设置线程属性和等待线程结束。在主函数中,我们首先分配线程栈,然后初始化线程属性。接着,我们设置线程栈和调度策略SCHED_FIFO,并设置线程优先级为50。然后,我们创建线程,等待线程结束,销毁线程属性,释放线程栈,并退出程序。 在线程函数中,我们打印线程信息,让线程休眠5秒钟,然后打印线程结束信息并返回NULL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注的罗哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值