实时操作系统实验报告专业:11通信工程学号:20110306136姓名: 王帅指导老师:申屠浩实验二 任务管理实验实验目的:1、理解任务管理的基本原理,了解任务的各个基本状态及其变迁过程;2、掌握µC/OS -II 中任务管理的基本方法(挂起、解挂);3、熟练使用µC/OS -II 任务管理的基本系统调用。
实验要求与思路:为了体现任务的各个基本状态及其变迁过程,本实验设计了T0、T1和T3三个任务,它们交替运行,如图2-2所示。
T0 T1 T2 T3 T4 T5 T6 T7 T8图2-2注意:图中的栅格并不代表严格的时间刻度,而仅仅表现各任务启动和执行的相对先后关系。
说明:在系统完成初始化后,可以先创建并启动优先级最低的TaskStart ,由它创建其他3个应用任务T0、T1和T2,之后整个系T0T2T1T0 T1 T2 T1T0统的运行流程如下:1)优先级最高的T0开始执行,之后T0挂起自己;2)然后系统调度选中T1开始执行,之后T1挂起自己;3)接着系统调度选中T2,之后唤醒T0;4)如此循环实现提示:在启动任务中创建三个任务后,应挂起任务1和任务2。
在每个任务恢复其它任务并挂起自己之前,显示当前三个任务的状态,并延时1秒。
函数说明:void PC_GetDateTime (char *s);获取"YYYY-MM-DD HH:MM:SS"格式的时间字串存放在字符串s中,s的长度最少为21字节。
void PC_DispStr (INT8U x, INT8U y, INT8U *s, INT8U color);在y行x列以color颜色值显示字串s,注意color由背景色和前景色两种颜色构成。
INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli);按时、分、秒、毫秒设置进行延时。
void OSTimeDly (INT16U ticks)按ticks值进行延时,1 ticks一般为10ms。
INT32U OSTimeGet (void)获取从程序开始运行到当前时间,所经过的ticks值。
实验程序:#include "includes.h"#define TASK_STK_SIZE 512OS_STK TaskStk1[TASK_STK_SIZE];OS_STK TaskStk2[TASK_STK_SIZE];OS_STK TaskStk3[TASK_STK_SIZE];OS_STK TaskStartStk[TASK_STK_SIZE];void Task1(void *data);void Task2(void *data);void Task3(void *data); /* Function prototypes of tasks*/void TaskStart(void *data); /* Function prototypes of Startup task */static void TaskStartCreateTasks(void);static void TaskStartDispInit(void);void main (void){PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the screen */OSInit(); /*Initialize uC/OS-II */PC_DOSSaveReturn(); /* Save environment to return to DOS */PC_VectSet(uCOS, OSCtxSw); /* Install uC/OS-II's context switch vector */OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 4);OSStart(); /* Start multitasking */}void TaskStart (void *pdata){#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */OS_CPU_SR cpu_sr;#endifchar s[100];INT16S key;pdata = pdata;TaskStartDispInit(); /* Initialize the display */OS_ENTER_CRITICAL();PC_VectSet(0x08, OSTickISR); /* Install uC/OS-II'sclock tick ISR */PC_SetTickRate(OS_TICKS_PER_SEC); /* Reprogram tick rate */OS_EXIT_CRITICAL();OSStatInit(); /* Initialize uC/OS-II'sstatistics */OSTaskCreate(Task1, (void *)0, &TaskStk1[TASK_STK_SIZE- 1], 1);OSTaskCreate(Task2, (void *)0, &TaskStk2[TASK_STK_SIZE -1], 2);OSTaskCreate(Task3, (void *)0, &TaskStk3[TASK_STK_SIZE -1], 3);OSTaskSuspend(2);/* Prevent compiler warning */OSTaskSuspend(3);for (;;) {if (PC_GetKey(&key) == TRUE) { /* See if keyhas been pressed */if (key == 0x1B) { /* Yes, see if it'sthe ESCAPE key */PC_DOSReturn(); /*Return to DOS */}}OSCtxSwCtr = 0; /* Clear contextswitch counter */OSTimeDly(1);}}static void TaskStartDispInit (void){PC_DispStr( 0, 0, " uC/OS-II,The Real-Time Kernel ",DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);PC_DispStr( 0, 1, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 2, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 3, " Time EXAMPLE ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 4, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 5, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 6, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 7, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 8, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 9, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 10, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 11, "",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 12, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 13, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 14, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 15, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 16, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 17, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 18, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 19, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 20, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 21, " ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr( 0, 22, " <-PRESS'ESC' TO QUIT-> ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK);}void Task1 (void *pdata){char ss[30];INT8U err;pdata=pdata;for (;;) {PC_DispStr(35, 10, "Task1 running", DISP_FGND_BLACK +DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 11, "Task2 Suspend ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 12, "Task3 Suspend ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */OSTaskResume(2); /* Delay 1 clock tick */OSTaskSuspend(OS_PRIO_SELF);}}void Task2 (void *pdata){char ss[30];INT8U err;pdata=pdata;for (;;) {PC_DispStr(35, 10, "Task1 Suspend ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 11, "Task2 running ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 12, "Task3 Suspend ", DISP_FGND_BLACK +DISP_BGND_LIGHT_GRAY);OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */ /* Delay 1 clock tick */OSTaskResume(3); /* Delay 1 clock tick */OSTaskSuspend(OS_PRIO_SELF);}}void Task3 (void *pdata){char ss[30];INT8U err;pdata=pdata;for (;;) {PC_DispStr(35, 10, "Task1 Suspend ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 11, "Task2 Suspend ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);PC_DispStr(35, 12, "Task3 running ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */ /* Delay 1 clock tick */OSTaskResume(1); /* Delay 1 clock tick */OSTaskSuspend(OS_PRIO_SELF);}}实验截图:实验感想通过这次试验我掌握了掌握μC/OS-II 中任务管理的基本方法(挂起、解挂),而且也理解μC/OS-II 中任务的调用方法,根据优先级来进行任务的调度,挂起任务以及恢复任务,也比较深刻的理解到抢占式处理的内涵。