struct sched_param 结构体结构

原创 2011年01月13日 13:46:00

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

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

pthread 简要使用指南(一) pthread_create

POSIX thread 简称为pthread,Posix线程是一个POSIX标准线程。该标准定义内部API创建和操纵线程。 Pthreads定义了一套 C程序语言类型、函数与常量,它以 pthrea...

Linux多线程学习(八)pthread_setschedparam

pthread_setschedparam 设置线程的权限 int pthread_setschedparam(pthread_t target_thread, int policy, const ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

sched_setscheduler用法

设置调度策略   #include        int sched_setscheduler(pid_t pid, int policy,       ...
  • allwtg
  • allwtg
  • 2010-01-25 15:32
  • 21123

Linux多线程学习(一)pthread_create

Linux系统下的多线程遵循POSIX线程接口,称为pthread。 #include int pthread_create(pthread_t *restrict tidp, con...

struct sched_param 结构体结构

sched_paramStructure that describes scheduling parametersSynopsis:#include struct sched_param { ...

T264_param_t, T264_t结构体

转载地址:http://blog.sina.com.cn/s/blog_4171e65d0100o4px.html   转载背后:今晚看图像采集应用程序,里面“T264_param_t m_par...

T264_param_t, T264_t结构体成员解析

转载地址:http://blog.sina.com.cn/s/blog_4171e65d0100o4px.html   转载背后:今晚看图像采集应用程序,里面“T264_param_t m_par...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)