OSSemPend是会把当前的任务挂起,直到另外一个任务的OSSemPost 运行完毕都得到Sem。但是可以通过改变OSSemCreate(x)里面的值x改变这种局面,当x不为0时,OSSemPend会马上得到Sem继续运行当前任务至结束,并将x的数值减一,直到为0。为0后,只有等其他任务的OSSemPost了。
为什么UC/OS-II创建互斥信号量失败?该怎么处理/*申请两个信号量,用于同步*/
SempEmpty=OSSemCreate(BUFFER_SIZE)
if(SempEmpty==NULL)
{
printf( "Create semophore fail.\n ")
//return
}
SempFull=OSSemCreate(0)
if(SempFull==NULL)
{
printf( "Create semophore fail.\n ")
//return
}
/*申请一个互斥信号量,用于任务间互斥*/
SempMutex=OSMutexCreate(2,&err)
if(SempMutex==NULL)
{
printf( "Create mutex fail.\n ")//为什么创建不成功啊??
//return
}
前两个信号量创建都成功,但创建的互斥信号粮总是返回NULL,奇怪啊,把SempMutex=OSMutexCreate(2,&err)
这句移到SempEmpty=OSSemCreate(BUFFER_SIZE)这句的前面最后创建的哪个信号量又失败!,也就是创建三个信号量,最后哪个总是失败(返回NULL)!!
------解决方案--------------------
LZ可以看看ucos2总共可以申请的muxsem是多少 我记得是可以配置的
OSSemPost()结束前会引发一次调度OS_Sched(),选择就绪表中优先级最高的任务运行,如果任务1的优先级高,会运行任务1,实时操作系统的任务都会被及时运行void OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr
#endif
if (OSIntNesting >0) { /* See if called from ISR ...*/
*err = OS_ERR_PEND_ISR /* ... can't PEND from an ISR*/
return
}
#if OS_ARG_CHK_EN >0
if (pevent == (OS_EVENT *)0) {/* Validate 'pevent' */
*err = OS_ERR_PEVENT_NULL
return
}
if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
*err = OS_ERR_EVENT_TYPE
return
}
#endif
OS_ENTER_CRITICAL()
if (pevent->OSEventCnt >0) { /* If sem. is positive, resource available ... */
pevent->OSEventCnt--/* ... decrement semaphore only if positive. */
OS_EXIT_CRITICAL()
*err = OS_NO_ERR
return
}
/* Otherwise, must wait until event occurs */
OSTCBCur->OSTCBStat |= OS_STAT_SEM /* Resource not available, pend on semaphore */
OSTCBCur->OSTCBDly = timeout /* Store pend timeout in TCB */
OS_EventTaskWait(pevent)/* Suspend task until event or timeout occurs*/
OS_EXIT_CRITICAL()
OS_Sched() /* Find next highest priority task ready */
OS_ENTER_CRITICAL()
if (OSTCBCur->OSTCBStat &OS_STAT_SEM) { /* Must have timed out if still waiting for event*/
OS_EventTO(pevent)
OS_EXIT_CRITICAL()
*err = OS_TIMEOUT /* Indicate that didn't get event within TO */
return
}
OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0
OS_EXIT_CRITICAL()
*err = OS_NO_ERR
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)