导 航┆主 站┆文 章┆资 料┆下 载┆美 食┆读 书┆笑 话┆博 客┆论 坛
>> Tony嵌入式开发论坛静态版首页
查看完全版本:Tony嵌入式开发论坛

 

    请教多任务编程
    作者:tony001 时间:2008-9-9 21:58:04

    以前用的C在windows 下 的编程一般是由一个main()作为程序入口,然后程序根据函数在main里的顺序依次执行,子函数定义可以写在main前或者把声明放在前面 ,程序依次调用,从结构上来说是比较清楚的 。
    那么作为多任务环境下的编程,是否有这么一个主函数呢?我的理解这个角色就是初始化主任务,在这里一般会spawn需要用到的任务,之后就是个任务的实现。
    void tmain(void)
    {
    task1=taskSpawn( , , , (FUNCPTR)taskone,。。。。)
    task2=taskSpawn( , , , (FUNCPTR)tasktwo。。。。)
    task3=taskSpawn( , , , (FUNCPTR)taskthree。。。。)
    }

    void taskone(void)
    {
    ....
    }

    void tasktwo(void)
    {
    ....
    }

    void taskthree(void)
    {
    ....
    }

    但是在实际编程中,各任务是如何互相协调,同时工作的,以结构化编程的想法似乎不好理解,请大家多多指教,多多探讨,最好有例子奉献一下,

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------


    作者:maomaotr 时间:2008-9-9 22:45:59

    买本书看吧!一两句说不清楚的!要靠自己的实践!其实跟VC的线程也是类似的,

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------


    作者:maomaotr 时间:2008-9-9 22:44:17

    /* msgQDemo.h - Header for the msgQDemo */

    /* Copyright 1984-1997 Wind River Systems, Inc. */

    /*
    modification history
    --------------------
    01b,06nov97,mm added copyright.
    01a,14dec93,ms written.
    */

    #define CONSUMER_TASK_PRI 99 /* Priority of the consumer task */
    #define PRODUCER_TASK_PRI 98 /* Priority of the producer task */
    #define TASK_STACK_SIZE 5000 /* stack size for spawned tasks */

    struct msg { /* data structure for msg passing */
    int tid; /* task id */
    int value; /* msg value */
    };

    LOCAL MSG_Q_ID msgQId; /* message queue id */
    LOCAL int numMsg = 8; /* number of messages */
    LOCAL BOOL notDone; /* Flag to indicate the completion
    of this demo */

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------


    作者:maomaotr 时间:2008-9-9 22:43:43

    /* msgQDemo.c - Demonstrates intertask communication using Message Queues */

    /* Copyright 1984-1997 Wind River Systems, Inc. */

    /*
    modification history
    --------------------
    01c,06nov97,mm added copyright.
    01b,19sep97,ram added include files stdio.h, sysLib.h
    tested ok
    01a,14dec93,ms written.
    */

    /* includes */

    #include "vxWorks.h"
    #include "taskLib.h"
    #include "msgQLib.h"
    #include "msgQDemo.h"
    #include "sysLib.h"
    #include "stdio.h"

    /* function prototypes */

    LOCAL STATUS producerTask (); /* producer task */
    LOCAL STATUS consumerTask (); /* consumer task */

    /*****************************************************************************
    * msgQDemo - Demonstrates intertask communication using Message Queues
    *
    * DESCRIPTION
    * Creates a Message Queue for interTask communication between the
    * producerTask and the consumerTask. Spawns the producerTask that creates
    * messages and sends messages to the consumerTask using the message queue.
    * Spawns the consumerTask that reads messages from the message queue.
    * After consumerTask has consumed all the messages, the message queue is
    * deleted.
    *
    * RETURNS: OK or ERROR
    *
    * EXAMPLE
    *
    * -> sp msgQDemo
    *
    */

    STATUS msgQDemo()
    {
    notDone = TRUE; /* initialize the global flag */

    /* Create the message queue*/
    if ((msgQId = msgQCreate (numMsg, sizeof (struct msg), MSG_Q_FIFO))
    == NULL)
    {
    perror ("Error in creating msgQ");
    return (ERROR);
    }

    /* Spwan the producerTask task */
    if (taskSpawn ("tProducerTask", PRODUCER_TASK_PRI, 0, TASK_STACK_SIZE,
    (FUNCPTR) producerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    == ERROR)
    {
    perror ("producerTask: Error in spawning demoTask");
    return (ERROR);
    }

    /* Spwan the consumerTask task */
    if (taskSpawn ("tConsumerTask", CONSUMER_TASK_PRI, 0, TASK_STACK_SIZE,
    (FUNCPTR) consumerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    == ERROR)
    {
    perror ("consumerTask: Error in spawning demoTask");
    return (ERROR);
    }

    /* polling is not recommended. But used to make this demonstration simple*/
    while (notDone)
    taskDelay (sysClkRateGet ());

    if (msgQDelete (msgQId) == ERROR)
    {
    perror ("Error in deleting msgQ");
    return (ERROR);
    }

    return (OK);
    }


    /*****************************************************************************
    * producerTask - produces messages, and sends messages to the consumerTask
    * using the message queue.
    *
    * RETURNS: OK or ERROR
    *
    */

    STATUS producerTask (void)
    {
    int count;
    int value;
    struct msg producedItem; /* producer item - produced data */

    printf ("producerTask started: task id = %#x \n", taskIdSelf ());

    /* Produce numMsg number of messages and send these messages */

    for (count = 1; count <= numMsg; count++)
    {
    value = count * 10; /* produce a value */

    /* Fill in the data structure for message passing */
    producedItem.tid = taskIdSelf ();
    producedItem.value = value;

    /* Send Messages */
    if ((msgQSend (msgQId, (char *) &producedItem, sizeof (producedItem),
    WAIT_FOREVER, MSG_PRI_NORMAL)) == ERROR)
    {
    perror ("Error in sending the message");
    return (ERROR);
    }
    else
    printf ("ProducerTask: tid = %#x, produced value = %d \n",
    taskIdSelf (), value);
    }

    return (OK);
    }

    /*****************************************************************************
    * consumerTask - consumes all the messages from the message queue.
    *
    * RETURNS: OK or ERROR
    *
    */

    STATUS consumerTask (void)
    {
    int count;
    struct msg consumedItem; /* consumer item - consumed data */

    printf ("\n\nConsumerTask: Started - task id = %#x\n", taskIdSelf());

    /* consume numMsg number of messages */
    for (count = 1; count <= numMsg; count++)
    {
    /* Receive messages */
    if ((msgQReceive (msgQId, (char *) &consumedItem,
    sizeof (consumedItem), WAIT_FOREVER)) == ERROR)
    {
    perror ("Error in receiving the message");
    return (ERROR);
    }
    else
    printf ("ConsumerTask: Consuming msg of value %d from tid = %#x\n",
    consumedItem.value, consumedItem.tid);
    }

    notDone = FALSE; /* set the global flag to FALSE to indicate completion*/
    return (OK);
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------



查看完全版本:Tony嵌入式开发论坛
Copyright 2006-2008 Cevx.Com Cevx.Net 制作 版权所有
网友发帖仅代表个人观点,与本论坛立场无关