C: freeRTOS 没有按预期工作

标签 c scheduler freertos

我写了一个简单的例子,包括 2 个任务:任务 1 和任务 2。任务 1 的优先级高于任务 2。在任务 1 函数中,我增加了任务 2 的优先级,使其优先级等于 (任务 1 的优先级 + 1)。此外,在任务 2 函数中,我将其优先级降低了 2,使其优先级低于任务 1。 结果,执行顺序为任务 1 -> 任务 2 -> 任务 1 -> 任务 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);
    }
}
/*-----------------------------------------------------------*/

Result

最佳答案

我刚刚尝试在 FreeRTOS Windows port 中运行它使用 FreeRTOS V8.2.3,输出显示任务 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

关于C: freeRTOS 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801180/

相关文章:

c - 驱动程序如何在 Windows 中工作?

c - 两个功能是否等价,work2 的改进时间是否超过 work1?

c - C语言中关于switch语句的贪吃蛇游戏

c - FreeRTOS 优先级 1 有什么特别之处?

c++ - 如何知道分配给定内存块的是什么?

oracle dbms_scheduler repeat_interval

jquery - 如何自定义 Kendo Scheduler Week View?

python apscheduler - 跳过 : maximum number of running instances reached

linux - WolfSSL on FreeRTOS 与 Linux 通信

stm32 - 如何从高优先级 ISR 唤醒 FreeRtos 任务?