/* includes */
#include "vxWorks.h"
#include "taskLib.h"
#include "kernelLib.h"
#include "sysLib.h"
/* function prototypes */
void taskOne(void);
void taskTwo(void);
void taskThree(void);
/* globals */
#define ITER1 100
#define ITER2 10
#define PRIORITY 101
#define TIMESLICE sysClkRateGet()
#define LONG_TIME 0xFFFFFFL
void sched(void) /* function to create the three tasks */
{
int taskIdOne, taskIdTwo, taskIdThree;
/* turn round-robin on */
if (kernelTimeSlice(TIMESLICE) == OK)
printf("\n\n\n\n\t\t\tTIMESLICE = %d seconds\n\n\n", TIMESLICE / 60);
/* spawn the three tasks */
if ((taskIdOne = taskSpawn("task1", PRIORITY, 0x100, 20000, (FUNCPTR)taskOne,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskOne failed\n");
if ((taskIdTwo = taskSpawn("task2", PRIORITY, 0x100, 20000, (FUNCPTR)taskTwo,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskTwo failed\n");
if ((taskIdThree = taskSpawn("task3", PRIORITY, 0x100, 20000, (FUNCPTR)taskThree,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskThree failed\n");
}
void taskOne(void)
{
unsigned int i, j;
for (i = 0; i < ITER1; i++)
{
/* log messages */
for (j = 0; j < ITER2; j++)
printf("task1\n");
/* allow time for context switch */
for (j = 0; j < LONG_TIME; j++)
;
}
}
void taskTwo(void)
{
unsigned int i, j;
for (i = 0; i < ITER1; i++)
{
/* log messages */
for (j = 0; j < ITER2; j++)
printf("task2\n");
/* allow time for context switch */
for (j = 0; j < LONG_TIME; j++)
;
}
}
void taskThree(void)
{
unsigned int i, j;
for (i = 0; i < ITER1; i++)
{
/* log messages */
for (j = 0; j < ITER2; j++)
printf("task3\n");
/* allow time for context switch */
for (j = 0; j < LONG_TIME; j++)
;
}
}
在这个程序中,用户启动了三个优先级相同的任务,并通过对kernelTimeSlice(TIMESLICE)的调用启动了时间片轮转调度。
程序运行输出:一会儿输出一些“task1”,一会儿输出一些“task2”,再一会儿输出一些“task3”。每次输出了某任务的一部分内容后,就开始输出另一任务的内容,这说明了task1、task2、task3再进行时间片轮转切换。
kernelTimeSlice()的函数原型为:
STATUS kernelTimeSlice (int ticks /* time-slice in ticks or 0 to disable round-robin */ );
程序中的kernelTimeSlice(TIMESLICE)展开后为kernelTimeSlice(sysClkRateGet()),即每秒钟进行一次轮转。如果kernelTimeSlice函数中的输入参数为0,时间片轮转调度就不会发生,程序的输出将是:先输出ITER1* ITER2个“task1”,再输出ITER1* ITER2个“task2”,最后输出ITER1* ITER2个“task3”。
阅读(4427) | 评论(0) | 转发(0) |