C:freeRTOS没有按预期工作

C:freeRTOS没有按预期工作,c,scheduler,freertos,C,Scheduler,Freertos,我写了一个简单的例子,包括两个任务:任务1和任务2。任务1的优先级高于任务2。在任务1功能中,我增加了任务2的优先级,使其优先级等于(任务1的优先级+1)。此外,在Task2函数中,我将其优先级降低了2,使其优先级低于Task1。 因此,执行顺序为task 1->task 2->task 1->task 2。。。 但是,当我运行代码时,任务2首先运行。有人能帮我解决这个问题吗?我包括我的代码和结果如下: /* Standard includes. */ #include <stdio.h&

我写了一个简单的例子,包括两个任务:任务1和任务2。任务1的优先级高于任务2。在任务1功能中,我增加了任务2的优先级,使其优先级等于(任务1的优先级+1)。此外,在Task2函数中,我将其优先级降低了2,使其优先级低于Task1。 因此,执行顺序为task 1->task 2->task 1->task 2。。。 但是,当我运行代码时,任务2首先运行。有人能帮我解决这个问题吗?我包括我的代码和结果如下:

/* Standard includes. */
#include <stdio.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "basic_io.h"

/*-----------------------------------------------------------*/
#define mainDELAY_LOOP_COUNT            ( 0xffffff )
/*
 * The tasks as described in the comments at the top of this file.
 */
static void prvTask1( void *pvParameters );
static void prvTask2( void *pvParameters );

/*
 * The task parameters
 */
const char *pvTask1Param = "Continuous task 1 is running\n";
const char *pvTask2Param = "Continuous task 2 is running\n";

xTaskHandle xTask1Handle, xTask2Handle;

/*-----------------------------------------------------------*/

void main( void )
{

        /* Start the two tasks as described in the comments at the top of this
        file. */
        xTaskCreate( prvTask1,                  /* The function that implements the task. */
                    "Task 1",                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */
                    100,                        /* The size of the stack to allocate to the task. */
                    (void*)pvTask1Param,        /* The parameter passed to the task - just to check the functionality. */
                    2,                          /* The priority assigned to the task. */
                    &xTask1Handle );            /* The task handle is not required, so NULL is passed. */

        xTaskCreate( prvTask2, "Task 2", 100, (void*)pvTask2Param, 1, &xTask2Handle );

        /* Start the tasks and timer running. */
        vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following
    line will never be reached.  If the following line does execute, then
    there was insufficient FreeRTOS heap memory available for the idle and/or
    timer tasks to be created.  See the memory management section on the
    FreeRTOS web site for more details. */
    for( ;; );
}
/*-----------------------------------------------------------*/

static void prvTask1( void *pvParameters )
{
    char *string = (char*)pvParameters;
    for( ;; )
    {
        vPrintString(string);

        portBASE_TYPE task1Priority = uxTaskPriorityGet(NULL);
        vTaskPrioritySet(xTask2Handle, task1Priority+1);
    }
}
/*-----------------------------------------------------------*/

static void prvTask2( void *pvParameters )
{
    char *string = (char*)pvParameters;
    for( ;; )
    {
        vPrintString(string);

        portBASE_TYPE task2Priority = uxTaskPriorityGet(NULL);
        vTaskPrioritySet(NULL, task2Priority-2);
    }
}
/*-----------------------------------------------------------*/
/*标准包括*/
#包括
/*内核包括*/
#包括“FreeRTOS.h”
#包括“task.h”
#包括“基本io.h”
/*-----------------------------------------------------------*/
#定义主延迟\循环\计数(0xffffff)
/*
*此文件顶部注释中描述的任务。
*/
静态空隙prvTask1(空隙*PVK参数);
静态空隙prvTask2(空隙*PVK参数);
/*
*任务参数
*/
const char*pvTask1Param=“连续任务1正在运行\n”;
const char*pvTask2Param=“连续任务2正在运行\n”;
xTaskHandle xTask1Handle,xTask2Handle;
/*-----------------------------------------------------------*/
真空总管(真空)
{
/*按照本手册顶部注释中的说明启动这两项任务
文件*/
xTaskCreate(prvTask1,/*实现任务的函数*/
“Task 1”,/*分配给任务的文本名称-仅用于调试,因为内核不使用它*/
100,/*要分配给任务的堆栈大小*/
(void*)pvTask1Param,/*传递给任务的参数-仅用于检查功能*/
2、/*分配给任务的优先级*/
&xTask1Handle);/*任务句柄不是必需的,因此传递NULL*/
xTaskCreate(prvTask2,“任务2”,100,(void*)pvTask2Param,1和xTask2Handle);
/*启动任务并运行计时器*/
vTaskStartScheduler();
/*如果一切正常,调度程序现在将运行,如下所示
永远不会到达行。如果执行了以下行,则
空闲和/或可用的FreeRTOS堆内存不足
要创建的计时器任务。请参阅上的内存管理部分
有关更多详细信息,请访问FreeRTOS网站*/
对于(;);
}
/*-----------------------------------------------------------*/
静态void prvTask1(void*pvParameters)
{
char*string=(char*)pvParameters;
对于(;;)
{
vPrintString(字符串);
portBASE_TYPE task1Priority=uxTaskPriorityGet(NULL);
vTaskPrioritySet(xTask2Handle,task1优先级+1);
}
}
/*-----------------------------------------------------------*/
静态void prvTask2(void*pvParameters)
{
char*string=(char*)pvParameters;
对于(;;)
{
vPrintString(字符串);
portBASE_TYPE task2Priority=uxTaskPriorityGet(NULL);
vTaskPrioritySet(NULL,task2Priority-2);
}
}
/*-----------------------------------------------------------*/

我刚刚尝试在使用FreeRTOS V8.2.3的版本中运行它,结果显示task 1首先运行,我认为这与预期的一样:

Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running
Continuous task 1 is running
Continuous task 2 is running

你使用哪种编译器?我的讲师用WATCOM C/C++构建了这个文件,并运行了可执行文件,但结果是任务2仍然首先运行。我为C和C++开发人员使用了Eclipse IDE,但没有按预期的方式运行。