Linux是否使用了一些优先级倒置的方案?
Does Linux use some of the solutions of priority inversion?
众所周知,优先级反转问题 - 当具有较高优先级的线程等待具有较低优先级的线程时:https://en.wikipedia.org/wiki/Priority_inversion
当我们有 3 个线程时会发生这种情况:L(低优先级)、M(中优先级)、H(高优先级)。 L 和 H 使用相同的互斥锁,但 L 比 H 早获得它,H 阻塞并进入休眠状态。然后M占用CPU-core,因为优先级比L高,L进入休眠状态,但是仍然获取到了mutex。 L&H在睡觉,M在工作
有一些solutions的优先级反转:
- 禁用所有中断以保护临界区
- 优先级上限
- 优先继承
- 随机提升 - 持有锁的就绪任务的优先级随机提升,直到它们退出临界区。此解决方案用于 Microsoft Windows.
- 避免阻塞
Linux是否使用了一些优先级倒置的解决方案,使用了哪些?
Linux使用优先继承.
优先级倒置可以用函数解决 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
其中 protocol
是:
PTHREAD_PRIO_NONE
- 获得互斥体所有权时没有任何反应
PTHREAD_PRIO_INHERIT
- Priority inheritance
PTHREAD_PRIO_PROTECT
- 根据函数 pthread_mutexattr_getprioceiling()
获得的 prioceiling
值使用固定级别的优先级
When a thread owns one or more mutexes initialized with the
PTHREAD_PRIO_PROTECT protocol, it shall execute at the higher of its
priority or the highest of the priority ceilings of all the mutexes
owned by this thread and initialized with this attribute, regardless
of whether other threads are blocked on any of these mutexes or not.
众所周知,优先级反转问题 - 当具有较高优先级的线程等待具有较低优先级的线程时:https://en.wikipedia.org/wiki/Priority_inversion
当我们有 3 个线程时会发生这种情况:L(低优先级)、M(中优先级)、H(高优先级)。 L 和 H 使用相同的互斥锁,但 L 比 H 早获得它,H 阻塞并进入休眠状态。然后M占用CPU-core,因为优先级比L高,L进入休眠状态,但是仍然获取到了mutex。 L&H在睡觉,M在工作
有一些solutions的优先级反转:
- 禁用所有中断以保护临界区
- 优先级上限
- 优先继承
- 随机提升 - 持有锁的就绪任务的优先级随机提升,直到它们退出临界区。此解决方案用于 Microsoft Windows.
- 避免阻塞
Linux是否使用了一些优先级倒置的解决方案,使用了哪些?
Linux使用优先继承.
优先级倒置可以用函数解决 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
其中 protocol
是:
PTHREAD_PRIO_NONE
- 获得互斥体所有权时没有任何反应PTHREAD_PRIO_INHERIT
- Priority inheritancePTHREAD_PRIO_PROTECT
- 根据函数pthread_mutexattr_getprioceiling()
获得的
prioceiling
值使用固定级别的优先级
When a thread owns one or more mutexes initialized with the PTHREAD_PRIO_PROTECT protocol, it shall execute at the higher of its priority or the highest of the priority ceilings of all the mutexes owned by this thread and initialized with this attribute, regardless of whether other threads are blocked on any of these mutexes or not.