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

📄 cn.cpp

📁 运行环境TC
💻 CPP
字号:
#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 BUFFNUM 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;
struct buffer *freebuff;
char far *indos_ptr=0;
char far *crit_err_ptr=0;

typedef int (far* codeptr)();

int DosBusy();
void InitDos();
void InitTCB();
void InitView();
void Begin();
void Wait();
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 initfreebuff();  /*生成空闲消息缓冲区队列*/
void block(struct TCB *q);  /*阻塞线程*/
void wakeup(struct TCB *q);  /*唤醒线程*/
void p(semaphore *sem);
void v(semaphore *sem);
struct buffer *getbuff(void);
void insert(struct buffer *mq.struct buffer *buff);
void send(int i,char *a ,int size);
void receive(struct buffer *mq);
void thread_1();
void thread_2();
void thread_3();
void thread_4();
void thread_5();



/*记录型信号量*/
struct semaphore 
{
	int value;
	stuct TCB *wq; /*指向等待队列*/
};

semaphore sfb,mutexfb;


struct  buffer
{
	int sender;   /*消息发送者*/
	int size;    /*消息长度*/
	char text[LEN];  /*正文*/
	struct buffer *next;   /*指向下一个消息缓冲区*/
};


struct TCB
{
	unsigned char *stack;  /*堆栈起始地址*/
	unsigned ss;
	unsigned sp;
	char state;
	char name[10];
	int value;
	struct TCB *next; /*指向等待队列中的下一个线程*/
	struct buffer *mq;   /*指向该线程的接收消息队列的队首指针*/
	semaphore mutex;    /*消息队列的互斥信号量*/
}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 SREGS segregs;

	regs.h.ah=GET_INDOS;
	intdosx(&regs,&regs,&segregs);
	indos_ptr=MK_FP(segregs.es,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;
		tcb[i].mq=NULL;
		tcb[i].mutex.value=1;
		tcb[i].mutex.wq=NULL;
	}

}

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_SEG(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;
}

void initfreebuff()
{
	struct buffer *s,*p;
	int i;
	s= (struct buffer)malloc (sizeof(buffer));
	s->sender = -1;
	s->text=NULL;
	s->next=NULL;
	s->size=0;
	freebuff = s;
	*p=NULL;
	freebuff->next=p;
	for (i=1;i<BUFFNUM;i++)
	{
		s= (struct buffer)malloc (sizeof(buffer));
		s->sender = -1;
		s->text=NULL;
		s->next=NULL;
		s->size=0;
		p->next = s;
		p=p->next;
	}
	sfb.value=BUFFNUM;
	sfb.wq=NULL;
	mutexfb.value=1;
	mutexfb.wq=NULL;
}

struct buffer *getbuff(void)
{
	struct buffer *buff;
	buff=freebuff;
	freebuff = freebuff->next;
	return (buff);
}
void insert(struct buffer *mq,struct buffer *buff)
{
	struct buffer *temp;
	if (buff == NULL) return;
	buff->next=NULL;
	if(*mq==NULL) *mq=buff;
	else {
		temp=mq;
		while (temp->next!=NULL)
			temp=temp->next;
		temp->next=buff;
	}
}
void send(int a,char *a ,int size)
{
	int i,j;
	disable();

	p(&sfb);
	p(&mutexfb);
	 buff=getbuff;
	v(&mutexfb);

	buff->sender=current;
	buff->size;
	buff->next=NULL;
	for(i=0;i<buff->size;i++,a++)
		buff->text[i]=*a;

	p(&tcb[a].mutex);
	 insert(&(tcb[a].mq),buff);
    v(&tcb[a].mutex);
	
	enable();
    for (i=0;i<10000;i++)
		for (j=0;j<10000;j++);
}

void receive (struct buffer *mq)
{
	struct buffer *p;
	*p=*mq;
	while (p!=NULL)
	{
		printf("%d\n",p->sender);
		printf("%s\n",p->text);
		p=p->next;
	}
}

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=FIND();
					_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=FIND();
	_SS=tcb[current].ss;
	_SP=tcb[current].sp;
	tcb[current].state=RUNNING;
	timecount=0;

	enable();
}

void block(struct TCB *q)  /*阻塞线程*/
{
	
    tcb[current].state = BLOCKED;
	struct TCB *p=q;
	while (p->next!=NULL) p=p->next;
	p->next=tcb[current];
	swtch();  /*重新进行CPU调度*/
}

void wakeup(struct TCB *q)
{
	q->state=READY;
	struct TCB *p=q;
	q=q->next;
	p->next=NULL;
}  /*唤醒线程*/

void p(semaphore *sem)
{
	struct TCB *q;
	disable();
	sem->value--;
	if (sem->value<0)
	{
		*q=*sem->wq;
		block(q);
	}
	enable();
}

void v(semaphore *sem)
{
	struct TCB *q;
	disable();
	*q=*sem->wq;
	sem->value++;
	if (sem->value <=0)
		wakeup(q);
	enable();
}

void Begin()
{
	int v1,v2,v3,v4,v5;

	while(1)
	{
		printf("/nPlease input the number of Threads(0<=TCB<=5)");
		scanf("%d",&tcb_num);
		if(tcb_num>=0&&tcb_num<=5)break;
	}

	while(1)
	{
		printf("/nPlease input the value of Time Slice(1<=TLE<=10)");
		scanf("%d",&TLE);
		if(TLE>=1&&TLE<=10)break;
	}

	while(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<50;i++)
	{
		printf("%d\n",i*i);
		for(j=0;j<RUNCOUNT;j++)
		for(k=0;k<RUNCOUNT;k++);

	}
}

void thread_2()
{
	int i,j,k;
	for(i=0;i<50;i++)
	{
		printf("\t%d\n",i*i*i);
		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\t%d\n",i);
		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)%d\n",tcb[4].value);
		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)%d\n",tcb[5].value);
		for(j=0;j<RUNCOUNT;j++)	
		for(k=0;k<RUNCOUNT;k++);
	}	
}

void main()
{
	int i;
	InitInDos();
	InitTCB();
	InitView();
	old_int8=getvect(8);
	
	strcpy(tcb[0].name,"MAIN");
	tcb[0].state=RUNNING;
	tcb[0].value=0;
	current=0;
	
	Begin();
	tcb_state(0);
	Wait();
	
	printf("\n\nThreads start running!!!\n");

	setvect(8,new_int8);
	swtch();

	while(!ALL_FINISHED());

	printf("\nThreads ended.\n");

	tcb[0].name[0]=0;
	tcb[0].state=FINISHED;
	setvect(8,old_int8);

	tcb_state(1);

  printf("\nMnlti_task system terminated.\n");
  printf("\nPress any key to exit the command line.\n");
  getch();

}
.

⌨️ 快捷键说明

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