欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

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

    • 资源ID:17606655       资源大小:165.50KB        全文页数:10页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    【精品文档】如有侵权,请联系网站删除,仅供学习与交流时间片轮转操作系统课程设计myprocess.精品文档.#include<stdio.h>#include<stdlib.h>#include<dos.h>#include<string.h>/*状态码*/#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/*线程时间片轮转的时间*/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;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;unsigned 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;/*对空闲缓冲区处理的互斥信号量*/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)(void);/*旧的时钟中断处理程序*/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(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 *receiver,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);/*获取系统原来的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;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 < NBUF ; i +) /*链接成一个队列*/temp2 = (struct buffer *)malloc(sizeof(struct buffer) ;temp1->next = temp2 ;temp1 = temp2 ;temp1->next = NULL ;void InitInDos(void)/* get the address of INDOS flags */regs.h.ah=GET_INDOS;intdosx(&regs,&regs,&segregs);indos_ptr=MK_FP(segregs.es,regs.x.bx);/* get the address of CRIT_ERR flag */if(_osmajor<3)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; intdosx(&regs,&regs,&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() hasn't been called*/void InitTcb(void)/*TCB的初始化*/ int i; for(i=0;i<NTCB;i+) tcbi.state=START; tcbi.name0='0' tcbi.next=NULL; tcbi.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;freetcb<NTCB;freetcb+)if(tcbfreetcb.state=START|tcbfreetcb.state=FINISHED)break;if(freetcb=NTCB)printf("nCreate was failed!n");return (-1);tcbfreetcb.state=READY; strcpy(tcbfreetcb.name,name);tcbfreetcb.stack=(unsigned *)malloc(stacklen);tcbfreetcb.stack=tcbfreetcb.stack+stacklen;stack=(struct int_regs *)tcbfreetcb.stack-1;stack->ds=_DS;stack->es=_ES;stack->flags=0x200;stack->ip=FP_OFF(code);stack->cs=FP_SEG(code);stack->off=FP_OFF(over);stack->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;i<NTCB;i+) switch(tcbi.state) case START: printf("n tcb%d %s's state is starting;",i,tcbi.name); break; case FINISHED: printf("n tcb%d %s's state is finished;",i,tcbi.name); break; case RUNNING: printf("n tcb%d %s's state is running;",i,tcbi.name); break; case READY: printf("n tcb%d %s's state is ready;",i,tcbi.name); break; case BLOCKED: printf("n tcb%d %s's state is ready;",i,tcbi.name); break; printf("n");void wait(semaphore *sem)struct TCB *qp;disable();sem->value=sem->value-1;if(sem->value<0)qp=&(sem->wq);block(qp);enable();void signal(semaphore *sem)struct TCB *qp;disable();qp=&(sem->wq);sem->value=sem->value+1;if(sem->value<=0)wakeup(qp); enable();void block(struct TCB *qp)struct TCB *temp;temp=(*qp);disable();tcbcurrenttcb.state=BLOCKED;if(*qp)=NULL) (*qp)=&tcbcurrenttcb;elsewhile(temp->next!=NULL)temp=temp->next; 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;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;disable();for(i=0;i<NTCB;i+)if(strcmp(receiver,tcbi.name)=0)id=i; break;if(id=-1)printf("Error: Receiver not exit!n");enable();return; wait(&sfb); wait(&mutexfb); buff=getbuff(); signal(&mutexfb); buff->sender=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 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->sender.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=tcbcurrenttcb.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;i=currenttcb+1; ks=0;disable();while(ks<NTCB)if(tcbi.state=READY)return i;i+;ks+;i=i%NTCB; printf("Seeknext tcb is error!n"); enable();void interrupt new_int8(void)(*old_int8)(); timecount+; if(timecount>=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_finished(void) int i; for(i=1;i<NTCB;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();_

    注意事项

    本文(时间片轮转操作系统课程设计myprocess.doc)为本站会员(豆****)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

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

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

    收起
    展开