C:freeRTOS不能按预期工作

时间:2015-11-19 10:25:35

标签: 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

1 个答案:

答案 0 :(得分:0)

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