嵌入式操作系统内核原理和开发(通用优先级调度)
作者:网络转载 发布时间:[ 2012/6/12 13:24:23 ] 推荐标签:
整个代码的流程也不复杂,大家可以运行、单步调试一把,试试看。
#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;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11