vxWorks互斥信号量示例



#include "vxWorks.h"
#include "semLib.h"
#include "taskLib.h"
#include "logLib.h"
#include "sysLib.h"
#include "stdio.h"


#define  CONSUMER_TASK_PRI           98  /* Priority of the consumerTask task*/
#define  PRODUCER_TASK_PRI           99  /* Priority of the producerTask task*/

#define PRODUCED  1                      /* Flag to indicate produced status*/ 
#define CONSUMED  0                      /* Flag to indicate consumed status*/
#define NUM_ITEMS 5                      /* Number of items */
struct shMem                             /* Shared Memory data structure */
{
    int tid;                             /* task id */  
    int count;                           /* count  number of item produced */
    int status;                          /* 0 if consumed or 1 if produced*/
};


LOCAL STATUS protectSharedResource ();      /* protect shared data structure */
LOCAL STATUS releaseProtectedSharedResource (); /* release protected access */
LOCAL STATUS producerTask ();               /* producer task */
LOCAL STATUS consumerTask ();               /* consumer task */
LOCAL struct shMem shMemResource;           /* shared memory structure */
LOCAL SEM_ID mutexSemId;                    /* mutual exclusion semaphore id*/
LOCAL BOOL notFinished;                     /* Flag that indicates the 
                                             * completion */

 
STATUS TestMSem()
{
    notFinished = TRUE;  /* initialize the global flag */

 
    /* Create the mutual exclusion semaphore*/
    if ((mutexSemId = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE
                                          | SEM_INVERSION_SAFE)) == NULL)
    {
        perror ("Error in creating mutual exclusion semaphore");
        return (ERROR);
    }
 
 
    /* Spwan the consumerTask task */
    if (taskSpawn ("tConsumerTask", CONSUMER_TASK_PRI, 0, 5000, 
                   (FUNCPTR) consumerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 
                   == ERROR)
        {
        perror ("consumerTask: Error in spawning demoTask");
        return (ERROR);
        }

     /* Spwan the producerTask task */
    if (taskSpawn ("tProducerTask", PRODUCER_TASK_PRI, 0, 5000, 
                   (FUNCPTR) producerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 
                   == ERROR)
    {
        perror ("producerTask: Error in spawning demoTask");
        return (ERROR);
    } 
 
    /* Polling is not recommended. But used for making this demonstration 
     * simple */

    while (notFinished)
        taskDelay (sysClkRateGet ());

    /* When done delete the mutual exclusion semaphore*/ 
    if (semDelete (mutexSemId) == ERROR)
    {
       perror ("Error in deleting mutual exclusion semaphore");
       return (ERROR);
    }
 
     return (OK);
}

/*****************************************************************************
 *  producerTask - produce the message, and write the message to the global 
 *                 shared data structure by obtaining exclusive access to 
 *                 that structure which is shared with the consumerTask. 
 *
 *  RETURNS: OK or ERROR
 *
 */

STATUS producerTask ()
{
    int count = 0;
    int notDone = TRUE;
    printf("in producerTask\n");
    while (notDone)
    {

        /* Produce NUM_ITEMS, write each of these items to the shared
         * global data structure.
         */
 
        if (count < NUM_ITEMS)
        {

            /* Obtain exclusive access to the global shared data structure */
            if (protectSharedResource() == ERROR)
                return (ERROR);

            /* Access and manipulate the global shared data structure */
            if (shMemResource.status == CONSUMED)
                { 
                count++;
                shMemResource.tid = taskIdSelf ();
                shMemResource.count = count;
                shMemResource.status = PRODUCED;
                }
            /* Release exclusive access to the global shared data structure */
            if (releaseProtectedSharedResource () == ERROR)
                return (ERROR);

            logMsg ("ProducerTask: tid = %#x, producing item = %d\n", 
                                                       taskIdSelf (), count,0,0,0,0);
            taskDelay (sysClkRateGet()/6); /* relingiush the CPU so that 
                                            * consumerTask can access the 
                                            * global shared data structure. 
                                            */
        }
        else 
            notDone = FALSE;
    }

    return (OK);
}


/*****************************************************************************
 *  consumerTask -  consumes the message from the global shared data 
 *                  structure and updates the status filled to CONSUMED 
 *                  so that producerTask can put the next produced message 
 *                  in the global shared data structure. 
 *
 *  RETURNS: OK or ERROR
 *
 */

STATUS consumerTask ()
{
    int notDone = TRUE;
    
    printf("in consumerTask\n");

    /* Initialize to consumed status */
    if (protectSharedResource() == ERROR)
        return (ERROR);
    shMemResource.status = CONSUMED;
    if (releaseProtectedSharedResource () == ERROR)
        return (ERROR);

    while (notDone) 
    {
        taskDelay (sysClkRateGet()/6);     /* relingiush the CPU so that 
                                            * producerTask can access the 
                                            * global shared data structure. 
                                            */

        /* Obtain exclusive access to the global shared data structure */
        if (protectSharedResource() == ERROR)
            return (ERROR);

        /* Access and manipulate the global shared data structure */
        if ((shMemResource.status == PRODUCED) && (shMemResource.count > 0)) 
            {
            logMsg ("ConsumerTask: Consuming item = %d from tid = %#x\n\n",
                shMemResource.count, shMemResource.tid,0,0,0,0);
            shMemResource.status = CONSUMED;
            }
        if (shMemResource.count >= NUM_ITEMS)
            notDone = FALSE;

        /* Release exclusive access to the global shared data structure */
        if (releaseProtectedSharedResource () == ERROR)
            return (ERROR);


    }

    notFinished = FALSE;
    return (OK);
}


/*****************************************************************************
 *  protectSharedResource -  Protect access to the shared data structure with 
 *                           the mutual exclusion  semaphore.
 *
 *  RETURNS: OK or ERROR
 *
 */
LOCAL STATUS protectSharedResource ()
    {
    if (semTake (mutexSemId, WAIT_FOREVER) == ERROR)
        {
        perror ("protectSharedResource: Error in semTake");
        return (ERROR);
        }
    else
        return (OK);
    }

/*****************************************************************************
 *  releaseProtectedSharedResource -  Release the protected access to the 
 *                                    shared data structure using the mutual 
 *                                    exclusion semaphore
 *
 *  RETURNS: OK or ERROR
 *
 */
LOCAL STATUS releaseProtectedSharedResource ()
    {
    if (semGive (mutexSemId) == ERROR)
        {
        perror ("protectSharedResource: Error in semTake");
        return (ERROR);
        }
    else
        return (OK);
    }









  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值