基于dos的多任务系统实现课程设计(编辑修改稿)内容摘要:

estroy(int id) { disable()。 free(tcb[id].stack)。 tcb[id].stack = NULL。 tcb[id].state = FINISHED。 printf(\n**** The thread %s has been destroyed ****\n,tcb[id].name)。 tcb[id].name[0] = 39。 \039。 enable()。 } void over(void) { destroy(now)。 swtch()。 } /* InitDos()函数:功能是获得 INDOS 标志的地址和严重错误标志的地址 */ void InitDos(void) { union REGS regs。 struct SREGS segregs。 /* 获得 INDOS 标志的地址 */ =GET_INDOS。 /* intdosx() : Turbo C 的库函数,其功能是调用 DOS 的 INT21H 中断 */ intdosx(amp。 regs,amp。 regs,amp。 segregs)。 /* MK_FP():不是一个函数,只是一个宏。 */ /*其功能是做段基址加上偏移地址的运算,也就是取实际地址。 */ indos_ptr=MK_FP(,)。 /*获得严重错误标志的地址 */ /*代码中用到的 _osmajor、 _osminor 是 Turbo C 的全程变量,其中前者为 */ /*DOS 版本号的主要部分,后者为版本号的次要部分。 */ if (_osmajor3) crit_err_ptr=indos_ptr+1。 else if (_osmajor==3 amp。 amp。 _osminor==0) crit_err_ptr=indos_ptr1。 else { =GET_CRIT_ERR。 intdosx(amp。 regs,amp。 regs,amp。 segregs)。 crit_err_ptr=MK_FP(,)。 } } /* DosBusy():函数功能是获得 Indos 标志及严重错误标志的值,判断是否 dos 忙: */ /* 如果返回值是 1,表示 dos 忙; */ /* 如果返回值是 0,表示 dos 不忙; */ /* 如果返回值是 1,表示还没有调用 InitDos() */ int DosBusy(void) { if (indos_ptr amp。 amp。 crit_err_ptr) return(*indos_ptr || *crit_err_ptr)。 else return(1)。 /* InitDos() hasn39。 t been called */ } void interrupt new_int8(void) { int loop=0。 (*old_int8)()。 if(++timecount TL) return。 if(DosBusy()) return。 disable()。 /*保存旧线程的指针 */ tcb[now].ss = _SS。 tcb[now].sp = _SP。 if(tcb[now].state == RUNNING) tcb[now].state = READY。 if( ++now == NTCB ) now = 1。 while(tcb[now].state != READY amp。 amp。 ++loop NTCB1) { now++。 if(now == NTCB) now = 1。 } if(tcb[now].state != READY) now = 0。 _SS = tcb[now].ss。 _SP = tcb[now].sp。 tcb[now].state = RUNNING。 timecount = 0。 /* tcb_state()。 */ enable()。 } void interrupt swtch(void) { int loop=0。 disable()。 tcb[now].ss=_SS。 tcb[now].sp=_SP。 if(tcb[now].state==RUNNING) tcb[now].state=READY。 /* 放入就绪队列中 */ if(++now==NTCB) now=0。 while(tcb[now].state!=READY amp。 amp。 loop++NTCB1) { now++。 if(now==NTCB) now=0。 } if(tcb[now].state!=READY) now=0。 _SS=tcb[now].ss。 _SP=tcb[now].sp。 /* 保存现场 */ tcb[now].state=RUNNING。 timecount=0。 enable()。 } void block(unsigned *chan, struct TCB **qp) {/*线程阻塞原语 */ int id。 struct TCB *tcbp。 id = now。 tcb[id].state = BLOCKED。 tcb[id].chan = chan。 if ((*qp) == NULL) (*qp) = amp。 tcb[id]。 else { tcbp = *qp。 while (tcbpnext != NULL) tcbp = tcbpnext。 tcbpnext = amp。 tcb[id]。 } tcb[id].next = NULL。 swtch()。 } void wakeup_first(struct TCB **qp) { /*线程唤醒原语 */ int i。 struct TCB *tcbp。 if ((*qp) == NULL) return。 tcbp = (*qp)。 (*qp) = (*qp)next。 tcbpstate = READY。 tcbpchan = NULL。 tcbpnext = NULL。 } void p(semaphore *sem) { struct TCB **qp。 disable()。 semvalue=semvalue1。 if(semvalue0){ qp=amp。 (semwq)。 block((unsigned *)sem,qp)。 printf(\n***i dont have semaphore so hangup***\n)。 } enable()。 } void v(semaphore *sem) { struct TCB **qp。 disable()。 qp=amp。 (semwq)。 semvalue=semvalue+1。 if(semvalue=0) wakeup_first(qp)。 enable()。 } struct buffer *remov(struct buffer **mq, int sende) { struct buffer *p, *q, *buff。 q = NULL。 p = *mq。 while ((psender != sende) amp。 amp。 (pnext != NULL)) { q = p。 p = pnext。 } if (psender == sende) { buff = p。 if (q == NULL) *mq = buffnext。 else qnext = buffnext。 buffnext = NULL。 return (buff)。 } else { return (NULL)。 } } void send(char *receiver,char *a,int size) { struct buffer *buff。 int i,id=1。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。