时间片轮转操作系统课程设计myprocess.doc
《时间片轮转操作系统课程设计myprocess.doc》由会员分享,可在线阅读,更多相关《时间片轮转操作系统课程设计myprocess.doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 时间 轮转 操作系统 课程设计 myprocess
限制150内