FreeRTOS中有关任务Task使用的疑惑

[复制链接]
6060|11
手机看帖
扫描二维码
随时随地手机跟帖
wdliming|  楼主 | 2013-10-27 16:44 | 显示全部楼层 |阅读模式
本帖最后由 wdliming 于 2013-10-27 19:12 编辑

最近在学习FreeRTOS,找到一些例子,在一个外国的例子中,有关任务的建立和定义,我觉得不太理解,上代码:

int main( void )
{
    /* Create one of the two tasks. */
    xTaskCreate( vTaskFunction,   /* Pointer to the function that implements the task. */
                    "Task 1",    /* Text name for the task.  This is to facilitate debugging only. */
                    1000,     /* Stack depth - most small microcontrollers will use much less stack than this. */
                    (void*)pcTextForTask1, /* Pass the text to be printed in as the task parameter. */
                    1,      /* This task will run at priority 1. */
                    NULL );     /* We are not using the task handle. */

    /* Create the other task in exactly the same way.  Note this time that we
    are creating the SAME task, but passing in a different parameter.  We are
    creating two instances of a single task implementation. */
    xTaskCreate( vTaskFunction, "Task 2", 1000, (void*)pcTextForTask2, 1, NULL );
    /* Start the scheduler so our tasks start executing. */
    vTaskStartScheduler();
    /* If all is well we will never reach here as the scheduler will now be
    running.  If we do reach here then it is likely that there was insufficient
    heap available for the idle task to be created. */
    for( ;; );
    return 0;
}
/*-----------------------------------------------------------*/
void vTaskFunction( void *pvParameters )
{
    char *pcTaskName;
    volatile unsigned long ul;
    /* The string to print out is passed in via the parameter.  Cast this to a
    character pointer. */
    pcTaskName = ( char * ) pvParameters;
    /* As per most tasks, this task is implemented in an infinite loop. */
    for( ;; )
    {
        /* Print out the name of this task. */
        vPrintString( pcTaskName );
        /* Delay for a period. */
        for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
        {
            /* This loop is just a very crude delay implementation.  There is
            nothing to do in here.  Later exercises will replace this crude
            loop with a proper delay/sleep function. */
        }
    }
}



正如诸位所看到的,main函数里有两个xTaskCreate,建立了两个任务,而入口地址均为vTaskFunction,编译后没问题,不知大侠们如何解释这个啊??谢谢!

相关帖子

john_lee| | 2013-10-28 09:15 | 显示全部楼层
没什么好解释的,楼主觉得有问题吗?

使用特权

评论回复
wdliming|  楼主 | 2013-10-28 16:55 | 显示全部楼层
john_lee 发表于 2013-10-28 09:15
没什么好解释的,楼主觉得有问题吗?

不该有两个实现函数啊,这里只有一个啊??

使用特权

评论回复
john_lee| | 2013-10-28 21:14 | 显示全部楼层
举个例,在windows中打开多个“记事本”,你认为windows会怎样在内存中为这两个“记事本”进程的代码段分配内存?
1、每个“记事本”进程的代码段分配不同的物理内存并映射到各个进程虚空间。
2、所有“记事本”进程的代码段都共享同一段物理内存,映射到各个进程虚空间。
如果楼主把这个问题想明白了,那么你的问题也就明白了。

使用特权

评论回复
wdliming|  楼主 | 2013-10-28 21:20 | 显示全部楼层
john_lee 发表于 2013-10-28 21:14
举个例,在windows中打开多个“记事本”,你认为windows会怎样在内存中为这两个“记事本”进程的代码段分配 ...

帮主,我对操作系统不是很理解啊,电子出身的。能否通俗一点啊??

使用特权

评论回复
john_lee| | 2013-10-28 21:24 | 显示全部楼层
再更简单地说,假设你两个任务的函数是不同的(vTaskFunction1, vTaskFunction2),但这两个任务函数都调用了同一个公用函数(vTaskFunction):
int main( void )
{
  xTaskCreate( vTaskFunction1, ...);
  xTaskCreate( vTaskFunction2, ...);
  vTaskStartScheduler();
  for( ;; );
  return 0;
}

void vTaskFunction( void *pvParameters )
{
  ...
}

void vTaskFunction1( void *pvParameters )
{
  vTaskFunction(pvParameters);
}

void vTaskFunction2( void *pvParameters )
{
  vTaskFunction(pvParameters);
}
这样做你认为也是非法的?如果不是非法的,那么vTaskFunction1和vTaskFunction2又是否有必要存在呢?

使用特权

评论回复
wdliming|  楼主 | 2013-10-28 21:27 | 显示全部楼层
john_lee 发表于 2013-10-28 21:24
再更简单地说,假设你两个任务的函数是不同的(vTaskFunction1, vTaskFunction2),但这两个任务函数都调用 ...

这个我知道,但是我的代码是建立2个任务而只有一个实现函数啊?这和你说的例子类似吗?

使用特权

评论回复
john_lee| | 2013-10-28 21:31 | 显示全部楼层
我举的例子,两个任务函数都运行到了同一个函数中,这不算“只有一个实现函数”吗?

使用特权

评论回复
wdliming|  楼主 | 2013-10-28 21:36 | 显示全部楼层
john_lee 发表于 2013-10-28 21:24
再更简单地说,假设你两个任务的函数是不同的(vTaskFunction1, vTaskFunction2),但这两个任务函数都调用 ...

帮主,貌似我理解了,和函数调用道理差不多,传递的参数不同罢了。

使用特权

评论回复
zhouaheng| | 2014-12-17 21:02 | 显示全部楼层
这是一个任务的二个实例而已,   文档有解释的

使用特权

评论回复
OldMouth| | 2016-2-27 16:12 | 显示全部楼层
zhouaheng 发表于 2014-12-17 21:02
这是一个任务的二个实例而已,   文档有解释的

你好,哪个文档有讲这个?麻烦告知,谢谢帮助

使用特权

评论回复
bbslhb| | 2019-4-11 10:59 | 显示全部楼层
我一个main()函数里能同时delay(100ms)和delay(200ms)吗?如果可以就行。
虽然两个task都调用了vTaskFunction这个函数,但是xTaskCreate()函数的第四个参数(传递给vTaskFunction的形参)不一样。
一个是(void*)pcTextForTask1【相当于200ms】,一个是(void*)pcTextForTask2【相当于200ms】。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:我的未来不是梦

76

主题

913

帖子

4

粉丝