您的位置:首页 > 其它

vxWorks的二值信号量示例

2016-02-08 20:57 811 查看
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: