The priority rollover problem of multi-threaded programming

Source: Internet
Author: User
Tags semaphore

First, what is the priority rollover problem

Priority inversion is when a high-priority task accesses a shared resource through a semaphore mechanism, the semaphore is already occupied by a low-priority task, While this low-priority task may be preempted by some other medium-priority tasks while accessing shared resources, high-priority tasks are blocked by many lower-priority tasks and are difficult to guarantee in real time.

For example, there are priority A, B, and C three tasks, priority a>b>c, task A, a is suspended, waiting for an event to occur, task C is running, and task C starts using a shared resource s. In use, task a waits for the event to arrive and task a becomes ready because it is higher than task C, so it executes immediately. When task a wants to use shared resource S, task A is suspended because it is being used by task C, and task C starts running. If task B waits for an event to arrive at this point, task B becomes the ready state. Task B starts running because task B has a higher priority than task C, and task C starts running until it finishes running. Task A is not executed until task C frees the shared resource S. In this case, the priority has flipped and Task B runs before task a .

Ii. Solutions

There are two ways to resolve priority rollover issues with priority ceilings and priority inheritance .

1.1 Priority Ceiling (Ceiling)

The priority ceiling is when a task requests a resource, the priority of the task is raised to the highest priority in all tasks that can access the resource , which is called the priority ceiling of the resource. This approach is easy and does not have to be complex to judge whether a task is blocking the operation of a high-priority task, as long as the task accesses the shared resource and the task's priority is increased.

1.2 Precedence Inheritance (priority inheritance)

Priority inheritance is when task a requests shared resources s, if s is being used by task C, by comparing the priority of task C with itself, such as finding that task C is less than its priority, the priority of task C is raised to its priority, and task C releases the resource s, then restores the original priority of task C /c8>. This approach changes the priority of tasks dynamically only when the low-priority tasks that occupy the resources block high-priority tasks, and if the process is more complex, it needs to be judged.

Third, the inspiration to the programmer

In general, priority inversion is unlikely to happen for the people writing the application layer, but it is a problem for the designer of the operating system.

As an application developer, you should try to avoid the issue of priority rollover. Therefore, the following points need to be noted:

    1. Priority reversal reminds us that the code snippet to use the lock should be as short as possible ;
    2. Pay attention to the use of small locks instead of large locks, reducing the chance of conflict;
    3. If the lock-protected code segment is short, it is a good idea to use atomic lock busy.

Summary:

This paper mainly writes about the possibility of multi-thread priority rollover in the operating system scheduling process, and the application developer should also pay attention to some optimizations for this problem.

The priority rollover problem of multi-threaded programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.