[关闭]
@songpfei 2016-03-08T08:19:40.000000Z 字数 2886 阅读 2416

华为OJ RTOSck-软中断调度器

OJ_算法


一、背景介绍
RTOSck是中软欧拉开发部自研的一款嵌入式实时操作系统。主要面向中低端嵌入式环境,具有体积小、效率高、易维测等特定。为了实现无上下文及栈切换的高效业务处理,RTOSck支持一种称为软中断的线程机制。软中断具有与中断类似的特性,支持优先级及优先级抢占,处理过程不能挂起。与硬中断通过硬件激活不同,软中断需要通过主动调用软中断激活函数进行激活。

二、题目描述
请模拟实现一个简单软中断调度器。该软中断调度器支持32个优先级(0~31,数值越小,优先级越高)。并支持如下调度行为:
1、在低优先级软中断中激活高优先级软中断,高优先级软中断将立即抢占执行;
2、在高优先级软中断中激活低优先级软中断,需要在高优先级软中断执行完成后才能得到调度;
3、低优先级软中断需要在所有直接或间接抢占的所有高优先级软中断执行完成后,再次恢复执行;
4、同优先级软中断按照先入先出顺序进行调度;
5、同一软中断可以连续多次激活,并响应同样多次。

三、 代码实现

  1. /******************************************************************************
  2. Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.
  3. ******************************************************************************
  4. File Name :
  5. Version :
  6. Author :
  7. Created : 2010/3
  8. Last Modified :
  9. Description :
  10. Function List :
  11. History :
  12. 1.Date : 2010/3
  13. Author :
  14. Modification: Created file
  15. ******************************************************************************/
  16. #include<vector>
  17. #include<queue>
  18. #include<map>
  19. using namespace std;
  20. typedef struct tag_SWI
  21. {
  22. unsigned int prio;//优先级
  23. unsigned int swiId;//软中断ID
  24. void (* proc)(void);//中断处理函数
  25. friend bool operator <(tag_SWI swi1,tag_SWI swi2)
  26. {
  27. return swi1.prio>swi2.prio;
  28. }
  29. } SWI;
  30. priority_queue<SWI> g_schedule_quene;//按照优先级排列的激活中断
  31. map<int,SWI> g_swi_creat_map;//创建的软中断
  32. int g_running_swiID=-1; //正在运行的软件中断
  33. /*************************************************************************************************
  34. 函数说明:创建软中断
  35. 输入参数:
  36. swiId: 创建软中断ID;
  37. prio: 创建软中断优先级;
  38. proc: 创建软中断处理函数。
  39. 输出参数:无
  40. 返回值 :成功返回0, 其它情况返回-1
  41. **************************************************************************************************/
  42. int SwiCreate(unsigned int swiId, unsigned int prio, void (* proc)(void))
  43. {
  44. //TODO: 添加代码...
  45. if(prio< 0 || prio>31)//优先级无效,创建中断失败
  46. return -1;
  47. if(proc==NULL)//处理函数为空
  48. return -1;
  49. if(g_swi_creat_map.count(swiId))//ID为swiId 的中断已存在,重复创建
  50. return -1;
  51. SWI swi_temp;
  52. swi_temp.prio=prio;
  53. swi_temp.swiId=swiId;
  54. swi_temp.proc=proc;
  55. g_swi_creat_map[swiId]=swi_temp;
  56. return 0;
  57. }
  58. /*************************************************************************************************
  59. 函数说明:软中断激活
  60. 输入参数:swiId: 待激活软中断ID
  61. 输出参数:无
  62. 返回值 :成功返回0, 其它情况返回-1
  63. **************************************************************************************************/
  64. int SwiActivate(unsigned int swiId)
  65. {
  66. //TODO: 添加代码...
  67. if(!g_swi_creat_map.count(swiId))//swiId无效或中断未创建
  68. return -1;
  69. g_schedule_quene.push(g_swi_creat_map[swiId]);
  70. while(!g_schedule_quene.empty())
  71. {
  72. SWI swi_top=g_schedule_quene.top();//优先级最高
  73. if(swi_top.swiId!= g_running_swiID)
  74. {
  75. int old_running_swiID=g_running_swiID;
  76. g_running_swiID=swi_top.swiId;
  77. swi_top.proc();
  78. g_running_swiID=old_running_swiID;
  79. g_schedule_quene.pop();
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. return 0;
  87. }
  88. /*************************************************************************************************
  89. 函数说明:清空所有的信息
  90. 输入参数:无
  91. 输出参数:无
  92. 返回值 :无
  93. **************************************************************************************************/
  94. void Clear(void)
  95. {
  96. //TODO: 添加代码...
  97. while(!g_schedule_quene.empty())
  98. g_schedule_quene.pop();
  99. g_swi_creat_map.clear();
  100. g_running_swiID=-1;
  101. }

四、参考:http://blog.csdn.net/dszgf5717/article/details/23034099

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注