摘自:

Mastering the FreeRTOS 7.3 Mutexes (and Binary Semaphores)

Priority Inversion

This Figure demonstrates one of the potential pitfalls of using a mutex to provide mutual

exclusion. The sequence of execution depicted shows the higher priority Task 2 having to wait

for the lower priority Task 1 to give up control of the mutex. A higher priority task being

delayed by a lower priority task in this manner is called ‘priority inversion’. This undesirable

behavior would be exaggerated further if a medium priority task started to execute while the

high priority task was waiting for the semaphore—the result would be a high priority task

waiting for a low priority task—without the low priority task even being able to execute. This

worst case scenario is shown in Figure below:

Figure.  A worst case priority inversion scenario

Priority inversion can be a significant problem, but in small embedded systems it can often be

avoided at system design time, by considering how resources are accessed.

Priority Inheritance

OS mutexes and binary semaphores are very similar—the difference being that

mutexes include a basic ‘priority inheritance’ mechanism, whereas binary semaphores do not.

Priority inheritance is a scheme that minimizes the negative effects of priority inversion. It

does not ‘fix’ priority inversion, but merely lessens its impact by ensuring that the inversion is

always time bounded. However, priority inheritance complicates system timing analysis, and it

is not good practice to rely on it for correct system operation.

Priority inheritance works by temporarily raising the priority of the mutex holder to the priority of

the highest priority task that is attempting to obtain the same mutex. The low priority task that

holds the mutex ‘inherits’ the priority of the task waiting for the mutex. This is demonstrated by

Figure below. The priority of the mutex holder is reset automatically to its original value when it

gives the mutex back.

Figure. Priority inheritance minimizing the effect of priority inversion

As just seen, priority inheritance functionality effects the priority of tasks that are using the

mutex. For that reason, mutexes must not be used from an interrupt service routines.

Deadlock (or Deadly Embrace)

‘Deadlock’ is another potential pitfall of using mutexes for mutual exclusion. Deadlock is

sometimes also known by the more dramatic name ‘deadly embrace’.

Deadlock occurs when two tasks cannot proceed because they are both waiting for a resource

that is held by the other. Consider the following scenario where Task A and Task B both need

to acquire mutex X and mutex Y in order to perform an action:

1. Task A executes and successfully takes mutex X.

2. Task A is pre-empted by Task B.

3. Task B successfully takes mutex Y before attempting to also take mutex X—but mutex

X is held by Task A so is not available to Task B. Task B opts to enter the Blocked

state to wait for mutex X to be released.

4. Task A continues executing. It attempts to take mutex Y—but mutex Y is held by Task

B, so is not available to Task A. Task A opts to enter the Blocked state to wait for

mutex Y to be released.

At the end of this scenario, Task A is waiting for a mutex held by Task B, and Task B is waiting for a mutex held by Task A. Deadlock has occurred because neither task can proceed. 最简单的死锁场景:自死锁。一个任务试图去获得一个自己已经持有的锁。 As with priority inversion, the best method of avoiding deadlock is to consider its potential at design time, and design the system to ensure that deadlock cannot occur. In particular, and as previously stated in this book, it is normally bad practice for a task to wait indefinitely (without a time out) to obtain a mutex. Instead, use a time out that is a little longer than the maximum time it is expected to have to wait for the mutex—then failure to obtain the mutex within that time will be a symptom of a design error, which might be a deadlock. In practice, deadlock is not a big problem in small embedded systems, because the system designers can have a good understanding of the entire application, and so can identify and remove the areas where it could occur.

linux 互斥锁优先级反转,互斥锁陷阱:优先级反转、死锁相关推荐

  1. Linux进程管理:内核中的优先级继承互斥(rtmutex.h):防止优先级反转

    目录 Priority inheritance in the kernel 译文 Priority inheritance in the kernel https://lwn.net/Articles ...

  2. 【Linux C 多线程编程】互斥锁与条件变量

    一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1) 初始化: 在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化: 对于静态 ...

  3. Linux多线程开发-线程同步-互斥锁pthread_mutex_t

    1.互斥锁 同一时刻只允许一个线程对临界区进行访问.POSIX库中用类型pthread_mutex_t来定义互斥锁,类型在pthreadtypes.h中定义. 2.如何声明一个互斥锁 #include ...

  4. LINUX线程同步:原子操作、锁、二元信号量、信号量、互斥量、临界区、读写锁、条件变量等

    注:摘自<程序员的自我修养>相关章节. 原子操作 共享数据(全局变量或堆变量)的自增(++)操作在多线程环境下会出现错误是因为这个操作(一条c语句)被编译为汇编代码后不止一条指令,因此在执 ...

  5. linux 只运行一个实例 互斥锁,Linux多线程4-1_互斥量

    //包含头文件 int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_init(pthread_mutex_t *r ...

  6. Java 中15种锁的介绍:公平锁,可重入锁,独享锁,互斥锁,乐观锁,分段锁,自旋锁等等...

    http://blog.51cto.com/13919357/2339446 Java 中15种锁的介绍 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容 ...

  7. Java 中15种锁的介绍:公平锁,可重入锁,独享锁,互斥锁,乐观锁,分段锁,自旋锁等等

    Java 中15种锁的介绍 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁 / 非公平锁 可重入锁 / 不可重入锁 独享锁 / 共享锁 互 ...

  8. java锁(公平锁和非公平锁、可重入锁(又名递归锁)、自旋锁、独占锁(写)/共享锁(读)/互斥锁、读写锁)

    前言 本文对Java的一些锁的概念和实现做个整理,涉及:公平锁和非公平锁.可重入锁(又名递归锁).自旋锁.独占锁(写)/共享锁(读)/互斥锁.读写锁 公平锁和非公平锁 概念 公平锁是指多个线程按照申请 ...

  9. 同步和互斥的POSXI支持(互斥锁,条件变量,自旋锁)

    同步和互斥在多线程和多进程编程中是一个基本的需求,互相协作的多个进程和线程往往需要某种方式的同步和互斥.POSIX定义了一系列同步对象用于同步和互斥. 同步对象是内存中的变量属于进程中的资源,可以按照 ...

  10. 详解各种锁:CAS、共享锁、排它锁、互斥锁、悲观锁、乐观锁、行级锁、表级锁、页级锁、死锁、JAVA对CAS的支持、ABA问题、AQS原理

    共享锁(S锁) 又称为读锁,可以查看但无法修改和删除的一种数据锁.如果事务T对数据A加上共享锁后,则其他事务只能对A再加共享锁,不能加排它锁.获准共享锁的事务只能读数据,不能修改数据. 共享锁下其它用 ...

最新文章

  1. 8.15 12.13-12.16
  2. 无需VR外设,普林斯顿学霸用DeepHand解放你的双手
  3. java创建请求拦截器_80.简单Retrofit+RxJava+日志拦截器结合使用
  4. jQuery快速入门专题
  5. JZOJ 5455. 【NOIP2017提高A组冲刺11.6】拆网线
  6. Python报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte s
  7. linux关闭时间戳是否有影响,linux – 如何让sec正确忽略时间戳
  8. 用户和组相关的配置文件总结
  9. predict函数 R_学习|R语言做机器学习的常用函数总结
  10. java判断对象已死_Java的JVM判断对象已死的基本算法分析
  11. 【转】ABP源码分析四十六:ABP ZERO中的Ldap模块
  12. 如何修改7 服务器配置,centos7修改服务器配置
  13. 列表排序应用FLIP动画(vue)
  14. 正则只能出现特定字符_python正则表达式的简单使用总结
  15. 《程序员代码面试指南》第二章 链表问题 反转部分单向链表
  16. ubuntu下源码安装Python
  17. Java对Internet为什么这么重要?
  18. AR/VR learning (3)--物体的运动与动画(iTween插件的使用)
  19. flash服务器停止响应,Adobe Flash Player已经在Windows 10上停止工作
  20. 记录一次阿里云Mysql 数据库恢复 qp.xb文件恢复数据

热门文章

  1. Day002 20210207
  2. 残差网络 ResNet 为什么能训练出1000层的模型 动手学深度学习v2
  3. git pull问题解决error: cannot lock ref
  4. Google Code Review 如何编写代码评论
  5. 稀缺-我们是如何陷入贫穷与忙碌的 读后感
  6. 589. N叉树的前序遍历
  7. 牛顿法的优缺点及特征
  8. 释放空间后将指针置空
  9. h5商城模板_几个常用H5制作软件、网站推荐
  10. CS231n李飞飞计算机视觉 迁移学习之物体定位与检测上