vxWorks的二值信号量示例


semLib版本vxWorks6.8的源码位置在:\WindRiver3.8\vxworks-6.8\target\usr\src\wind\semLib.c


下面是测试示例:

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



SEM_ID semId; 
LOCAL SEM_ID semId1;            /* semaphore id of binary semaphore 1 */
LOCAL SEM_ID semId2;            /* semaphore id of binary semaphore 2 */


LOCAL BOOL notDone;                /* flag to indicate completion */
LOCAL STATUS taskA ();
LOCAL STATUS taskB ();


//测试入口
STATUS TestBSem()
{
    notDone = TRUE;
 
    /* semaphore semId1 is availble  after creation*/
    if ((semId1 = semBCreate (SEM_Q_PRIORITY, SEM_FULL)) == NULL)
    {
        perror ("synchronizeDemo: Error in creating semId1 semaphore");
        return (ERROR);
    }

    /* semaphore semId2 is not available  after creation*/
    if ((semId2 = semBCreate (SEM_Q_PRIORITY, SEM_EMPTY)) == NULL)
    {
        perror ("synchronizeDemo: Error in creating semId2 semaphore");
        return (ERROR);
    }

    /* Spwan taskA*/
    if (taskSpawn ("tTaskA", 98, 0, 5000, (FUNCPTR) taskA, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR)
    {
        perror ("synchronizeDemo: Error in spawning taskA");
        return (ERROR);
    }

    /* Spwan taskB*/
    if (taskSpawn ("tTaskB", 98, 0, 5000, (FUNCPTR) taskB, 0,
                   0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR)
    {
        perror ("synchronizeDemo: Error in spawning taskB");
        return (ERROR);
    }
    
    /* Polling is not recommended. But used for simple demonstration purpose */
    while (notDone) 
    {
        taskDelay (sysClkRateGet());  /* wait here until done */
    }
	
	
    /* Delete the created semaphores */
    if (semDelete (semId1) == ERROR)
    {
        perror ("syncronizeDemo: Error in deleting semId1 semaphore");
        return (ERROR);
    }
    if (semDelete (semId2) == ERROR)
    {
       perror ("syncronizeDemo: Error in deleting semId1 semaphore");
       return (ERROR);
    }
    printf ("\n\n synchronizeDemo now completed \n");
    
    return (OK);
}

/*****************************************************************************
 *  taskA - executes event A first and wakes up taskB to excute event B next
 *          using binary semaphores for synchronization.
 *
 *  RETURNS: OK or ERROR
 *
 */

LOCAL STATUS taskA ()
{
    int count;

    for (count = 0; count < 3; count++)
    {
        if (semTake (semId1, WAIT_FOREVER) == ERROR)
        {
            perror ("taskA: Error in semTake");
            return (ERROR);
        } 
        printf ("taskA: Started first by taking the semId1 semaphore - %d times\n",
                (count + 1));
        printf("This is task  <%s> : Event A now done\n", taskName(taskIdSelf()));
        printf("taskA: I'm done, taskB can now proceed; Releasing semId2 semaphore\n\n");
        if (semGive (semId2) == ERROR)
        {
            perror ("taskA: Error in semGive");
            return (ERROR);
        }
    }
    return (OK);
}


/*****************************************************************************
 *  taskB - executes event B first and wakes up taskA to excute event A next
 *          using binary semaphores for synchronization.
 *
 *  RETURNS: OK or ERROR
 *
 */
LOCAL STATUS taskB()
    {
    int count;

    for (count = 0; count < 3; count++)
    {
        if (semTake (semId2,WAIT_FOREVER) == ERROR)
        {
            perror ("taskB: Error in semTake");
            return (ERROR);
        } 
        printf ("taskB: Synchronized with taskA's release of semId2 - %d times\n",
                (count + 1 ));
        printf("This is task  <%s> : Event B now done\n", taskName (taskIdSelf()));
        printf("taskB: I'm done, taskA can now proceed; Releasing semId1 semaphore\n\n\n");
        if (semGive (semId1) == ERROR)
        {
            perror ("taskB: Error in semGive");
            return (ERROR);
        }
    }
    notDone = FALSE;
    return (OK);
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值