⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 task.c

📁 一个简单的多任务调度器源码.可在Dos/windows下运行.
💻 C
字号:
/***********************task.c*************************************************
 This file created by peng.wang 06/20/2006

 Multitasking task scheduling function
 
 *************************************************************************/
 
#include <task.h>
#include<type.h>
#include<dos.h>

 static void interrupt T_task_switch_interrupt(void);
 static void  T_init_task(void);

static void interrupt (* pInterruptFun)(void); 
static Int16 task_counts;      /*the counts of the task*/         
static Tcb* current_task=NULL;/*the main task*/
static Tcb* next_task=NULL;       /*the task will running next time*/

#define M_DEFINE_STACK
#include<taskreg.h>          /*set up the task stack,thery are arraries*/

#undef M_DEFINE_STACK
#undef M_TASK(NAME, PRIORITY, STACK_SIZE, TASK_ENTRY)
#undef M_BEGIN_REGISTER_TASK
#undef M_END_REGISTER_TASK

#define M_DEFINE_TASK
#include<taskreg.h>        /*set up task TCB*/


 
/*the cock interrupt function,it will be called every 54ms interval  to schedule task.(it is better for 20ms).
 warring: this function can't be portable, if you want ransplant "myos",you must rewrite this function.
 you can't define any auto var at this function .elsewise ,the stack will be shuffled*/

static void interrupt T_task_switch_interrupt(void)
{  
  
     
     next_task=T_get_ready_task();
    current_task->Stack_top=MK_FP(_SS,_SP) ;
   _SS=FP_SEG(next_task->Stack_top);
   _SP=FP_OFF(next_task->Stack_top);
    current_task=next_task;
   asm int 80h

}

 static void  T_init_task(void)
{   
    Int16 i;
    /*Initinitialize the stack of every task.Simulate a context of stack like  just occur a interrupt*/
    for(i=1;G_tasks[i].Task_entry!=NULL;i++) 
    {
        Int16  *sp;
     sp=G_tasks[i].Stack_top;

     *(--sp)=0x0202;  /*psw*/
        *(--sp)=FP_SEG(G_tasks[i].Task_entry);
        *(--sp)=FP_OFF(G_tasks[i].Task_entry);
        *(--sp)=0x0000;/*ax*/
        *(--sp)=0x0000;/*bx*/
        *(--sp)=0x0000;/*cx*/
        *(--sp)=0x0000;/*dx*/
        *(--sp)=_ES;/*es*/
        *(--sp)=_DS;/*ds*/
        *(--sp)=0x0000;/*si*/
        *(--sp)=0x0000;/*di*/
        *(--sp)=0x0000;/*bp*/
        G_tasks[i].Stack_top=sp;
    }

  task_counts=sizeof(G_tasks)/sizeof(Tcb)-1; 
  current_task=&G_tasks[0];
 }

 
 /*only the time slice turn,you can change the task schedule arithmetic here*/
Tcb*  T_get_ready_task(void)
{
      static Int16  i=0;
      i++;
      i=i%task_counts;
      return(&G_tasks[i]);  
}

 
/*launch "myos"*/ 
void T_main(void)
{
 
  T_init_task();
  
   /*set the timer interrupt for thread switch*/
   pInterruptFun= getvect(0x08);
   setvect(0x08,T_task_switch_interrupt);
   setvect(0x80,pInterruptFun);
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -