我对 RTOS 编程相当陌生,并且在使用互斥锁时遇到了一些优先级问题。
我确定了以下优先事项。
#define T_HI_PRIORITY 10
#define T_ME_PRIORITY 50
我希望这段代码运行具有最高优先级的任务“tMePriorityTask”和具有中等优先级的“tHiPriorityTask”。“tLoPriorityTask”已被注释,因此现在不应运行。
#include <stdio.h>
#include "main.h"
#include "vxWorks.h"
#include "semLib.h"
#include "taskLib.h"
SEM_ID semMutex; // named semaphore object
char alphabet[27]; // memory resource to have exclusive access
void tHiPriorityTask (void)
{
int i;
// enter critical region - any other tasks wanting access to alphabet[] should
// wait for available semaphore
semTake (semMutex, WAIT_FOREVER);
// write alphabet to global array
for (i= 0; i < 26; i++)
alphabet[i] = 'A' + i;
alphabet[i] = '\0';
printf("High priority.\n-Counting alphabet...\n");
// leave critical region
semGive (semMutex);
}
void tMePriorityTask (void)
{
// enter critical region
semTake (semMutex, WAIT_FOREVER);
//medium priority task enters
printf("Medium priority.\n-Just entering...\n");
// leave critical region
semGive (semMutex);
}
/*void tLoPriorityTask (void)
{
// enter critical region
semTake (semMutex, WAIT_FOREVER);
// array members guaranteed stable while being read by this task
printf("Low priority\n");
printf ("-alphabet= %s ", alphabet);
// leave critical region
semGive (semMutex);
}*/
void main (void)
{
//create binary semaphore which is initially full (available)
semMutex = semBCreate (SEM_Q_PRIORITY, SEM_FULL);
// spawn high priority task
taskSpawn ("hi_priority_task", T_ME_PRIORITY, VX_FP_TASK, 10000, (FUNCPTR) tHiPriorityTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// spawn medium priority task
taskSpawn ("me_priority_task", T_HI_PRIORITY, VX_FP_TASK, 10000, (FUNCPTR) tMePriorityTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// spawn low priority task
//taskSpawn ("lo_priority_task", T_LO_PRIORITY, VX_FP_TASK, 10000, (FUNCPTR) tLoPriorityTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
我尝试在生成任务时更改优先级,但这似乎不起作用,至少它不会改变屏幕上的任何内容。我正在使用 VxWorks RTOS。
谢谢你。