这个程序有利于自己对信号量的理解
#include "semLib.h"
#include "taskLib.h"
#include "osdrv.H"
/*测试适配的任务和信号量的函数*/
void taskSub1();
void taskSub2();
XHANDLE semm;
void taskMain() /*主任务:在shell下输入sp taskMain*/
{
XHANDLE subId1,subId2;
semm="SimOs"_CreateSemaphore(1, "no name");/*生成信号量*/
printf("semm OK\n");
subId1=SimOs_CreatThread(taskSub1, 0, 0, XTASK_PRIORITY_ABOVE_NORMAL,XW_DEFAULT, 2000, 0, "taskSub");/*生成子任务,优先级比主任务的100要高*/
printf("taskSub1 spawned\n");
subId2=SimOs_CreatThread(taskSub2, 0, 0, XTASK_PRIORITY_ABOVE_NORMAL,XW_DEFAULT, 2000, 0, "taskSub");/*生成子任务,优先级比主任务的100要高*/
printf("taskSub2 spawned\n");
taskSuspend(0);
if(taskDelete(subId1)!=OK)
printf("delete taskSub error:%d\n",errno);
else
printf("taskSub deleteed.OK\n");
if(taskDelete(subId2)!=OK)
printf("delete taskSub error:%d\n",errno);
else
printf("taskSub deleteed.OK\n");
}
void taskSub1()
{
SimOs_ObtainSemaphore(semm, XW_INFINITE);/*获取信号量*/
taskSuspend(0);
printf("taskSub:resumed and release mutex1a\n");
SimOs_ReleaseSemaphore(semm);/*释放信号量*/
printf("taskSub:resumed and release mutex1b\n");
}
void taskSub2()
{ printf("come into the taskSub2\n");
SimOs_ObtainSemaphore(semm, XW_INFINITE);/*获取信号量*/
printf("taskSub:resumed and release mutex2a\n");
SimOs_ReleaseSemaphore(semm);/*释放信号量*/
printf("taskSub:resumed and release mutex2b\n");
}
用户73924 2006-11-20 21:30