关闭

struct sched_param 结构体结构

标签: structparametersthreadloopsstructureattributes
5450人阅读 评论(0) 收藏 举报

sched_param

Structure that describes scheduling parameters

Synopsis:

#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

Description:

You'll use the sched_param structure when you get or set the scheduling parameters for a thread or process.

You can use these functions to get the scheduling parameters:

You can use these functions to set the scheduling parameters:

The members of sched_param include:

sched_priority
When you get the scheduling parameters, this member reflects the priority that was assigned to the thread or process. It doesn't reflect any temporary adjustments due to priority inheritance.

When you set the scheduling parameters, set this member to the priority that you want to use. The priority must be between the minimum and maximum values returned by sched_get_priority_min() and sched_get_priority_max() for the scheduling policy.

sched_curpriority
When you get the scheduling parameters, this member is set to the priority that the thread or process is currently running at. This is the value that the kernel uses when making scheduling decisions.

When you set the scheduling parameters, this member is ignored.

The other members are used with sporadic scheduling. The following #define directives create the POSIX names that correspond to those members and should be used instead of accessing members directly.

sched_ss_low_priority
The background or low priority for the thread that's executing.
sched_ss_max_repl
The maximum number of times a replenishment will be scheduled, typically because of a blocking operation. After a thread has blocked this many times, it automatically drops to the low-priority level for the remainder of its execution until its execution budget is replenished.
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
The time that should be used for the thread's execution budget. As the thread runs at its high-priority level, its execution time is carved out of this budget. Once the budget is entirely depleted, the thread drops to its low-priority level, where, if possible because of priority arrangements, it can continue to run until the execution budget is replenished.

Note:
  • The sched_priority must always be higher than sched_ss_low_priority.
  • The sched_ss_max_repl must be smaller than SS_REPL_MAX.
  • The sched_ss_init_budget must be larger than sched_ss_repl_period.

For more information, see Scheduling algorithms in the Neutrino Microkernel chapter of the System Architecture guide.

Examples:

This code shows a duty-cycle usage of the sporadic server thread:

#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;
}

See also sched_getparam().

Classification:

POSIX 1003.1 PS

See also:

pthread_attr_getschedparam(), pthread_attr_setschedparam(), pthread_getschedparam(), pthread_setschedparam(), sched_getparam(), sched_setparam(), sched_setscheduler(), SchedGet(), SchedSet(), ThreadCreate()

 

Source Ref : http://china.qnx.com/developers/docs/6.4.1/neutrino/lib_ref/s/sched_param.html

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

C 结构体 struct 详解

数组(Array),它是一组具有相同类型的数据的集合。但在实际的编程过程中,我们往往还需要一组类型不同的数据,例如对于学生信息登记表,姓名为字符串,学号为整数,年龄为整数,所在的学习小组为字符,成绩为...
  • zhanghow
  • zhanghow
  • 2016-12-05 11:47
  • 2547

结构体类型struct(c++)

本文简单介绍了定义结构体的三种形式,嵌套结构体,结构体变量的初始化,结构体变量成员的引用方法,以及结构体指针
  • sinat_34927324
  • sinat_34927324
  • 2016-12-23 20:04
  • 413

C/C++结构体struct详解

结构体定义 typedefstruct 用法详解和用法小结 typedef是类型定义的意思。typedef struct 是为了使用这个结构体方便。 具体区别在于: 若struct n...
  • itguangit
  • itguangit
  • 2015-11-30 19:07
  • 9147

关于C中struct结构体的动态malloc和memcpy的指针操作

今天碰到一个问题,先贴代码:typedef struct Gifheader { char Signature[3]; char Version[3]; gifhea...
  • dgj8300
  • dgj8300
  • 2016-04-17 01:17
  • 2108

C# struct(结构)与按值复制、按索引复制详解

本文讲解了struct的用法以及按值复制、按索引复制的不同。struct是结构(structure)的简写,struct看上去与类非常相似,它们也有字段和属性。
  • u013180863
  • u013180863
  • 2015-09-23 15:33
  • 1464

C#语言struct结构体适用场景和注意事项

我们有时感觉结构比较简单的类改为struct可能会提高性能,但这种感觉在绝大多数情况下其实是错误的。那么我们自己在编写代码的时候究竟在什么情况下适合定义struct而不是class呢?
  • zeroflamy
  • zeroflamy
  • 2016-08-01 00:03
  • 3458

C语言结构体(struct)常见使用方法

结构体与函数: 关于传参,首先: void func(int); func(a.b.c); 把结构体中的int成员变量当做和普通int变量一样的东西来使用,是不用脑子就想到的一种方法。 另外两...
  • huqinweI987
  • huqinweI987
  • 2014-04-14 01:51
  • 416810

C语言中的结构体(struct)

C语言中,结构体类型属于一种构造类型(其他的构造类型还有:数组类型,联合类型)。本文主要介绍关于结构体以下几部分。 1、概念为什么要有结构体?因为在实际问题中,一组数据往往有很多种不同的数据类型。例...
  • yanggangclcsdn
  • yanggangclcsdn
  • 2015-11-08 13:50
  • 2705

结构体定义:struct与typedef struct 用法详解和用法小结

一、typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: typedef int INT; typ...
  • u012966194
  • u012966194
  • 2016-10-28 11:19
  • 1544

Java 结构体之 JavaStruct 使用教程<三> JavaStruct 数组进阶

经过前面两篇博客的介绍,相信对于 JavaStruct 的认识以及编程使用,读者已经有一定的基础了。只要理解和实践结合起来,掌握还是很容易的。下面进行一些数组使用方面的实例说明及演示。 在结构体类中使...
  • JAZZSOLDIER
  • JAZZSOLDIER
  • 2017-07-21 18:07
  • 1240
    个人资料
    • 访问:12115601次
    • 积分:90300
    • 等级:
    • 排名:第16名
    • 原创:718篇
    • 转载:169篇
    • 译文:16篇
    • 评论:3068条
    学习经历
    中科院、百度、创新工场、小米、阿里巴巴、米扑科技

    期货从业资格证(2017.03)
    基金从业资格证(2016.11)
    证券投资顾问证(2016.9)
    证券从业资格证(2016.6)
    系统架构设计师(2013)
    软件设计师(2008)
    CSDN创业专访

    程序员创业邦
    QQ群: 239292073
    青春,每一个有梦想的人

    资深产品经理人
    QQ群: 338142405
    思与行,人人都是产品经理

    爱脚本,爱技术
    QQ群: 320296250
    (已满)
    python、ruby、php、go、shell

    程序人生的平凡生活
    QQ群: 282297696
    (已满)
    汇聚百度、小米、微软、腾讯、
    创新工场、阿里巴巴、日本雅虎
    个人博客
    博客专栏
    博客公告
    本博客内容,由米扑博客精心整理
    欢迎交流,欢迎转载,大家转载注明出处,禁止用于商业目的。

    文章分类