时间片轮转操作系统课程设计myprocess.doc

上传人:豆**** 文档编号:17606655 上传时间:2022-05-25 格式:DOC 页数:10 大小:165.50KB
返回 下载 相关 举报
时间片轮转操作系统课程设计myprocess.doc_第1页
第1页 / 共10页
时间片轮转操作系统课程设计myprocess.doc_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《时间片轮转操作系统课程设计myprocess.doc》由会员分享,可在线阅读,更多相关《时间片轮转操作系统课程设计myprocess.doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流时间片轮转操作系统课程设计myprocess.精品文档.#include#include#include#include/*状态码*/#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/*线程时间片轮转的时间*

2、/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

3、;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;uns

4、igned 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;/*对空闲缓冲区

5、处理的互斥信号量*/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)(v

6、oid);/*旧的时钟中断处理程序*/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(

7、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 *recei

8、ver,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);/*获取系

9、统原来的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

10、;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 next = temp2 ;temp1 = temp2 ;temp1-next = NULL ;void Init

11、InDos(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(_osmajor3)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; intdo

12、sx(®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() hasnt been called*/void InitTcb(void)/*TCB的初始化*/ int i; for(i=0;iNTCB;i+) tcbi.state=START; tcbi.name0=0; tcbi.next=NULL; t

13、cbi.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;freetcbds=_DS;stack-es=_ES;stack-flags=0x200;stack-ip=FP_OFF(code);stack-cs=FP_SEG(code);stack-off=FP_OFF(over);stack

14、-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;ivalue=sem-value-1;if(sem-valuewq);block(qp);enable();void signal(semaphore *sem)struct TCB *qp;disable();qp=&(sem-wq);sem-value=sem-value+1;if(sem-valuenext!=NULL)temp=temp-nex

15、t; 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

16、;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;

17、disable();for(i=0;isender=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

18、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-send

19、er.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=t

20、cbcurrenttcb.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

21、;i=currenttcb+1; ks=0;disable();while(ks=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_

22、finished(void) int i; for(i=1;iNTCB;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();_

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁