gpt4 book ai didi

C: freeRTOS 没有按预期工作

转载 作者:行者123 更新时间:2023-11-30 17:08:10 31 4
gpt4 key购买 nike

我写了一个简单的例子,包括 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com