/* 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 HIGH 100 /* high priority */
#define MID 101 /* medium priority */
#define LOW 102 /* low priority */
#define LONG_TIME 0xFFFFFFL
void sched(void) /* function to create the two tasks */
{
int taskIdOne, taskIdTwo, taskIdThree;
printf("\n\n\n\n\n");
/* spawn the three tasks */
if ((taskIdOne = taskSpawn("task1", LOW, 0x100, 20000, (FUNCPTR)taskOne, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskOne failed\n");
if ((taskIdTwo = taskSpawn("task2", MID, 0x100, 20000, (FUNCPTR)taskTwo, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskTwo failed\n");
if ((taskIdThree = taskSpawn("task3", HIGH, 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++)
;
}
}
程序的运行结果为:先输出ITER1* ITER2个“task3”,再输出ITER1* ITER2个“task2”,最后输出ITER1* ITER2个“task1”。这是因为task1、task2、task3的优先级顺序是“task3-task2-task1”,故在task3执行完(或被阻塞、挂起)前,task2得不到执行;同理,在在task2执行完(或被阻塞、挂起)前,task1也得不到执行。
阅读(7399) | 评论(0) | 转发(0) |