时间片轮转操作系统课程设计myprocess.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流时间片轮转操作系统课程设计myprocess.精品文档.#include<stdio.h>#include<stdlib.h>#include<dos.h>#include<string.h>/*状态码*/#define NTCB 5/*TCB 的数量*/#define NTEXT 60/*线程通信缓冲区文本内容的大小*/#define NBUF 10/*缓冲区的个数*/*线程的五种状态*/#define START -1#define FINISHED 0#define RUNNING 1#define READY 2#define BLOCKED 3#define timeout 1/*线程时间片轮转的时间*/typedef int(far *codeptr)(void);/*函数指针定义*/*DOS的初始化状态码*/#define GET_INDOS 0x34#define GET_CRIT_ERR 0x5d06struct bufferint sender;int size;char textNTEXT;struct buffer *next;*freebuff;/*消息缓冲区的定义*/typedef struct int value;struct TCB *wq;semaphore ;/*记录型信号量定义*/struct TCBunsigned char *stack;unsigned ss;unsigned sp;char state;char name10;struct buffer *mq ;/*消息队列排队使用*/semaphore mutex ;/*互斥信号量,用于对TCB消息队列处理时互斥*/semaphore sm ;/*消息队列的计数信号亮*/struct TCB *next; tcbNTCB;/*线程控制块定义*/struct int_regsunsigned bp;unsigned di;unsigned si;unsigned ds;unsigned es;unsigned dx;unsigned cx;unsigned bx;unsigned ax;unsigned ip;unsigned cs;unsigned flags;unsigned off;unsigned seg;/*线程私有堆栈的数据结构的定义*/char far *indos_ptr=0;char far *crit_err_ptr=0;union REGS regs;struct SREGS segregs;int currenttcb=-1;/*初始化TCB的值*/int timecount=0;/*时间计数初始为0*/semaphore sfb = NBUF, NULL;/*空闲缓冲区的同步信号量*/semaphore mutexfb = 1, NULL;/*对空闲缓冲区处理的互斥信号量*/semaphore fmutex=0,NULL;/*F1和F2的同步信号量*/void InitBuff(void);/*初始化消息缓冲区*/int DosBusy(void); /* DosBusy Function return non_zero if DOS is busy*/void InitInDos(void);/*InitInDosFuntion to get the addresses of INDOS and CRIT_ERR flags */void InitTcb(void);/*初始化TCB*/void interrupt (*old_int8)(void);/*旧的时钟中断处理程序*/void interrupt new_int8(void);/*新的时钟中断处理程序 */int create(char *name,codeptr code,int stacklen);/*创建线程*/void tcb_state(void);/*线程状态函数*/int Seeknext(void);/*寻找下一个就绪线程*/void interrupt cpu_switch(void);/*CPU调度函数*/int all_finished();/*判断所有线程是否完成*/void Destroy(int i);/*销毁一个线程*/void over(void);/*结束一个线程*/void wait(semaphore *sem);/*P操作*/void signal(semaphore *sem);/*v操作*/void block(struct TCB *qp);/*阻塞函数*/void wakeup(struct TCB *qp);/*唤醒函数*/void f1(void);/*函数F1*/void f2(void);/*函数F2*/void insert(struct buffer *mq,struct buffer *buff);/*将buff所指的缓冲区插到*mq所指的缓冲队列末尾*/void send(char *receiver,char *a,int size);/*发送一个消息给receiver*/void receive(void);/*接收消息*/void returnTCBbuff(struct buffer *buff);/*归还已经接收信息的消息块到空闲消息缓冲区中去*/struct buffer *getbuff(void);/*获得一个空闲消息块*/struct buffer *getmessage(struct buffer *qp);/*从消息队列上获得消息*/main() InitBuff();InitInDos();InitTcb();old_int8=getvect(8);/*获取系统原来的INT 08H 的入口地址并将它保存起来*/*创建0#线程*/strcpy(tcb0.name,"main");tcb0.state=RUNNING;currenttcb=0;create("f1",(codeptr)f1,1024);create("f2",(codeptr)f2,1024);tcb_state();/*启动多个线程的并发执行*/setvect(8,new_int8);/*调用新的时间片函数 new_int8(void)*/cpu_switch(); while(!all_finished();/*终止多任务系统*/tcb0.name0='0'tcb0.state=FINISHED;setvect(8,old_int8);tcb_state();printf("n Multi_task system terminated.n");getchar();void InitBuff(void)int i;struct buffer *temp1, *temp2 ;/*在堆上分配各个消息块的空间*/temp1 = (struct buffer *)malloc(sizeof(struct buffer);freebuff = temp1 ; for(i = 0 ; i < NBUF ; i +) /*链接成一个队列*/temp2 = (struct buffer *)malloc(sizeof(struct buffer) ;temp1->next = temp2 ;temp1 = temp2 ;temp1->next = NULL ;void InitInDos(void)/* get the address of INDOS flags */regs.h.ah=GET_INDOS;intdosx(®s,®s,&segregs);indos_ptr=MK_FP(segregs.es,regs.x.bx);/* get the address of CRIT_ERR flag */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(®s,®s,&segregs); crit_err_ptr=MK_FP(segregs.ds,regs.x.si);int DosBusy(void)if(indos_ptr&&crit_err_ptr)return (*indos_ptr|*crit_err_ptr);else return (-1); /* InitInDos() hasn't been called*/void InitTcb(void)/*TCB的初始化*/ int i; for(i=0;i<NTCB;i+) tcbi.state=START; tcbi.name0='0' tcbi.next=NULL; tcbi.mq=NULL; tcbi.mutex.value=1; tcbi.mutex.wq=NULL; tcbi.sm.value=0; tcbi.sm.wq=NULL;int create(char *name,codeptr code,int stacklen)int freetcb;struct int_regs *stack;for(freetcb=1;freetcb<NTCB;freetcb+)if(tcbfreetcb.state=START|tcbfreetcb.state=FINISHED)break;if(freetcb=NTCB)printf("nCreate was failed!n");return (-1);tcbfreetcb.state=READY; strcpy(tcbfreetcb.name,name);tcbfreetcb.stack=(unsigned *)malloc(stacklen);tcbfreetcb.stack=tcbfreetcb.stack+stacklen;stack=(struct int_regs *)tcbfreetcb.stack-1;stack->ds=_DS;stack->es=_ES;stack->flags=0x200;stack->ip=FP_OFF(code);stack->cs=FP_SEG(code);stack->off=FP_OFF(over);stack->seg=FP_SEG(over);tcbfreetcb.ss=FP_SEG(stack);tcbfreetcb.sp=FP_OFF(stack);return freetcb;void tcb_state(void)int i;for(i=0;i<NTCB;i+) switch(tcbi.state) case START: printf("n tcb%d %s's state is starting;",i,tcbi.name); break; case FINISHED: printf("n tcb%d %s's state is finished;",i,tcbi.name); break; case RUNNING: printf("n tcb%d %s's state is running;",i,tcbi.name); break; case READY: printf("n tcb%d %s's state is ready;",i,tcbi.name); break; case BLOCKED: printf("n tcb%d %s's state is ready;",i,tcbi.name); break; printf("n");void wait(semaphore *sem)struct TCB *qp;disable();sem->value=sem->value-1;if(sem->value<0)qp=&(sem->wq);block(qp);enable();void signal(semaphore *sem)struct TCB *qp;disable();qp=&(sem->wq);sem->value=sem->value+1;if(sem->value<=0)wakeup(qp); enable();void block(struct TCB *qp)struct TCB *temp;temp=(*qp);disable();tcbcurrenttcb.state=BLOCKED;if(*qp)=NULL) (*qp)=&tcbcurrenttcb;elsewhile(temp->next!=NULL)temp=temp->next; temp->next=&tcbcurrenttcb;tcbcurrenttcb.next=NULL; cpu_switch(); enable();void wakeup(struct TCB *qp)struct TCB * temp;temp=(*qp);if(temp!=NULL)(*qp)=(*qp)->next;temp->state=READY;temp->next=NULL;void insert(struct buffer *mq,struct buffer *buff)struct buffer *temp;if(buff=NULL) return;buff->next=NULL;if(*mq=NULL)*mq=buff;elsetemp=*mq;while(temp->next!=NULL)temp=temp->next; temp->next=buff;void returnTCBbuff(struct buffer *buff)struct buffer *temp ;temp = freebuff ;while(temp->next != NULL)temp = temp->next ;temp->next = buff ;void send(char *receiver,char *a, int size)struct buffer *buff;int i,id=-1;disable();for(i=0;i<NTCB;i+)if(strcmp(receiver,tcbi.name)=0)id=i; break;if(id=-1)printf("Error: Receiver not exit!n");enable();return; wait(&sfb); wait(&mutexfb); buff=getbuff(); signal(&mutexfb); buff->sender=currenttcb; buff->size=size; buff->next=NULL; strcpy(buff->text,a); wait(&tcbid.mutex); insert(&(tcbid.mq),buff); signal(&tcbid.mutex); signal(&tcbid.sm); enable();void receive(void) struct buffer *buff = NULL;disable();if(tcbcurrenttcb.mq = NULL)/*当TCB中的消息队列为空时*/printf("There is no massage in %s 's message buff!n", tcbcurrenttcb.name);return ;wait(&tcbcurrenttcb.sm);/*实现TCB中发送与接收消息的同步*/signal(&tcbcurrenttcb.mutex);/*实现对TCB中取消息的互斥操作*/buff = getmessage(&(tcbcurrenttcb.mq);signal(&tcbcurrenttcb.mutex);printf("%s receive a message from %s: %sn ", tcbcurrenttcb.name, tcbbuff->sender.name,buff->text);wait(&mutexfb);/*实现对空闲消息缓冲区的互斥操作*/returnTCBbuff(buff);signal(&mutexfb);signal(&sfb);void interrupt cpu_switch(void)disable();tcbcurrenttcb.ss=_SS;tcbcurrenttcb.sp=_SP;if(tcbcurrenttcb.state=RUNNING)tcbcurrenttcb.state=READY; currenttcb=Seeknext();tcbcurrenttcb.state=RUNNING; _SS=tcbcurrenttcb.ss; _SP=tcbcurrenttcb.sp;enable();struct buffer *getbuff(void)struct buffer *buff;buff=freebuff;freebuff=freebuff->next;return(buff);struct buffer *getmessage(struct buffer *qp)struct buffer *temp;temp = (*qp) ;(*qp) = (*qp)->next ;temp->next = NULL ;return(temp);int Seeknext(void)int i,ks;i=currenttcb+1; ks=0;disable();while(ks<NTCB)if(tcbi.state=READY)return i;i+;ks+;i=i%NTCB; printf("Seeknext tcb is error!n"); enable();void interrupt new_int8(void)(*old_int8)(); timecount+; if(timecount>=timeout) if(!DosBusy() disable(); tcbcurrenttcb.ss=_SS; tcbcurrenttcb.sp=_SP; if(tcbcurrenttcb.state=RUNNING) tcbcurrenttcb.state=READY; currenttcb=Seeknext();_SS=tcbcurrenttcb.ss;_SP=tcbcurrenttcb.sp;tcbcurrenttcb.state=RUNNING;timecount=0; enable();int all_finished(void) int i; for(i=1;i<NTCB;i+) if(tcbi.state=RUNNING|tcbi.state=READY|tcbi.state=BLOCKED) return 0; return 1;void Destroy(int i)if(tcbi.state=RUNNING)disable();tcbi.state=FINISHED;free(tcbi.stack);enable();return;void over(void)Destroy(currenttcb);printf("n %s has finished!nn", tcbcurrenttcb.name);cpu_switch();void f1(void)char s60="bbbbbbbbbbbbbbb"send("f2",s,60); signal(&fmutex);void f2(void) wait(&fmutex); receive();_