打印
[牛人杂谈]

ucos--互斥型信号量

[复制链接]
628|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ccw1986|  楼主 | 2016-4-18 17:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一:互斥型信号量的理解
    互斥型信号量首先是二值信号量,实现对共享资源的独占式处理,其次互斥型信号量可以在应用程序代码中用于降解优先级的反转问题,这个是它和普通信号量的最本质的区别。
二:优先级反转的问题
   假设现在有三个任务分别是Task1,Task2,Task3,优先级从大到小。程序在运行过程中,Task1和Task2处于挂起(pend)的状态,等待某个事件的发生。这样优先级最低的Task3首先申请到了mutex并开始同共享资源打交道,过了一会儿,Task1等待的事件出现了,那么此时Task1就需要使用共享资源了,于是申请mutex(OSMutexPend)。在这种情况下,OSMutexPend注意到高优先级的任务要使用这个共享资源,于是将Task3的优先级升到比Task1更高的级别,并强制任务调度回到Task3。而Task1只好继续挂起,等待共享资源被释放。同时Task3继续运行,直到Task3调用OSMutexPost(),释放Mutex.在调用OSMutexPost()时,OSMutexPost()会将Task3的优先级恢复到原来的水平,同时还会注意到有个高优先级的任务Task1需要这个Mutex,于是将这个Mutex交给Task1,并做任务切换。
三:uc/os-ii的互斥型信号量由三个部分组成:
   ◆一个标志,指示mutex是否可以使用(0或1)
   ◆一个优先级,准备一旦高优先级的任务需要这个mutex,赋予给占有mutex的任务。
   ◆一个等待该mutex的任务列表

四:程序示例
Void main (void)

{

   OSInit();

   **************应用程序初始化**********************

    OSMutexCreate(9,&err);            

    OSTaskCreate(TaskPrio10,(void *)0,&TaskPrio10Stk[999],10);

    OSTaskCreate(TaskPrio15,(void *)0,&TaskPrio15Stk[999],15);

    OSTaskCreate(TaskPrio20,(void *)0,&TaskPrio20Stk[999],20);

   **************应用程序初始化************************

   OSStart();

}

Void TaskPrio10 (void * pdata)

{

   While(1){

   *************应用程序代码***********************

   OSMutexPend(ResouceMutex,0,&err);

   *************占用共享资源***********************

   OSMutexPost(ResouceMutex);

   *************应用程序代码***********************  

   }

}





Void TaskPrio15 (void * pdata)

{

   While(1){

   *************应用程序代码***********************

   OSMutexPend(ResouceMutex,0,&err);

   *************占用共享资源***********************

   OSMutexPost(ResouceMutex);

   *************应用程序代码***********************

   }

}



Void TaskPrio20 (void * pdata)

{

   While(1){

   *************应用程序代码***********************

   OSMutexPend(ResouceMutex,0,&err);

   *************占用共享资源***********************

   OSMutexPost(ResouceMutex);

   *************应用程序代码***********************

   }

}


沙发
ccw1986|  楼主 | 2016-4-18 17:42 | 只看该作者
五,三个重要函数的应用
1.      OSMutexCreate (INT8U prio, INT8U*err)
Arguments  : prio is the priority to use when accessing themutual exclusion semaphore.  In other words, whenthe semaphore is acquired and a higher priority task attempts toobtain the semaphore then the priority of the task owning thesemaphore is raised to this priority.  It isassumed that you will specify a priority that is LOWER in valuethan ANY of the tasks competing for the mutex.
err is a pointer to an error code which will bereturned to your application:
  OS_NO_ERR             if the callwas successful.
  OS_ERR_CREATE_ISR    if you attempted to create aMUTEX from an ISR
  OS_PRIO_EXIST          ifa task at the priority inheritance priority alreadyexist.
  OS_ERR_PEVENT_NULL   No moreevent control blocks available.
  OS_PRIO_INVALID    if the priority you specify is higher that the maximum allowed(i.e. > OS_LOWEST_PRIO)
Returns   : != (void *)0  is a pointer to the event controlclock (OS_EVENT) associated with the created mutex.
         == (void *)0  if an error is detected.
2. OSMutexPend(OS_EVENT *pevent, INT16U timeout, INT8U *err)
Arguments : pevent is a pointer to the event control blockassociated with the desired mutex.
Returns   : none
2.      OSMutexPost (OS_EVENT *pevent)
Arguments  : pevent is a pointer to the event control blockassociated with the desired mutex.

使用特权

评论回复
板凳
zhuomuniao110| | 2016-4-18 21:44 | 只看该作者
在多数情况下,互斥型信号量和二值型信号非常相似,但是从功能上二值型信号量用于同步,而互斥型信号量用于资源保护。互斥型信号量和二值型信号量还有一个最大的区别,互斥型信号量可以有效解决优先级反转现象。

使用特权

评论回复
地板
Micachl| | 2016-4-18 22:26 | 只看该作者
zhuomuniao110 发表于 2016-4-18 21:44
在多数情况下,互斥型信号量和二值型信号非常相似,但是从功能上二值型信号量用于同步,而互斥型信号量用于 ...

如果我定义一个全局变量,在此时刻保证只在任务1中使用,而其他任务不能使用该如何操作呢

使用特权

评论回复
5
Jessicakjdsl| | 2016-4-20 22:40 | 只看该作者
这个共享资源独占是指的全局变量之类的信号吗

使用特权

评论回复
6
zhuomuniao110| | 2016-4-23 21:57 | 只看该作者
确实抽象难懂,不过还好不常用。

使用特权

评论回复
7
huangcunxiake| | 2016-4-23 23:13 | 只看该作者
在处理过程中优先级还能改变?原来如此

使用特权

评论回复
8
yiyigirl2014| | 2016-4-24 00:01 | 只看该作者
uc/os-ii的互斥型信号量由三个部分组成:
   ◆一个标志,指示mutex是否可以使用(0或1)
   ◆一个优先级,准备一旦高优先级的任务需要这个mutex,赋予给占有mutex的任务。
   ◆一个等待该mutex的任务列表
总结的很好。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

925

帖子

5

粉丝