簡體   English   中英

C:freeRTOS無法按預期運行

[英]C: freeRTOS does not work as expected

我寫了一個簡單的示例,其中包含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);
    }
}
/*-----------------------------------------------------------*/

結果

我只是嘗試使用FreeRTOS V8.2.3在FreeRTOS Windows端口中運行此命令,並且輸出顯示首先運行任務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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM