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

📄 cn.cpp.bak

📁 运行环境TC
💻 BAK
字号:
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define START -1
#define FINISHED 0
#define RUNNING 1
#define READY 2
#define BLOCKED 3
#define RUNCOUNT 10000
#define TCBNUM 6
#define LEN 100
#define GET_INDOS 0x34
#define GET_CRIT_ERR 0x5d06
#define INT_MIN -32767

int TLE=6;
int current=-1;
int timecount=0;
int tcb_num=6;

char far *indos_ptr=0;
char far *crit_err_prt=0;

typedef int (far* codeptr)();

int DosBusy();
void InitDos();
void InitTCB();
void InitView();
void Begin();
void Wait();
void FUN();
void over();
void tcb_state();
void interrupt(*old_int8)();  
void interrupt new_int8();    
void interrupt swtch();
void create(char*,codeptr,int,int); 
int ALL_FINISHED();
int FIND();

void thread_1();
void thread_2();
void thread_3();
void thread_4();
void thread_5();

struct TCB
{
	unsigned char *stack;  /*堆栈起始地址*/
	unsigned ss;      
	unsigned sp;
	char state;
	char name[10];
	int value;
}tcb[TCBNUM];

struct int_regs
{
	unsigned DP,DI,SI,DS,ES,DX,CX,BX,AX,IP,CS,FLAGS,off,seg;
};

void InitInDos()
{
	union REGS regs;
	struct STREGS segregs;

	regs.h.ah=GET_INDOS;
	intdosx(&regs,&regs,&segregs);
	indos_ptr=MK_FP(segregs.eg.regs.x.bx);

	if(_osmajor<3) crit_err_ptr=indos_ptr+1;
	else if (_osmajor==3&&_osminor==0) crit_err_ptr==indos_ptr-1;
	else
	{
		regs.x.ax=GET_CRIT_ERR;
		intdosx(&regs,&regs,&segregs);
		crit_err_ptr=MK_FP(segregs.ds.regs.x.si);
	}
}

int DosBusy()
{
	if(indos_ptr&&crit_err_ptr) return (*indos_ptr || *crit_err_ptr);
	else return -1;
}

void InitTCB()
{
	int i;
	for(i=0;i<TCBNUM;i++)
	{tcb[i].state=START;}
}

void over()
{
	if(tcb[current].state == RUNNING)
	{
		disable();
		tcb[current].state=FINISHED;
		free(tcb[current].stack);
		enable();
	}
	swtch;
}

void tcb_state(int flag)
{
	int i;
	printf("\n");
	if(!flag)printf("The Main Thread is READY\n");
	for (i=1;i<TCBNUM;i++)
	{
		if(tcb[i].state==START)continue;
		switch(tcb[i].state)
		{
		case FINISHED:printf("Thread(%d) is FINISHED\n",i);break;
			case RUNNING:printf("Thread(%d) is RUNNING\n",i);break;
				case BLOCKED:printf("Thread(%d) is BLOCKED\n",i);break;
					case READY:printf("Thread(%d) is READY\n",i);break;

		}
	}
	if(flag)printf("The Main Thread si FINISHED\n");
}

void InitView()
{
	printf("This is Operating System Course Design\n");
	printf("Based on the threads of the multi-tasking System\n");
}

void create(char* name,codeptr code,int stacklen,int value)
{
	int i;
	struct int_regs* tmp;
	for(i=1;i<TCBNUM;i++)
	{
		if(tcb[i].state==START || tcb[i].state==FINISHED) break;
	}
	if(i==TCBNUM){printf("create was failed/n");return;}
	
	strcpy(tcb[i].name,name);
	tcb[i].stack=(unsigned char*)malloc(stacklen);
	tcb[i].value=value;
	tcb[i].stack += stacklen;
	tmp = (struct int_regs*)tcb[i].stack -1;
	tmp->DS=_DS;
	tmp->ES=_ES;
	tmp->FLAGS=0x200;
	tmp->CS=FP_SEGS(code);
	tmp->IP=FP_OFF(code);
	tmp->off=FP_OFF(over);
	tmp->seg=FP_SEG(over);

	tcb[i].ss=FP_SEG(tmp);
	tcb[i].sp=FP_OFF(tmp);
	tcb[i].state=READY;
	return;
}

int FIND() /*寻找优先权最高的READY线程*/
{
		int i;
		int num=0;
		int v=INT_MIN;
		for (i=0;i<TCBNUM;i++)
		{
			if(i==current)continue;
			if(tcb[i].state==READY && tcb[i].value>v)
				{
					num = i;
					v=tcb[i].value;	
				}	
		}
		return num;
}

int ALL_FINISHED()
{
	int i;
	for(i=1;i<TCBNUM;i++)
	{
		if(tcb[i].state!=FINISHED && tcb[i].state!=START) return 0;	
	}	
	return 1;
}

void interrupt new_int8()
{
	(*old_int8)();
	timecount++;
	if(timecount>=TLE)
		{
			if(!DosBusy())
				{
					disable();
					
					tcb[current].ss=_SS;	
					tcb[current].sp=_SP;
					tcb[current].value--;
					if(tcb[current].state==RUNNING) tcb[current].state=READY;
						
					current=FUND();
					_SS=tcb[current].ss;
					_SP=tcb[current].sp;
					tcb[current].state=RUNNING;
					timecount=0;
					
					enable();
				}	
		}	
}

void interrupt swtch()
{
	disable();
	
	tcb[current].ss=_SS;	
	tcb[current].sp=_SP;
	tcb[current].value--;
	if(tcb[current].state==RUNNING) tcb[current].state=READY;
				
	current=FUND();
	_SS=tcb[current].ss;
	_SP=tcb[current].sp;
	tcb[current].state=RUNNING;
	timecount=0;
					
	enable();
}

void Begin()
{
	int v1,v2,v3,v4,v5;
	
	whilie(1)
	{
		printf("/nPlease input the number of Threads(0<=TCB<=5)");
		scanf("%d",&tcb_num);
		if(tcb_num>=0&&tcb<=5)break;	
	}	

	whilie(1)
	{
		printf("/nPlease input the value of Time Slice(1<=TLE<=10)");
		scanf("%d",&TLE);
		if(TLE>=1&&TLE<=10)break;	
	}	
	
	whlie(1)
	{
		if(tcb_num<1)break;
		printf("/nPlease input the value of NO.1 thread:");	
		scanf("%d".&v1);
		printf("NO.1 thread has been created!!!\n");
		create ("thread_1",(codeptr)thread_1,1024,v1);
		if(tcb_num==1) break;   /*线程1创建*/
	  		
	  printf("/nPlease input the value of NO.2 thread:");	
		scanf("%d".&v2);
		printf("NO.2 thread has been created!!!\n");
		create ("thread_2",(codeptr)thread_2,1024,v2);
		if(tcb_num==2) break;   /*线程2创建*/
	
		printf("/nPlease input the value of NO.3 thread:");	
		scanf("%d".&v3);
		printf("NO.3 thread has been created!!!\n");
		create ("thread_3",(codeptr)thread_3,1024,v3);
		if(tcb_num==3) break;   /*线程3创建*/
			
		printf("/nPlease input the value of NO.3 thread:");	
		scanf("%d".&v3);
		printf("NO.3 thread has been created!!!\n");
		create ("thread_3",(codeptr)thread_3,1024,v3);
		if(tcb_num==3) break;   /*线程3创建*/
			
		printf("/nPlease input the value of NO.4 thread:");	
		scanf("%d".&v4);
		printf("NO.4 thread has been created!!!\n");
		create ("thread_4",(codeptr)thread_4,1024,v4);
		if(tcb_num==4) break;   /*线程4创建*/
	
		printf("/nPlease input the value of NO.5 thread:");	
		scanf("%d".&v5);
		printf("NO.5 thread has been created!!!\n");
		create ("thread_5",(codeptr)thread_5,1024,v5);
		if(tcb_num>=5) break;   /*线程5创建*/
	
	}
}

void Wait()
{
	int i,j,k;
	printf("\nPlease Waiting...\n");
	for(i=0;i<30000;i++)	
	for(j=0;j<30000;j++)
	for(k=0;k<3;k++)
}

void thread_1()
{
	int i,j,k;
	for(i=0;i<20;i++)
	{
		printf("Thread(1)\n");
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

void thread_2()
{
	int i,j,k;
	for(i=0;i<20;i++)
	{
		printf("\tThread(2)\n");
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

void thread_3()
{
	int i,j,k;
	for(i=0;i<20;i++)
	{
		printf("\t\tThread(3)\n");
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

void thread_4()
{
	int i,j,k;
	for(i=0;i<20;i++)
	{
		printf("\t\t\tThread(4)\n");
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

void thread_5()
{
	int i,j,k;
	for(i=0;i<20;i++)
	{
		printf("\t\t\t\tThread(5)\n");
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

⌨️ 快捷键说明

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