资源描述:
《操作系统线程设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、操作系统线程设计#include#include#include#include#defineFINISHED0#defineRUNNING1#defineREADY2#defineBLOCKED3#defineNTCB10#defineNBUF10#defineNTEXT50#defineGET_INDOS0x34#defineGET_CRIT_ERR0x5d06#defineTL2structtcb_stack{unsignedbp,di,si,ds,es,dx,cx,bx,ax,ip,cs,flags,off,seg
2、;};structbuffer{intsender;/*消息发送者的标识数*/intsize;/*消息长度*/chartext[NTEXT];/*消息正文*/structbuffer*next;/*指向下一个消息缓冲区的指针*/}*freebuf;typedefstruct{intvalue;structTCB*wq;}semaphore;semaphoremutexfb;/*消息缓冲区的互斥信号量*/semaphoresfb;/*空闲缓冲队列的计数信号量*/structTCB{unsignedchar*stack;unsignedss;unsignedsp;charstate;charnam
3、e[10];intID;structTCB*next;structbuffer*mq;/*消息队列队首指针*/semaphoremutex;/*消息队列的互斥信号量*/semaphoresm;/*消息队列的计数信号量*/}tcb[NTCB];typedefstructTCB*READYQUEUE;typedefint(far*codeptr)(void);charfar*indos_ptr=0;charfar*crit_err_ptr=0;intcurrent;inttimecount=0;intcount1,count2;READYQUEUEreadyhead;intDosBusy(void
4、);/*判断DOS是否忙或出现严重错误*/voidInitInDos(void);/*获得DOS状态标志位*/voidf1(void);voidf2(void);voidtcb_state();/*打印所有线程状态*/voidInitTcb();/*初始化TCB*/intall_finished(void);/*判断所有线程是否结束*/voidinterruptswtch(void);voidinterruptswtch2(void);voidinterruptnew_int8(void);/*新的时钟中断*/voidinterrupt(*old_int8)(void);voidCreate(
5、char*name,codeptrcode,intstacklen);/*线程创建函数*/voidOver();/*线程终止函数*/voidDestroy(inti);/*释放线程堆栈指针*/intChoose_FIFO();/*先进先出调度算法*/intChoose_Priority();/*基于优先级的调度算法*/voidP(semaphore*sem);/*P操作*/voidV(semaphore*sem);/*V操作*/voidwakeup_first(structTCB**qp);/*线程唤醒原语*/voidblock(structTCB**qp);/*线程阻塞原语*/voidadd
6、();voidchangecount1();structbuffer*getbuf(void);voidinsert(structbuffer**mq,structbuffer*buff);/*将buff所指的缓冲区插到*mq所指的缓冲队列末尾*/voidsend(char*receiver,char*a,intsize);voidFreebuf(structbuffer*buff);voidInitbuff(void);voidreceive(char*sender);voidSysInit(void);main(){inti,j,k;SysInit();strcpy(tcb[0].name
7、,"main");tcb[0].state=RUNNING;Create("f1",(codeptr)f1,1024);Create("f2",(codeptr)f2,1024);Create("f3",(codeptr)add,1024);tcb_state();swtch();/*setvect(8,new_int8);for(i=0;i<20;i++){putchar('c');for(j=