您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 实验一基于dos的多任务系统实现
#includedos.h#includestdio.h#includestdlib.h#defineNTCB10/*系统允许运行的最大线程数*//*状态码常量定义*/#defineFINISHED0/*表示线程处于终止态或TCB是空白状态*/#defineRUNNING1/*表示线程牌运行态*/#defineREADY2/*表示线程处于就绪*/#defineBLOCKED3/*表示线程处于阻塞态*/#defineCLOCK_NUM2/*每个线程所运行的时钟数*/#defineNTEXT1000/*消息长度*/#defineNBUFF5/*空闲缓冲区最大数量*/#defineGET_INDOS0x34#defineGET_CRIT_ERR0x5d06charfar*indos_ptr=0;charfar*crit_err_ptr=0;intcurrent;/*当前进程tcb的下标*/inttimecount;/*时钟中断次数*/typedefstruct{intvalue;structTCB*wq;/*阻塞队列首个TCB*/}semaphore;semaphoremutex={1,NULL};semaphoreempty={NBUFF,NULL};semaphorefull={0,NULL};semaphoremutexfb={1,NULL};semaphoresfb={NBUFF,NULL};structbuffer{intsender;/*消息发送者的内部标识*/intsize;/*消息长度=NTEXT个字节*/chartext[NTEXT];/*消息正文*/structbuffer*next;/*指向下一个消息缓冲区的指针*/};structbuffer*freebuf;/*空闲缓冲区*/semaphoremutexfb;semaphoresfb;/*空闲缓冲区的信号量*/semaphorefull,empty,mutex;/*用于读者写者问题*/intpro=0;/*产品数量*/structTCB{unsignedchar*stack;/*线程堆栈的起始地址*/unsignedss;/*堆栈段址*/unsignedsp;/*堆栈指针*/charstate;/*线程状态*/charname[10];/*线程的外部标识符*/structTCB*next;/*下一个TCB指针*//*以下三个变量用于线程通信*/structbuffer*mq;/*接收线程的消息队列队首指针*/semaphoremutex;/*接收线程的消息队列的互斥信号量*/semaphoresm;/*接收线程的消息队列的计数信息量,用于实现同步*/}tcb[NTCB];structint_regs{unsignedbp/*基址指针寄存器*/,di/*目的变址寄存器*/,si/*源变址寄存器*/,ds/*数据段段地址*/,es/*附加数据段*/,dx,cx,bx,ax/*能用/累加器....*/,ip,cs,/*代码段的段地址*//*代码段的段内偏移地址*/flags,/*flags寄存器的允许中断位*/off,seg;/*撤销线程代码的偏移地址*//*撤销线程代码的段址*/};/*def*//*马龙龙*/voidInitDos(void);intDosBusy(void);typedefint(far*codeptr)(void);/*self*/voidInitTcb(void);/*initializethetcb*/voidsender();voidreceiver();/*曹亚娟*//*self*/intcreate(char*name,codeptrcode,intstck);/*self*/voiddestroy(intid);/*self*/voidover(void);/*沈伟臣*//*self*/intfind(void);/*findthenextthread*//*self*/voidinterruptmy_swtch(void);/*switchtoanotherthread*/voidinterrupt(*old_int8)(void);/*self*/voidinterruptnew_int8(void);/*盛竹青*/voidtcb_state(void);intisFinished(void);/*checkwhetherallthreadisfinished*/voidf1(void);voidf2(void);voidproducer(void);voidconsumer(void);/*赵怀瑞*/voidblock(structTCB**p);voidwakeup(structTCB**p);voidp(semaphore*sem);voidv(semaphore*sem);/*梁宏燏*//*self*/voidInitBuf(void);/*self*/structbuffer*getbuf(void);voidinsert(structbuffer**mq,structbuffer*buff);voidsend(char*receiver,char*a,intsize);/*滕越*//*self*/structbuffer*remov(structbuffer**mq,intsender);/*self*/intreceive(char*sender,char*b);voidsleep();voidInitDos(void){unionREGSregs;structSREGSsegregs;regs.h.ah=GET_INDOS;intdosx(®s,®s,&segregs),indos_ptr=MK_FP(segregs.es,regs.x.bx);/*MK_FP()getrealaddress*/if(_osmajor3)crit_err_ptr=indos_ptr+1;elseif(_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);}}intDosBusy(void){if(indos_ptr&&crit_err_ptr)return(*indos_ptr||*crit_err_ptr);elsereturn-1;}voidInitTcb(void){inti;for(i=0;iNTCB;i++){tcb[i].stack=NULL;tcb[i].state=FINISHED;tcb[i].name[0]='\0';tcb[i].next=NULL;tcb[i].mq=NULL;tcb[i].mutex.value=1;tcb[i].sm.value=0;}}intcreate(char*name,codeptrcode,intstck){inti;structint_regs*regs_pt;for(i=0;iNTCB;i++){if(tcb[i].state==FINISHED)break;}tcb[i].stack=(char*)malloc(sizeof(char)*stck);regs_pt=(structint_regs*)(tcb[i].stack+stck)-1;regs_pt-cs=FP_SEG(code);regs_pt-ip=FP_OFF(code);regs_pt-flags=0x200;regs_pt-ds=_DS;regs_pt-es=_DS;regs_pt-seg=FP_SEG(over);regs_pt-off=FP_OFF(over);strcpy(tcb[i].name,name);tcb[i].state=READY;tcb[i].ss=FP_SEG(regs_pt);tcb[i].sp=FP_OFF(regs_pt);printf(Thethread%shasbeencreated!\n,tcb[i].name);returni;}voiddestroy(intid){if(tcb[id].state==FINISHED)return;disable();free(tcb[id].stack);tcb[id].stack=NULL;tcb[id].state=FINISHED;}voidover(){/*线程结束后的处理工作*/destroy(current);my_swtch();enable();}intfind(){inti;for(i=current+1;i!=current;i++){if(i==NTCB)i=0;if(tcb[i].state==READY){break;}}returni;}voidinterruptmy_swtch(void){inti;disable();tcb[current].ss=_SS;tcb[current].sp=_SP;if(tcb[current].state==RUNNING)tcb[current].state=READY;i=find();_SS=tcb[i].ss;_SP=tcb[i].sp;tcb[i].state=RUNNING;current=i;timecount=0;enable();}voidinterruptnew_int8(void){(*old_int8)();timecount++;if(timecount=CLOCK_NUM){if(!DosBusy()){my_swtch();}}}voidTCBState(void){inti;for(i=0;iNTCB;i++){switch(tcb[i].state){case0:printf(Thestateoftcb[%d](%s)isfinished\n,i,tcb[i].name);break;case1:printf(Thestateoftcb[%d](%s)isrunning\n,i,tcb[i].name);break;case2:printf(Thestateoftcb[%d](%s)isready\n,i,tcb[i].name);break;case3:printf(Thestateoftcb[%d](%s)isblocked\n,i,tcb[i].name);break;}}}intisFinished(){/*判断除主线程外的其他线程是否已经完成*/inti;for(i=1;iNTCB;i++){if(tcb[i].state!=FINISHED)return0;}return1;}voidblock(structTCB**pptcb){structTCB*tmp;disable();tcb[current].state=BLOCKED;if(*pptcb==NULL)*pptcb=&tcb[current];else{tmp=*pptcb;while(tmp-next!=NULL){tmp=tmp-next;}tmp-next=&tcb[current];}my_swtch();enable();}voidwakeup(structTCB**pptcb){disable();if(*pptcb==NULL)return;(*pptcb)-state=READY;*pptcb=(*pptcb)-next;enable();}voidp(semaphore*sem){structTCB**qp;disable();sem-value=sem-value-1;/*printf(sem-value=%d\n,sem-value);*/if(sem-value0){qp=&(sem-wq);bloc
本文标题:实验一基于dos的多任务系统实现
链接地址:https://www.777doc.com/doc-2457714 .html