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); }
相关文章推荐
- rhel6 下删除多余的内核版本
- 学习记录 - 1
- html中dl,dt,dd标签的用法
- 基于AT89C51教程之软件篇(二)学习前的知识储备
- Cocos2d-x 3.2在mac下android的环境配置
- HDU3336 Count the string(KMP)
- javaweb入门
- 计算广告与机器学习-技术共享平台
- HQL和SQL的区别
- The Java™ Tutorials — Generics :Restrictions on Generics 泛型约束
- 在Centos6.5上安装xen的两种方式
- R语言学习路线和常用数据挖掘包
- python 基础3
- android用shape画虚线,怎么也不显示
- 归并排序(Merge Sort)
- hdu 1561 The more, The Better 树形dp
- hdu 1050 Moving Tables
- 使用Windows Live Writer离线写csdn博客
- MongoDB的简单操作
- 朴素贝叶斯分类器本质上是线性分类器