/* 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, taskIdFour;
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");
if ((taskIdFour = taskSpawn("task4", HIGH, 0x100, 20000, (FUNCPTR)taskFour,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn taskFour 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++)
;
}
}
void taskFour(void)
{
taskDelay(TIMESLICE / 10);
taskPrioritySet(taskIdOne, HIGH - 1);
}
task4延迟100毫秒后,将task1的优先级设置到最高:在task2任务运行输出“task2”的过程中,task1抢占了task2的CPU资源,先是输出了所有的“task1”,最后剩余的“task2”得以输出。
在VxWorks中,我们可以使用taskPrioritySet()函数来动态改变任务的优先级,这个函数的原型是:
STATUS taskPrioritySet (int tid, /* task ID */ int newPriority /* new priority */ );
阅读(4787) | 评论(0) | 转发(0) |