uc/os-ii Mutex Semaphore and Code Implementation

Mutex semaphore solves priority inversion problem


  • Suppose there are three tasks a, b, c, a priority is higher than b, b priority is higher than c, both a and c need to access a shared resource s, the semaphore protecting this resource is a mutually exclusive semaphore,
  • Suppose that the current task c has applied for semaphore access s, but it has not been released. At this time, task a starts to run, then a will deprive c of the operation and run a. When a accesses resource s, because the semaphore is not available, Therefore, it must be released to wait for the semaphore, and the task c can be re-run. The process is normal here. The design of the semaphore is also to meet this function.

write picture description here

  • However, when task c is running and is ready to release the semaphore, task b starts to run, then task b will deprive task c of running. At this time, only b is running in the system, and a can interrupt the running of b but needs to Semaphore, but c has a low priority and cannot be run, so a can only wait until b runs and releases the right to use it before it can be run.
    write picture description here
    • Priority inversion: a with a higher priority cannot be seized when a with a lower priority b is running, but the deprivable kernel cannot be deprived, and the system fails, which greatly reduces the real-time performance of the system.
  • In order to solve this problem, ucos introduces the method of priority promotion in the mutex semaphore. The basic idea is: briefly raise the priority of the task that currently obtains the mutex semaphore to the maximum priority acceptable to the system, try to make the task complete as quickly as possible and release the semaphore, and restore the task's original priority after the release.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325265533&siteId=291194637