UCOS2 O (1) algorithm for process scheduling

Source: Internet
Author: User

UCOS2 the only place worth learning is the O (1) algorithm for process scheduling :

The simplest and most foolish way is to maintain a list of lists.

The problem with this approach is that when a thread is ready, the algorithm has a time complexity of O (n) if it is inserted into list according to its priority.

Linux uses the BITMAP,UCOS2 is no exception. Of course uCOS2 is easier to handle because uCOS2 must be configured at system compile time to support the maximum number of thread to allocate bitmap.

For example:

Supports 64, then allocates 8 bytes of bitmap, and if 256 is supported, allocates 16 bytes of bitmap.

Each bit of the bitmap represents the state of the thread, and if bit is 1, 0 indicates pending.

Take 64 as an example:

How can I quickly find a bit of 1 in bitmap and convert it to the priority of thread?

If a scan is used, bitwise AND operation is not a good method, and there are loops and counts involved. So the operating time and thread count are again O (N).

So uCOS2 uses the method of grouping: 8 bytes 64BIT, divided into 8*8,.

Use the bit of the OSRDYGRP variable to indicate which packet has the thread in place, then extract the packet, and then find the highest priority thread from the group.

Thread priority precedence and grouping relationships. That is, how X, y form priority.

One more question: How do I find the highest-priority thread? the smaller the priority value, the higher the thread precedence.

  For example osrdygrp = 0x10001000,osrdytbl[3]=0x10001000.

It is obvious that the osrdygrp of the BIT4 and Osrdytbl[3] BIT4 represents the thread 27 that we want.

In order to solve this problem, UCOS2 also adopted a fast conversion table OSMAPTBL.

The conversion table works as follows: 0x10001000---> Conversion table---->3

0x00001000---> Conversion table--->3

1 bytes 8BIT, there are 256 bit combinations, but the combination of the return value can be calculated in advance, there is osmaptbl.

For example, osrdygrp=0x11111111 and osrdygrp=0x00000001 should return 0. [0 represents the first group]

So Osmaptbl is as follows:

int8u Const OSUNMAPTBL[256] = {
0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x00 to 0x0F * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x10 to 0x1F * /
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x20 to 0x2F * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x30 to 0x3F * /
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x40 to 0x4f * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x50 to 0x5f * /
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x60 to 0x6F * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x70 to 0x7F * /
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x80 to 0x8F * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0x90 to 0x9F * /
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0xA0 to 0xAF * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0xb0 to 0xBF * /
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0xC0 to 0xCF * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0xD0 to 0xDF * /
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 0xE0 to 0xEF * /
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0/* 0xF0 to 0xFF * /
}; It can be seen that osmaptbl[0] and osmaptbl[255] are all 0.

This article link: http://www.cnblogs.com/cposture/p/4291540.html

UCOS2 O (1) algorithm for process scheduling

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.