Структура, описывающая параметры планирования
#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
используется при получении или установке параметров планирования для потока или процесса.
Следующие функции используются для получения параметров планирования:
Следующие функции используются для установки параметров планирования:
Структкра sched_param
включает следующие поля:
Остальные поля структуры используются со спорадическим планированием. Следующие директивы #define
создают POSIX-имена, соответствующие этим полям, и именно их следует использовать вместо прямого доступа к полям.
|
Данный код показывает использование рабочего цикла спорадического потока сервера:
#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, ¶ms );if ( ret != 0 ){printf( "pthread_getschedparam() failed %d \n", errno );break;} elseif ( 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( ¶ms.sched_ss_init_budget, &MY_INIT_BUDGET, sizeof( MY_INIT_BUDGET ) );memcpy( ¶ms.sched_ss_repl_period, &MY_REPL_PERIOD, sizeof( MY_REPL_PERIOD ) );params.sched_ss_max_repl = MY_MAX_REPL;ret = pthread_attr_setschedparam( &attr, ¶ms );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);}
См. также sched_getparam().
POSIX 1003.1 Process Scheduling
struct timespec, pthread_attr_getschedparam(), pthread_attr_setschedparam(), pthread_getschedparam(), pthread_setschedparam(), pthread_setschedprio(), sched_getparam(), sched_setparam(), sched_setscheduler(), SchedGet(), SchedSet(), ThreadCreate()
Предыдущий раздел: Описание API системной библиотеки