01task的分析
#include "TaskSample.h"
void TaskSampleTaskA(W stacd,VP exinf);
void TaskSampleTaskB(W stacd,VP exinf);
void TaskSampleTaskC(W stacd,VP exinf);
static ID TaskID_A; /定义TaskID_A,TaskID_B,TaskID_C三个ID型局部变量**/
static ID TaskID_B;
static ID TaskID_C;
ER TaskSample( void)
{
ER ercd=E_OK; //初始化ercd
T_CTSK ctsk; //定义一个T_CTSK型的结构体;
tm_putstring((UB*)"Task sample create step 1-Create Task A;\n"); //输出:Task sample create step 1-Create Task A;
ctsk.exinf = (VP)NULL; //扩展信息为空;
ctsk.tskatr = TA_HLNG|TA_RNG0; //任务属性为高级语言, 保护级别为0;
ctsk.task = TaskSampleTaskA; //任务服务函数为TaskSampleTaskA;
ctsk.itskpri = 24; //任务优先级为24;
ctsk.stksz = 512*4; //任务大小为512*4;
TaskID_A = tk_cre_tsk(&ctsk); //创建任务A,把返回值给TaskID_A;
if(TaskID_A < E_OK) { /*如果TaskID_A小于E_OK,返回TaskID_A*/
ercd=TaskID_A;
return ercd;
}
tm_putstring((UB*)"Task sample create step 2-Create Task B;\n"); //输出:Task sample create step 2-Create Task B;
ctsk.exinf = (VP)NULL; //扩展信息为空;
ctsk.tskatr = TA_HLNG|TA_RNG0; //任务属性为高级语言, 保护级别为0;
ctsk.task = TaskSampleTaskB; //任务服务函数为TaskSampleTaskB;
ctsk.itskpri = 26; //任务优先级为26;
ctsk.stksz = 512*4; //任务大小为512*4;
TaskID_B = tk_cre_tsk(&ctsk); //创建任务B,把返回值给TaskID_B;
if(TaskID_B < E_OK) {
ercd=TaskID_B; /*如果TaskID_B小于E_OK,返回TaskID_B*/
return ercd;
}
tm_putstring((UB*)"Task sample create step 3-Create Task C;\n"); //输出:Task sample create step 3-Create Task C;
ctsk.exinf = (VP)NULL; //扩展信息为空;
ctsk.tskatr = TA_HLNG|TA_RNG0; //任务属性为高级语言, 保护级别为0;
ctsk.task = TaskSampleTaskC; //任务服务函数为TaskSampleTaskC;
ctsk.itskpri = 28; //任务优先级为28;
ctsk.stksz = 512*4; //任务大小为512*4;
TaskID_C = tk_cre_tsk(&ctsk); //创建任务C,把返回值给TaskID_C;
if(TaskID_C < E_OK){
ercd=TaskID_C; /*如果TaskID_C小于E_OK,返回TaskID_C*/
return ercd;
}
tm_putstring((UB*)"Task sample create step 4-Start Task B;\n"); //输出:Task sample create step 4-Start Task B;
tk_sta_tsk(TaskID_B,5); //启动任务B;
return E_OK;
}
void TaskSampleTaskA(W stacd,VP exinf) //任务A的服务函数;
{
for(;;) {
tm_putstring((UB*)"Task A will be sleeping;\n"); //输出:Task A will be sleeping;
tk_slp_tsk(1000); //任务A进入休眠;
}
tm_putstring((UB*)"task A will Exit ;\n"); //输出: task A will Exit ;
tk_ext_tsk(); //任务A退出, 任务A处于静止状态;
}
void TaskSampleTaskB(W stacd,VP exinf) //任务B的服务函数;
{
B c;
B s[3]="0\n";
if(stacd>0){ /*如果sracd大于0,则sracd累加于s[0]中,
s[0]+=stacd;
tm_putstring((UB*)"Task B parameter stacd is ");
tm_putstring((UB*)s); 输出:Task B parameter stacd is , 输出*/
tm_putstring((UB*)"Task B will start task C;\n"); //输出:Task B will start task C;
tk_sta_tsk(TaskID_C,0); //任务C启动;
tm_putstring((UB*)"Task B will start task A;\n"); //输出:Task B will start task A;
tk_sta_tsk(TaskID_A,0); //启动任务A;
}
for(;;) {
tm_putstring((UB*)"Task B is running,input Command(e=exit):\n"); /*输出:Task B is running,input Command(e=exit):
c = tm_getchar(0);
if(c=='e') {
break;
}
tm_putstring((UB*)"Task B will be sleeping;\n");
tk_slp_tsk(500); 如果输入e,则跳出循环, 然后任务B进入睡眠状态*/
}
tm_putstring((UB*)"Task B will terminate&delete task A;\n"); //输出:Task B will terminate&delete task A;
tk_ter_tsk(TaskID_A); //任务A进入暂停, 并删除任务A;
tk_del_tsk(TaskID_A);
tm_putstring((UB*)"Task B will terminate&delete task C;\n"); //输出:Task B will terminate&delete task C;
tk_ter_tsk(TaskID_C); //任务C进入暂停,并删除任务C;
tk_del_tsk(TaskID_C);
tm_putstring((UB*)"Task B will terminate&delete self;\n"); //输出:Task B will terminate&delete self;
tk_exd_tsk(); //任务B退出;
}
void TaskSampleTaskC(W stacd,VP exinf) //任务C的服务函数;
{
UB i;
for(;;) {
tm_putstring((UB*)"****************************************\n"); //输出:****************************************
for(i=0;i<200;i++); //延时运作;
}
tm_putstring((UB*)"task C will Exit ;\n"); //输出:task C will Exit ;
tk_ext_tsk(); 任务C退出;
}
输出结果:
Task sample create step 1-Create Task A;
Task sample create step 2-Create Task B;
Task sample create step 3-Create Task C;
Task B parameter stacd is 5
Task B will start task C;
Task B will start task A;
Task A will be sleeping;
Task B is running,input Command(e=exit):
分析:先是创建任务A, B, C;创建的任务处于静止态;然后任务B先启动,执行服务于B的函数;
进入函数TaskSampleTaskB. 函数启动了任务A,由于任务A的优先级别任务B高,进而任务A处于运行态;
转而执行服务于任务A的函数;然后任务A进入睡眠状态;转过来执行TaskSampleTaskB, 输入e, 跳出
for循环;然后接着输出:Task B will terminate&delete task A; 任务A被删除;
Task B will terminate&delete task C; 任务C被删除;
Task B will terminate&delete self; 任务B退出;
如果没有输入e:
则输出:Task B will be sleeping; //任务B进入睡眠状态;然后任务C运行;
****************************************
然后任务C进入睡眠