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

📄 test.bak

📁 TC-ucos-philos-详细注释.rar 用TC编译ucos并在图像化界面下演示哲学家就餐问题
💻 BAK
字号:
#include <GRAPHICS.H>
#include <MATH.H>
#include "includes.h"


#define pi 3.141592653
#define CenterX 400
#define CenterY 250
#define r 100

#define  TASK_STK_SIZE   512			//任务堆栈长度

OS_STK   StartTaskStk[TASK_STK_SIZE];		//定义任务堆栈区
OS_STK	 PhilosStk[5][TASK_STK_SIZE];
INT8U	 PhilosID[]={1,2,3,4,5};

INT16S   key;					//用于退出uCOS_II的键	

INT8U   err;
INT8U	x=0,y=0,z=0;				//字符显示位置
INT8U	i=0;

int driver=0,mode=0;
int philosx[5],philosy[5];
int x1[5],y1[5];
int x2[5],y2[5];
//driver=0;
//mode=0;

OS_EVENT *ChopStick[5];

void  StartTask(void *data);
void  Philos(void *data);
void  PickChop(INT8U id);
void  PutChop(INT8U id);
void  Thinking(INT8U id);
void  Hungry(INT8U id);
void  Eating(INT8U id);
void  Quitkey(void);

/************************主函数*********************************************/
void  main (void)
{
    OSInit();					//初始化uCOS_II
    PC_DOSSaveReturn();				//保存Dos环境
    PC_VectSet(uCOS, OSCtxSw);			//安装uCOS_II中断
	for(i=0;i<5;i++)
	{
		philosx[i]=(int)(CenterX+r*sin(pi*(72*i)/180));
		philosy[i]=(int)(CenterY-r*cos(pi*(72*i)/180));
		x1[i]=(int)(CenterX+(r+20)*sin(pi*(72*i-36)/180));
	    	y1[i]=(int)(CenterY-(r+20)*cos(pi*(72*i-36)/180));
		x2[i]=(int)(CenterX+(r-20)*sin(pi*(72*i-36)/180));
	    	y2[i]=(int)(CenterY-(r-20)*cos(pi*(72*i-36)/180));
	}

	for(i=0;i<5;i++)
	{
		ChopStick[i]=OSSemCreate(1);
//		if (ChopStick[i]!=(OS_EVENT *)0)
//		{
//			sprintf(temp,"ChopStick %d create error",i);
//			PC_DispStr(40,20+i,temp,DISP_BGND_BLACK+DISP_FGND_YELLOW);
//		}
	}
    OSTaskCreate(StartTask,0,&StartTaskStk[TASK_STK_SIZE - 1],5);		
    OSStart();			
}

//*****************************StartTask********************************************
void  StartTask(void *pdata)
{
	char temp[50];
	pdata=pdata;
    OS_ENTER_CRITICAL();
    PC_VectSet(0x08, OSTickISR);
    PC_SetTickRate(OS_TICKS_PER_SEC);
    OS_EXIT_CRITICAL();
//  OSStatInit();

	for(i=0;i<5;i++)
	{
		err=OSTaskCreate(Philos,(void *)&PhilosID[i],&PhilosStk[i][TASK_STK_SIZE - 1],i+6);
		if(err==OS_NO_ERR)
			//test if task is created successfully
		{
			sprintf(temp,"Philosopher %d create error",PhilosID[i]);
			PC_DispStr(0,20+i,temp,DISP_BGND_BLACK+DISP_FGND_YELLOW);
			sprintf(temp,"%d",err);
        		PC_DispStr(30,20+i,temp,DISP_BGND_BLACK+DISP_FGND_WHITE);
		}

	}
	initgraph(&driver,&mode,"d:\\TCPP30E\\BGI");
	setcolor(YELLOW);
	outtextxy(150,30,"Demo of Dining Philosphy in Ucos-II");
	outtextxy(190,40,"by Zengzhi Wang 2008.6.10");

	setcolor(WHITE);
	outtextxy(20,80,"Thinking...");
	circle(120,85,10);

	outtextxy(20,110,"Hungry...");
	circle(120,115,10);
	setfillstyle(LTSLASH_FILL,WHITE);
	floodfill(120,115,WHITE);

	outtextxy(20,140,"Eating...");
	circle(120,145,10);
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(120,145,WHITE);

	for(i=0;i<5;i++)
	{
		circle(philosx[i],philosy[i],20);
		line(x1[i],y1[i],x2[i],y2[i]);
	}

	OSTaskSuspend(OS_PRIO_SELF);	
}
//*****************************Philos********************************************
//the Philosopher first has to pick up the chopstick on his left hand then right
void  Philos(void *pdata)
{
	INT8U PhID;
	char temp[50];
   	PhID = *(INT8U *)pdata; 

    for (;;) 
	{
//		sprintf(temp,"Philosopher %d is thinking",PhID);
//		PC_DispStr(0,y++,temp,DISP_BGND_BLACK+DISP_FGND_WHITE);
		Thinking(PhID-1);
		OSTimeDly(400;
//		sprintf(temp,"Philosopher %d is hungry",PhID);
//		PC_DispStr(0,y++,temp,DISP_BGND_BLACK+DISP_FGND_WHITE);
		Hungry(PhID-1);
		OSTimeDly(400);

		OSSemPend(ChopStick[(PhID-1)],0,&err);
		PickChop(PhID-1);

		OSSemPend(ChopStick[(PhID%5)],0,&err);
		PickChop(PhID%5);
		Eating(PhID-1);

//		sprintf(temp,"Philosopher %d is eating",PhID);
//		PC_DispStr(0,y++,temp,DISP_BGND_BLACK+DISP_FGND_WHITE);
		OSTimeDly(400);


		OSSemPost(ChopStick[(PhID%5)]);
		PutChop(PhID%5);

		OSSemPost(ChopStick[(PhID-1)]);
		PutChop(PhID-1);

		OSTimeDly(400);
       		Quitkey();

        }
}
//***************************PickChop()********************************************
void PickChop(INT8U id)
{
	setcolor(BLACK);
	line(x1[id],y1[id],x2[id],y2[id]);
}
//***************************PutChop()********************************************
void PutChop(INT8U id)
{
	setcolor(WHITE);
	line(x1[id],y1[id],x2[id],y2[id]);
}

//***************************Thinking()********************************************
void Thinking(INT8U id)
{
	setfillstyle(SOLID_FILL,BLACK);
	floodfill(philosx[id],philosy[id],WHITE);
}
//***************************Hungry()********************************************
void Hungry(INT8U id)
{
	setfillstyle(WIDE_DOT_FILL,WHITE);
	floodfill(philosx[id],philosy[id],WHITE);
}
//***************************Eating()********************************************
void Eating(INT8U id)
{
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(philosx[id],philosy[id],WHITE);
}

//*****************************Quitkey()********************************************
void Quitkey(void)
{
	if(PC_GetKey(&key)==TRUE)
	{
		if(key==0x1B)
			{restorecrtmode();PC_DOSReturn();}
	}
}
//5	0	4
//1	1	0
//2	2	1
//3	3	2
//4	4	3
//5	0	4

⌨️ 快捷键说明

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