@songpfei
2016-03-08T08:19:40.000000Z
字数 2886
阅读 2830
OJ_算法
一、背景介绍
RTOSck是中软欧拉开发部自研的一款嵌入式实时操作系统。主要面向中低端嵌入式环境,具有体积小、效率高、易维测等特定。为了实现无上下文及栈切换的高效业务处理,RTOSck支持一种称为软中断的线程机制。软中断具有与中断类似的特性,支持优先级及优先级抢占,处理过程不能挂起。与硬中断通过硬件激活不同,软中断需要通过主动调用软中断激活函数进行激活。
二、题目描述
请模拟实现一个简单软中断调度器。该软中断调度器支持32个优先级(0~31,数值越小,优先级越高)。并支持如下调度行为:
1、在低优先级软中断中激活高优先级软中断,高优先级软中断将立即抢占执行;
2、在高优先级软中断中激活低优先级软中断,需要在高优先级软中断执行完成后才能得到调度;
3、低优先级软中断需要在所有直接或间接抢占的所有高优先级软中断执行完成后,再次恢复执行;
4、同优先级软中断按照先入先出顺序进行调度;
5、同一软中断可以连续多次激活,并响应同样多次。
三、 代码实现
/******************************************************************************Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.******************************************************************************File Name :Version :Author :Created : 2010/3Last Modified :Description :Function List :History :1.Date : 2010/3Author :Modification: Created file******************************************************************************/#include<vector>#include<queue>#include<map>using namespace std;typedef struct tag_SWI{unsigned int prio;//优先级unsigned int swiId;//软中断IDvoid (* proc)(void);//中断处理函数friend bool operator <(tag_SWI swi1,tag_SWI swi2){return swi1.prio>swi2.prio;}} SWI;priority_queue<SWI> g_schedule_quene;//按照优先级排列的激活中断map<int,SWI> g_swi_creat_map;//创建的软中断int g_running_swiID=-1; //正在运行的软件中断/*************************************************************************************************函数说明:创建软中断输入参数:swiId: 创建软中断ID;prio: 创建软中断优先级;proc: 创建软中断处理函数。输出参数:无返回值 :成功返回0, 其它情况返回-1**************************************************************************************************/int SwiCreate(unsigned int swiId, unsigned int prio, void (* proc)(void)){//TODO: 添加代码...if(prio< 0 || prio>31)//优先级无效,创建中断失败return -1;if(proc==NULL)//处理函数为空return -1;if(g_swi_creat_map.count(swiId))//ID为swiId 的中断已存在,重复创建return -1;SWI swi_temp;swi_temp.prio=prio;swi_temp.swiId=swiId;swi_temp.proc=proc;g_swi_creat_map[swiId]=swi_temp;return 0;}/*************************************************************************************************函数说明:软中断激活输入参数:swiId: 待激活软中断ID输出参数:无返回值 :成功返回0, 其它情况返回-1**************************************************************************************************/int SwiActivate(unsigned int swiId){//TODO: 添加代码...if(!g_swi_creat_map.count(swiId))//swiId无效或中断未创建return -1;g_schedule_quene.push(g_swi_creat_map[swiId]);while(!g_schedule_quene.empty()){SWI swi_top=g_schedule_quene.top();//优先级最高if(swi_top.swiId!= g_running_swiID){int old_running_swiID=g_running_swiID;g_running_swiID=swi_top.swiId;swi_top.proc();g_running_swiID=old_running_swiID;g_schedule_quene.pop();}else{break;}}return 0;}/*************************************************************************************************函数说明:清空所有的信息输入参数:无输出参数:无返回值 :无**************************************************************************************************/void Clear(void){//TODO: 添加代码...while(!g_schedule_quene.empty())g_schedule_quene.pop();g_swi_creat_map.clear();g_running_swiID=-1;}
四、参考:http://blog.csdn.net/dszgf5717/article/details/23034099