欢迎光临
梦想从学习开始!

嵌入式操作系统内核原理和开发(通用优先级调度)| 小熊测试

本文主要介绍 嵌入式操作系统内核原理和开发(通用优先级调度)| 小熊测试,小熊希望对大家的学习或者工作具有一定的参考学习价值,在测试领域有所提升和发展。

  相比较其他调度算法而言,时间片的轮转更多的注重公平性。但是,任务与任务之间也是有先后之分的,有的任务我们希望多安排一些时间片,而有的任务我们则希望少安排一些时间片。比较说,如果我们在上网的话,我们就希望上网的操作响应的更快一些;如果我们在进行GUI操作,我们当然就希望图形响应更快一些。这些都是可以理解的,下面我们就绪要对数据结构进行一些修改。
typedef struct _TASK_INFO
{
    UINT32 id;
    UINT32* stack;
    UINT32 size;
    UINT32 context;
    UINT32 priority;
    UINT32 time_slice;
    void (*func)();

}TASK_INFO;
 


  这里的priority就是当前线程的优先级,所以最简单的方法就是根据priority直接分配对应的time_slice。也就是这个函数,


void reset_time_slice ()
{
    int index;

    for(index = 0; index < THREAD_MAX_NUMBER; index++)
        gAllTask[index].time_slice = gAllTask[index].priority + 1;
}
 


  所以,以后每次调度的时候,我们就首先寻找当前最高优先级的任务,看看当前任务安排的时间片是否用完了,没有用完就继续运行。如果当前优先级的任务已经没有时间片了,那么此时就可以安排低优先级的任务进行调度了。


void signal_handler(int m)
{
        int index;

start:
        index = find_next_thread();
        if(-1 == index)
        {
            reset_time_slice();
            goto start;
        }

        gAllTask[index].time_slice –;
        current_thread_id = index;
        swap(&old, &gAllTask[current_thread_id].context);
}
 


  下面,我们就根据任务优先级挑选下一个需要运行的thread了,


int find_next_thread()
{
    int index;

    for(index = THREAD_MAX_NUMBER -1; index >=0; index –)
    {
        if(0 != gAllTask[index].time_slice)
            break;
    }

    return index;     
}


  整个代码的流程也不复杂,大家可以运行、单步调试一把,试试看。


#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>

#define UINT32 unsigned   int
#define STACK_LENGTH      512
#define THREAD_MAX_NUMBER 10

typedef struct _TASK_INFO
{
    UINT32 id;
    UINT32* stack;
    UINT32 size;
    UINT32 context;
    UINT32 priority;
    UINT32 time_slice;
    void (*func)();

}TASK_INFO;

static struct itimerval oldtv;
UINT32 old   = 0;
UINT32 count = 0;
UINT32 task_stack[THREAD_MAX_NUMBER][STACK_LENGTH] = {0};
TASK_INFO gAllTask[THREAD_MAX_NUMBER] = {0};
UINT32 current_thread_id = 0;

void set_timer()
{
        struct itimerval itv;
        itv.it_interval.tv_sec = 1;
        itv.it_interval.tv_usec = 0;
        itv.it_value.tv_sec = 1;
        itv.it_value.tv_usec = 0;
        setitimer(ITIMER_REAL, &itv, &oldtv);
}

void swap(UINT32* prev, UINT32* next)
{
    __asm(“push %%eax “
          “push %%ebx “
          “push %%ecx “
          “push %%edx “
          “push %%esi “
          “push %%edi “
          “push %%ebp “
          “push %%esp “
          “lea 0x8(%%ebp), %%eax “
          “mov (%%eax), %%eax “
          “mov %%esp, (%%eax) “

          “lea 0xc(%%ebp), %%eax “
          “mov (%%eax), %%eax “
          “mov (%%eax), %%esp “
          “pop %%esp “
          “pop %%ebp “
          “pop %%edi “
          “pop %%esi “
          “pop %%edx “
          “pop %%ecx “
          “pop %%ebx “
          “pop %%eax “
          ::);
}

void hello()
{
        int temp = 0;

        while(1) {
            printf(“id = %d, temp = %d, count = %d in thread!”,current_thread_id,  temp ++, count ++);
            swap(&gAllTask[current_thread_id].context, &old);

            printf(“id = %d, temp = %d, count = %d in thread!”,current_thread_id,  temp ++, count ++);
            swap(&gAllTask[current_thread_id].context, &old);
        }
}

int find_next_thread()
{
    int index;

    for(index = THREAD_MAX_NUMBER -1; index >=0; index –)
    {
        if(0 != gAllTask[index].time_slice)
            break;
    }

    return index;     
}

void reset_time_slice ()
{
    int index;

    for(index = 0; index < THREAD_MAX_NUMBER; index++)
        gAllTask[index].time_slice = gAllTask[index].priority + 1;
}

void task_init(int index)
{
        UINT32 unit = gAllTask[index].size;
        UINT32* pData = gAllTask[index].stack;

        memset((void*)pData,(int) 0, unit * sizeof(UINT32));
        pData[unit -1] = (UINT32) gAllTask[index].func;
        pData[unit -2] = 0;
        pData[unit -3] = 0;
        pData[unit -4] = 0;
        pData[unit -5] = 0;
        pData[unit -6] = 0;
        pData[unit -7] = 0;
        pData[unit -8] = 0;
        pData[unit -9] = 0;
        pData[unit -10] = (UINT32) &pData[unit – 9];
        gAllTask[index].context = (UINT32) &pData[unit -10];
}

void signal_handler(int m)
{
        int index;

start:
        index = find_next_thread();
        if(-1 == index)
        {
            reset_time_slice();
            goto start;
        }

        gAllTask[index].time_slice –;
        current_thread_id = index;
        swap(&old, &gAllTask[current_thread_id].context);
}

void set_all_task()
{
        int index;
        memset(gAllTask, 0, sizeof(gAllTask));

        for(index = 0; index < THREAD_MAX_NUMBER; index ++)
        {
            gAllTask[index].id = index;
            gAllTask[index].stack = task_stack[index];
            gAllTask[index].size = STACK_LENGTH;
            gAllTask[index].context = 0;
            gAllTask[index].func = hello;
            gAllTask[index].priority = index;
            gAllTask[index].time_slice = index + 1;

            task_init(index);
        }
}

int main()
{
        char val;

        set_all_task();
        set_timer();
        signal(SIGALRM, signal_handler);

        while(1)
        {
            scanf(“%c”, &val);
        }

        exit(0);
        return 1;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小熊分享邦(www.xxfxb.com),希望大家能坚持软件测试之路,谢谢。

赞(0) 打赏
未经允许不得转载:小熊分享邦 » 嵌入式操作系统内核原理和开发(通用优先级调度)| 小熊测试

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏