【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
typedef struct _TASK_INFO{这里的priority就是当前线程的优先级,所以最简单的方法就是根据priority直接分配对应的time_slice。也就是这个函数,
UINT32 id;
UINT32* stack;
UINT32 size;
UINT32 context;
UINT32 priority;
UINT32 time_slice;
void (*func)();
}TASK_INFO;
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 10typedef 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\n\t" "push %%ebx\n\t" "push %%ecx\n\t" "push %%edx\n\t" "push %%esi\n\t" "push %%edi\n\t" "push %%ebp\n\t" "push %%esp\n\t" "lea 0x8(%%ebp), %%eax\n\t" "mov (%%eax), %%eax\n\t" "mov %%esp, (%%eax)\n\t" "lea 0xc(%%ebp), %%eax\n\t" "mov (%%eax), %%eax\n\t" "mov (%%eax), %%esp\n\t" "pop %%esp\n\t" "pop %%ebp\n\t" "pop %%edi\n\t" "pop %%esi\n\t" "pop %%edx\n\t" "pop %%ecx\n\t" "pop %%ebx\n\t" "pop %%eax\n\t" ::);}void hello(){ int temp = 0; while(1) { printf("id = %d, temp = %d, count = %d in thread!\n",current_thread_id, temp ++, count ++); swap(&gAllTask[current_thread_id].context, &old); printf("id = %d, temp = %d, count = %d in thread!\n",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;}