《实验一:基于dos的多任务系统实现》由会员分享,可在线阅读,更多相关《实验一:基于dos的多任务系统实现(19页珍藏版)》请在金锄头文库上搜索。
1、#include #include #include #define NTCB 10 /* 系统允许运行的最大线程数*/ /* 状态码常量定义 */ #define FINISHED 0 /* 表示线程处于终止态或TCB 是空白状态 */ #define RUNNING 1 /* 表示线程牌运行态 */ #define READY 2 /* 表示线程处于就绪 */ #define BLOCKED 3 /* 表示线程处于阻塞态 */ #define CLOCK_NUM 2 /* 每个线程所运行的时钟数 */ #define NTEXT 1000 /* 消息长度 */ #define NBUFF
2、5 /* 空闲缓冲区最大数量 */ #define GET_INDOS 0x34 #define GET_CRIT_ERR 0x5d06 char far *indos_ptr =0; char far *crit_err_ptr=0; int current; /* 当前进程 tcb 的下标 */ int timecount; /* 时钟中断次数 */ typedef struct int value; struct TCB *wq;/* 阻塞队列首个TCB */ semaphore; semaphore mutex = 1, NULL; semaphore empty = NBUFF, N
3、ULL; semaphore full = 0, NULL; semaphore mutexfb = 1,NULL; semaphore sfb = NBUFF,NULL; struct buffer int sender; /* 消息发送者的内部标识 */ int size; /* 消息长度 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); strcp
4、y(tcbi.name,name); tcbi.state=READY; tcbi.ss = FP_SEG(regs_pt); tcbi.sp = FP_OFF(regs_pt); printf(“The thread %s has been created!n“, tcbi.name); return i; void destroy(int id) if(tcbid.state = FINISHED) return; disable(); free(tcbid.stack); tcbid.stack = NULL; tcbid.state = FINISHED; void over() /*
5、 线程结束后的处理工作 */ destroy(current); my_swtch(); enable(); int find() int i; for (i = current + 1; i != current; i+) if (i = NTCB) i = 0; if (tcbi.state = READY) break; return i; void interrupt my_swtch(void) int i; disable(); tcbcurrent.ss = _SS; tcbcurrent.sp = _SP; if(tcbcurrent.state = RUNNING) tcbc
6、urrent.state = READY; i = find(); _SS = tcbi.ss; _SP = tcbi.sp; tcbi.state = RUNNING; current = i; timecount = 0; enable(); void interrupt new_int8(void) (*old_int8)(); timecount+; if(timecount=CLOCK_NUM) if(!DosBusy() my_swtch(); void TCBState(void) int i; for(i=0;inext!=NULL) tmp=tmp-next; tmp-nex
7、t= my_swtch(); enable(); void wakeup(struct TCB *pptcb) disable(); if(*pptcb=NULL) return ; (*pptcb)-state=READY; *pptcb=(*pptcb)-next; enable(); void p(semaphore *sem) struct TCB *qp; disable(); sem-value=sem-value-1; /* printf(“sem-value=%dn“,sem-value); */ if(sem-valuewq); block(qp); enable(); vo
8、id v(semaphore *sem) struct TCB *qp; disable(); qp= sem-value=sem-value+1; /* printf(“sem-value=%dn“,sem-value); */ if(sem-valuenext=(struct buffer *)malloc(sizeof(struct buffer); tmp=tmp-next; tmp-next=NULL; struct buffer *getbuf(void) struct buffer *buff; buff=freebuf; freebuf=freebuf-next; return
9、(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(char *receiver,char *a,int size) struct buffer *buff; int i,id=-1; disable();
10、for(i=0;isender=current; buff-size=size; buff-next=NULL; for(i=0;isize;i+,a+) buff-texti=*a; /*将消息缓冲区插入到接收线程的消息队列末尾*/ p( insert( v( v( enable(); struct buffer* remov(struct buffer *mq,int sender) int i,j; struct buffer *front,*rear; front=*mq;rear=*mq; while(front!=NULL) if(front-sender=sender) brea
11、k; rear=front; front=front-next; if(front=*mq) *mq=(*mq)-next; else rear-next=rear-next-next; return front; int receive (char *sender,char *str) /*取出 sender 发出的消息并复制到str 中 */ struct buffer *buff,*tmp; int i,id=-1,size; disable(); for(i=0;isize; /* printf(“get message succesn“); */ for(i=0;isize;i+)
12、stri=buff-texti; /* 将取完消息后的消息缓冲区插入到系统空闲消息缓冲队列中 freebuf*/ buff-next=NULL; buff-sender = -1; buff-size = 0; buff-text0 = 0; p( if(freebuf=NULL) freebuf=buff; else tmp=freebuf; while(tmp-next!=NULL) tmp=tmp-next; tmp-next=buff; v( v( enable(); return size; void _sleep() int k1,k2; for(k1=0;k1=10000;k1+
13、) for(k2=0;k2=10000;k2+); void sender(void) int i,k1,k2; char sendStr30; printf(“thread1 start to send messagen“); for(i=0;i10;i+) sprintf(sendStr,“this is %d message from thread1“,i); send(“receiver“,sendStr,strlen(sendStr)+1); printf(“thread1 send message %dn“,i); _sleep(); printf(“thread1 send me
14、esage overn“); void receiver(void) int i; char recStr30; for(i=0;i10;i+) receive(“sender“,recStr); printf(“%s n“,recStr); _sleep(); _sleep(); void producer(void) while(1) p( p( pro+; printf(“producer product a product pro=%dn“,pro); v( v( _sleep(); void consumer(void) while(1) p( p( printf(“consumer
15、 use a product pro=%dn“,pro); pro-; v( v( _sleep(); _sleep(); void f1(void) int i; for(i=0;i40;i+) putchar(a); _sleep(); void f2(void) int i; for(i=0;i30;i+) putchar(b); _sleep(); _sleep(); int main() InitDos(); InitTcb(); InitBuf(); old_int8=getvect(8); /* 获取系统原来的时钟中断服务程序的入口地址 */ strcpy(tcb0.name,“main“); tcb0.state=RUNNING; current=0; tcb0.ss = _SS; tcb0.sp = _SP; create(“f1“,(codeptr)f1,1024); create(“f2“