struct sched_param 结构体

18 篇文章 1 订阅

作用

  • 描述调度参数的结构

概要

#include <sched.h>

struct sched_param 
{ 
    int32_t  sched_priority; 
    int32_t  sched_curpriority; 
    union 
    { 
        int32_t  reserved[8]; 
        struct 
        {    
            int32_t  __ss_low_priority;  
            int32_t  __ss_max_repl;  
            struct timespec     __ss_repl_period;   
            struct timespec     __ss_init_budget;   
        }           __ss;   
    }           __ss_un;    
}

#define sched_ss_low_priority   __ss_un.__ss.__ss_low_priority
#define sched_ss_max_repl       __ss_un.__ss.__ss_max_repl
#define sched_ss_repl_period    __ss_un.__ss.__ss_repl_period
#define sched_ss_init_budget    __ss_un.__ss.__ss_init_budget

描述

  • 当您获得或设置线程/进程的调度参数时,会使用sched_param结构
使用下列函数获取调度参数
  • pthread_attr_getschedparam()
  • pthread_getschedparam()
  • sched_getparam()
  • SchedGet()
使用下列参数设置调度参数
  • pthread_attr_setschedparam()
  • pthread_setschedparam()
  • sched_setparam()
  • sched_setscheduler()
  • SchedSet()
  • ThreadCreate()
sched_param的成员包括
  • sched_priority
    • 当获得调度参数时,该成员反映出分配给线程或进程的优先级。它不反映任何由于优先级继承而造成的临时调整。
    • 当您设置调度参数时,请将此成员设置为您想要使用的优先级。优先级必须介于sched_get_priority_min()和sched_get_priority_max()为调度策略返回的最小值和最大值之间。
  • sched_curpriority
    • 当您获得调度参数时,该成员被设置为线程或进程当前运行的优先级。这是内核在做出调度决策时使用的值。
    • 当您设置调度参数时,该成员将被忽略。
  • sched_ss_low_priority
    • 正在执行的后台或低优先级线程
  • sched_ss_max_repl
    • 补给计划的最大次数,通常是由于阻塞操作。在一个线程多次阻塞之后,它会自动将其执行的剩余部分降至低优先级,直到其执行预算得到补充。
  • sched_ss_repl_period
    • The time that should be used for scheduling the replenishment of an execution budget after being blocked or having overrun the maximum number of replenishments. This time is used as an offset against the time that a thread is made READY.
  • sched_ss_init_budget
    • 应该用于线程执行预算的时间。当线程运行在其高优先级级别时,它的执行时间就从这个预算中划分出来了。一旦预算完全耗尽,线程就会降到低优先级,如果可能的话,由于优先级安排,线程可以继续运行,直到执行预算得到补充。
注意
  • sched_priority必须总是高于sched_ss_low_priority。
  • sched_ss_max_repl必须小于SS_REPL_MAX。
  • sched_ss_init_budget必须大于sched_ss_repl_period。

举例

  • 这段代码显示了服务器线程的占空比使用情况:
#include <stdio.h>
#include <errno.h>
#include <sched.h>
#include <pthread.h>
#include <inttypes.h>
#include <sys/syspage.h>
#include <sys/neutrino.h>

/* 50 % duty cycle of 5 secs on 5 secs off */
struct timespec g_init_budget = { 5, 0 };
struct timespec g_repl_period = { 10, 0 };

#define MY_HIGH_PRIORITY 5
#define MY_LOW_PRIORITY 4
#define MY_REPL_PERIOD g_repl_period
#define MY_INIT_BUDGET g_init_budget
#define MY_MAX_REPL 10

#define DUTY_CYCLE_LOOPS 10

/*
 Run a compute-bound thread (minimal blocking) to show the
 duty cycle.
*/
void *st_duty_check(void *arg) {
    struct sched_param  params;
    uint64_t        stime, etime, cps;
    double          secs;
    int         ret, prio;
    int         prevprio, iterations;

    stime = ClockCycles();
    cps = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
    iterations = 0;

    printf("/n");

    prevprio = -1;
    while(iterations < DUTY_CYCLE_LOOPS) {
        etime = ClockCycles();
        ret = pthread_getschedparam(pthread_self(), &prio,
                                    &params);

        if(ret != 0) {
            printf("pthread_getschedparam() failed %d /n",
                   errno);
            break;
        } else if (prevprio != -1 && prevprio !=
                   params.sched_priority) {
            stime = etime - stime;
            secs = (double)stime / (double)cps;
            printf("pri %d (cur %d) %lld cycles %g secs/n", 
                params.sched_priority, 
                params.sched_curpriority, 
                stime, secs);
            stime = etime;
            iterations++;
        }
        prevprio = params.sched_priority;
    }

    return NULL;
}

int main(int argc, char **argv) {
    struct sched_param params;
    pthread_attr_t     attr;
    pthread_t      thr;
    int        ret;

    /* Set the attribute structure with the sporadic values */
    printf("# Set sporadic attributes ...");
    pthread_attr_init(&attr);
    ret = pthread_attr_setinheritsched(&attr,
             PTHREAD_EXPLICIT_SCHED);
    if(ret != 0) {
        printf("pthread_attr_setinheritsched() failed %d /n",
               errno);
        return 1;
    }

    ret = pthread_attr_setschedpolicy(&attr, SCHED_SPORADIC);
    if(ret != 0) {
        printf("pthread_attr_setschedpolicy() failed %d %d/n",
               ret, errno);
        return 1;
    } 

    params.sched_priority = MY_HIGH_PRIORITY;
    params.sched_ss_low_priority = MY_LOW_PRIORITY;
    memcpy(&params.sched_ss_init_budget, &MY_INIT_BUDGET,
           sizeof(MY_INIT_BUDGET));
    memcpy(&params.sched_ss_repl_period, &MY_REPL_PERIOD,
           sizeof(MY_REPL_PERIOD));
    params.sched_ss_max_repl = MY_MAX_REPL; 
    ret = pthread_attr_setschedparam(&attr, &params);
    if(ret != 0) {
        printf("pthread_attr_setschedparam() failed %d /n", errno);
        return 1;
    }
    printf("OK/n");

    /* Create a sporadic thread to check the duty cycle */
    printf("# Creating thread duty cycle thread (%d changes) ... ",
           DUTY_CYCLE_LOOPS);
    ret = pthread_create(&thr, &attr, st_duty_check, NULL);
    if(ret != 0) {
        printf("pthread_create() failed %d /n", errno);
        return 1;
    }
    pthread_join(thr, NULL);
    printf("OK/n");

    return 0;
}
  • 7
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值