版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include <dos.h>#include <alloc.h>#include <stdlib.h>#include <stdio.h>#include <string.h>/*#include "indos.c"#include "exterr.c"*/#define GET_INDOS 0X34#define GET_GRIT_ERR 0X5D06#define NTCB 5#define NTEXT 20#define NBUF 5/*#define TL 3*/* state co
2、de */* null 0 not assigned */#define FINISHED 0#define RUNNING 1#define READY 2#define BLOCKED 3unsigned oldss,oldsp;static int current=0; /* the tcb index of the current process */int multstop=1;int timecount=0;int TL;int n =0;int buf1;char far *indos_ptr = 0 ; char far *crit_err_ptr = 0 ;/* the po
3、inter to the process's code */typedef int (far *codeptr)(void); /*定义了一个函数指针类型*/typedef struct int value; struct TCB *wq; semaphore;semaphore mutexfb=1,NULL;/* buf is a critical resource */semaphore sfb=NBUF,NULL;/* buf */semaphore mutex=1,NULL;semaphore sa=1,NULL,sb=0,NULL;struct buffer int id;
4、int size; char textNTEXT; struct buffer *next; bufNBUF,*freebuf;struct TCB int id; /* unique process id */ char name10; /* process's name given by user */ unsigned char *stack; /* the beginning address of the stack */ unsigned sp,ss; char state; /* the state of process */ unsigned *chan; /* even
5、t process is awainting */ struct buffer *mq; semaphore mutex; semaphore sm; struct TCB *next; /* link pointer */ tcbNTCB ;/* the registers pushed in the stack after entering an interrupt funtion */struct int_regs / unsigned bp,di,si,ds,es,dx,cx,bx,ax,ip,cs,flags;/;/*头文件的这个数据结构修改成这样 */struct int_regs
6、 unsigned bp,di,si,ds,es,dx,cx,bx,ax,ip,cs,flags,off,seg;/*struct char name10; codeptr code;tableNTCB="f1",f1; "f2",f2; "f3",f3; "f4",f4; ;*/void interrupt (*old_int8)(void);void interrupt new_int8(void);int create(char *name,codeptr code,int stck);void destro
7、y(int id);void over(void);void interrupt swtch(void);int all_finished(void);void free_all(void);void tcb_state(void);void InitTcb(void);int DosBusy(void) if(indos_ptr && crit_err_ptr) return (*indos_ptr|*crit_err_ptr); else return(-1);/*用于判断当前DOS是否处于繁忙状态*/void InitInDos() union REGS regs; st
8、ruct SREGS segregs; regs.h.ah=GET_INDOS; /*使用34H号系统功能调用*/ intdosx(®s,®s,&segregs); indos_ptr=MK_FP(segregs.es,regs.x.bx); if(_osmajor<3) crit_err_ptr=indos_ptr+1; /*严重错误在INDOS后一字节处*/ else if(_osmajor=3&&_osmajor=0) crit_err_ptr=indos_ptr-1; /*严重错误在INDOS前一字节处*/ else regs.
9、x.ax=GET_GRIT_ERR; intdosx(®s,®s,&segregs); crit_err_ptr=MK_FP(segregs.ds,regs.x.si); /*初始化DOS,取得INDOS标志和严重错误标志地址*/void InitTcb(void) int i; for(i=0;i<NTCB;i+) tcbi.stack=NULL; tcbi.state=FINISHED; tcbi.chan=NULL; 0='0' tcbi.next=NULL; tcbi.mq=NULL; tcbi.mutex.v
10、alue=1; tcbi.mutex.wq=NULL; tcbi.sm.value=0; tcbi.sm.wq=NULL; int create(char *name,codeptr code,int stck) struct int_regs far *r; int i,id=-1; unsigned char *ptr; for(i=0;i<NTCB;i+) if(tcbi.state=0) id=i; break; if(id=-1) return(0); disable(); tcbid.stack=(unsigned char *)malloc(stck); r=(struct
11、 int_regs *)(tcbi.stack+stck-1); r-; tcbid.ss=FP_SEG(r); tcbid.sp=FP_OFF(r); r->cs=FP_SEG(code); r->ip=FP_OFF(code); r->ds=_DS; r->es=_DS; r->flags=0x200; r->seg=FP_SEG(over); r->off=FP_OFF(over); tcbid.state=READY; for(i=0;i<10;i+,name+) if(i=*name)=NULL) break; tc
12、9='0' enable(); return(1);void destroy(int id) /*me:线程撤销函数*/ disable(); free(tcbid.stack); tcbid.stack=NULL; tcbid.state=FINISHED; printf("n * Thread %s is terminated。 *n",); 0='0' enable();void over(void) /*me:撤销线程并重新进行调度*/ destroy(current); swt
13、ch();void block(unsigned *chan,struct TCB *qp) int id; struct TCB *tcbp; id=current; tcbid.state=BLOCKED; tcbid.chan=chan; if(*qp)=NULL) (*qp)=&tcbid; else tcbp=*qp; while(tcbp->next!=NULL) tcbp=tcbp->next; tcbp->next=&tcbid; tcbid.next=NULL; swtch();void wakeup_first(struct TCB *qp
14、) int i; struct TCB *tcbp; if(*qp)=NULL) return; tcbp=(*qp); (*qp)=(*qp)->next; tcbp->state=READY; tcbp->chan=NULL; tcbp->next=NULL;void p(semaphore *sem) struct TCB *qp; disable(); sem->value=sem->value-1; if(sem->value<0) qp=&(sem->wq); block(unsigned *)sem,qp); enab
15、le();void v(semaphore *sem) struct TCB *qp; disable(); qp=&(sem->wq); sem->value=sem->value+1; if(sem->value<=0) wakeup_first(qp); enable();void interrupt swtch(void) int loop=0; disable(); /* save the old process */ tcbcurrent.ss=_SS; tcbcurrent.sp=_SP; if(tcbcurrent.state=RUNNIN
16、G) tcbcurrent.state=READY; /* find a new process and restore it */ if(+current=NTCB) current=0; while(tcbcurrent.state!=READY && loop+<NTCB-1) current+; if(current=NTCB) current=0; if(tcbcurrent.state!=READY) current=0; _SS=tcbcurrent.ss; _SP=tcbcurrent.sp; tcbcurrent.state=RUNNING; timec
17、ount=0; /* tcb_state();*/ enable();int all_finished(void) int i; for(i=1;i<NTCB;i+) if(tcbi.state!=FINISHED) return(0); return(1);void free_all(void) int i; for(i=0;i<NTCB;i+) if(tcbi.stack) 0='0' tcbi.state=FINISHED; free(tcbi.stack); tcbi.stack=NULL; void interrupt new_int8(
18、void) int loop=0; (*old_int8)(); if(+timecount<TL) return; if(DosBusy() return; disable(); tcbcurrent.ss=_SS; tcbcurrent.sp=_SP; if(tcbcurrent.state=RUNNING) tcbcurrent.state=READY; /* find a new process and restore it */ if(+current=NTCB) current=1; while(tcbcurrent.state!=READY && +loop
19、<NTCB-1) current+; if(current=NTCB) current=1; if(tcbcurrent.state!=READY) current=0; _SS=tcbcurrent.ss; _SP=tcbcurrent.sp; tcbcurrent.state=RUNNING; timecount=0;/* tcb_state();*/ enable();void tcb_state(void) int i; /* if(!multstop) return; */ printf("n"); for(i=0;i<NTCB;i+) printf(
20、"proc %d(%9s): ",i,); switch(tcbi.state) case READY:printf("READYn");break; case RUNNING:printf("RUNNINGn"); break; case FINISHED:printf("FINISHEDn"); break; case BLOCKED:printf("BLOCKEDn"); break; void init_buf(void) int i; for(i=0;i<NBU
21、F-1;i+) bufi.next=&bufi+1; bufi.next=NULL; freebuf=&buf0;struct buffer *getbuf(void) struct buffer *buff; buff=freebuf; freebuf=freebuf->next; return(buff);void insert(struct buffer *mq,struct buffer *buff) struct buffer *temp; if(buff=NULL) return; buff->next=NULL; if(*mq=NULL) *mq=bu
22、ff; else temp=*mq; while(temp->next!=NULL) temp=temp->next; temp->next=buff; struct buffer *remov(struct buffer *mq,int sender) struct buffer *p,*q,*buff; q=NULL; p=*mq; while(p->id!=sender)&&(p->next!=NULL) q=p; p=p->next; if(p->id=sender) buff=p; if(q=NULL) *mq=buff-&g
23、t;next; else q->next=buff->next; buff->next=NULL; return(buff); else return(NULL); 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,)=0) id=i; break; if(id=-1) enable(); return; p(&sfb); p(&mute
24、xfb); buff=getbuf(); v(&mutexfb); buff->id=current; buff->size=size; buff->next=NULL; for(i=0;i<buff->size;i+,a+) buff->texti=*a; p(&tcbid.mutex); insert(&(tcbid.mq),buff); v(&tcbid.mutex); v(&tcbid.sm); enable();int receive(char *sender,char *b) int i,size,id=-
25、1; struct buffer *buff; disable(); for(i=0;i<NTCB;i+) if(strcmp(sender,)=0) id=i; break; if(id=-1) enable(); return(0); p(&tcbcurrent.sm); p(&tcbcurrent.mutex); buff=remov(&(tcbcurrent.mq),id); v(&tcbcurrent.mutex); if(buff=NULL) v(&tcbcurrent.sm); enable(); return(-1
26、); size=buff->size; for(i=0;i<buff->size;i+,b+) *b=buff->texti; p(&mutexfb); insert(&freebuf,buff); v(&mutexfb); v(&sfb); enable(); return(size);/*codeptr lookup(char *name) int i; for(i=0;i<NTCB;i+) strcmp(name,)=0) return(tablei.code); return(NULL);*/void
27、f1(void) long i,j,k; for(i=0;i<1000;i+) putchar('a'); for(j=0;j<1000;j+) for(k=0;k<100;k+); void f2(void) long i,j,k; for(i=0;i<100;i+) putchar('b'); for(j=0;j<1000;j+) for(k=0;k<100;k+); /*int n=0;*/void f3(void) long i,j,k; int temp; for(i=0;i<100;i+) temp=n; d
28、elay(5); temp+; n=temp; for(j=0;j<1000;j+) for(k=0;k<99;k+); void f4(void) long i,j,k; int temp; for(i=0;i<100;i+) temp=n; temp+; delay(10); n=temp; for(j=0;j<1000;j+) for(k=0;k<100;k+); /*semaphore mutex=1,NULL;*/void f5(void) long i,j,k; int temp; for(i=0;i<100;i+) p(&mutex);
29、 temp=n; delay(5); temp+; n=temp; v(&mutex); for(j=0;j<1000;j+) for(k=0;k<99;k+); void f6(void) long i,j,k; int temp; for(i=0;i<100;i+) p(&mutex); temp=n; temp+; delay(10); n=temp; v(&mutex); for(j=0;j<1000;j+) for(k=0;k<100;k+); /*int buf1;/semaphore sa=1,NULL,sb=0,NULL;*
30、/void cp1(void) int i; for(i=0;i<10;i+) n+; buf1=n; void iop1(void) int i,tem; for(i=0;i<10;i+) tem=buf1; printf("Out %d:%dn",i,tem); void cp(void) int i; for(i=0;i<10;i+) n+; p(&sa); buf1=n; v(&sb); void iop(void) int i,tem; for(i=0;i<10;i+) p(&sb); tem=buf1; v(&am
31、p;sa); printf("Out %d:%dn",i,tem); void sender(void) int i,j; char a10;loop: for(i=0;i<10;i+) strcpy(a,"message"); a7='0'+n; a8=0; send("receiver",a,strlen(a); printf("sender:Message "%s" has been sentn",a); n+; receive("receiver"
32、;,a); if (strcmp(a,"ok")!=0) printf("Not be committed,Message should be resended!n");/*接收进程没确认,需重新发送消息*/ goto loop; else printf("Committed,Communication is finished!n");/*发送者得到接收者的确认,通信结束*/void receiver(void) int i,j,size; char b10; for(i=0;i<10;i+) b0=0; while(size=
33、receive("sender",b)=-1); printf("receiver: Message is received-");/已接收到消息- for(j=0;j<size;j+) putchar(bj); printf("n"); strcpy(b,"ok"); send("sender",b,3);/* 发送确认消息 */void main(void) int sele=-1; char name10; InitInDos(); InitTcb(); old_int8=getve
34、ct(8); /* create thread 0 */ strcpy(,"main"); tcb0.state=RUNNING; current=0; for(;sele;) do clrscr(); printf("nnnt*n"); printf("t* 0.Quit Multi-Task System: *n");/*退出多任务系统*/ printf("t* 1.Threads perform in sequence way: *n");/*线索的"顺序"执行*/ pr
35、intf("t* 2.Threads perform in concurrent way: *n");/*线索的并发执行*/ printf("t* 3.Threads share critical resource(no mutual exclusion): *n");/*线索同时共享临界资源*/ printf("t* 4.Threads share critical resource(in mutual exclusion): *n");/*线索互斥共享临界资源*/ printf("t* 5.Threads running
36、 wihtout synchronism: *n");/*线索没做好同步时情况*/ printf("t* 6.Threads running in synchronism: *n");/*线索同步时的情况*/ printf("t* 7.Communication among threads: *n");/*线索间的通信*/ printf("t*nn"); printf("t Please enter the function num.(0-7):"); scanf("%d",&
37、sele); while(sele<0|sele>7) ; switch(sele) case 1: create("f1",(codeptr)f1,1024); create("f2",(codeptr)f2,1024); clrscr(); printf("t No deprivation in two threads' concurrent running: n");/*不剥夺方式下两个线索的并发执行*/ printf("t Thread f1 continuous output 'a
38、9; 1000 times ;n");/*其中线索f1不断输出字母a,共1000次*/ printf("t Thread f2 continuous output 'b' 1000 times 。n");/*而线索f2不断输出字母b,共100次*/ printf("t Press any key to continue!n");/*按任意键继续*/ getch(); swtch(); break; case 2: create("f1",(codeptr)f1,1024); create("f2&q
39、uot;,(codeptr)f2,1024); clrscr(); printf("t Round-Robin in two threads' concurrent running:n");/*时间片轮转方式下两个线索的并发执行*/ printf("t Thread f1 continuous output 'a' 1000 times ;n");/*其中线索f1不断输出字母a,共1000次*/ printf("t Thread f2 continuous output 'b' 1000 times 。n
40、");/*而线索f2不断输出字母b,共100次。*/ printf("t Please configurate the length of time slice(1/18.2):");/*请设置时间片的大小(单位为1/18.2秒)*/ scanf("%d",&TL); printf("t Press any key to continue!n"); getch(); setvect(8,new_int8); swtch(); break; case 3: n=0; create("f3",(cod
41、eptr)f3,1024); create("f4",(codeptr)f4,1024); clrscr(); printf("t Threads share critical resource(no mutual exclusion).n");/* 线索同时共享临界资源的情况*/ printf("t Thread f3 and f4 share the same variable parameter 'n'n");/*线索f3和f4共享一变量n*/ printf("t Thread f3 continuou
42、s add 1 to 'n' 100 times;n"); /*times线索f3不断对n进行加1操作,共进行100次*/ printf("t Thread f4 also continuous add 1 to 'n' 100 times;n");/*线索f4也不断对n进行加1操作,共进行100次*/ printf("t Result should be 200.n");/*因此,最后n的值应为200*/ printf("t Press any key to continue!n"); ge
43、tch(); TL=1; setvect(8,new_int8); swtch(); break; case 4: n=0; create("f3",(codeptr)f5,1024); create("f4",(codeptr)f6,1024); clrscr(); printf("t Threads share critical resource(in mutual exclusion): n");/*线索互斥共享临界资源*/ printf("t Thread f3 and f4 share the same varia
44、ble parameter 'n'n");/*线索f3和f4共享一变量n*/ printf("t Thread f3 continuous add 1 to 'n' 100 times;n");/*线索f3不断对n进行加1操作,共进行100次*/ printf("t Thread f4 also continuous add 1 to 'n' 100 times;n");/*线索f4也不断对n进行加1操作,共进行100次*/ printf("t Result should be 200.
45、n");/*因此,最后n的值应为200*/ TL=1; printf("t Press any key to continue!n"); getch(); setvect(8,new_int8); swtch(); break; case 5: n=0; create("iop",(codeptr)iop1,1024); create("cp",(codeptr)cp1,1024); clrscr(); printf("t Computing process and Output process concurren
46、tly running wihtout synchronism: n");/*没同步的计算进程和输出进程的并发执行*/ printf("t Computing process 'cp' continuous calculate and put the result into a buffer,n");/*并将其放入一缓冲区*/ printf("t Printing process 'iop' orderly fetch 'cp' and output it.n");/*打印进程iop依次取出cp放
47、入缓冲区中的数,将它输出出来*/ printf("t Here ,'cp' computed 10 results:0,1,2,3,4,5,6,7,8,9n");/*本例中,cp计算出十个数,分别为0,1,2,3,4,5,6,7,8,9*/ printf("t Press any key to continue!n"); getch(); TL=1; setvect(8,new_int8); swtch(); break; case 6: n=0; create("iop",(codeptr)iop,1024); cr
48、eate("cp",(codeptr)cp,1024); clrscr(); printf("t Computing process and Output process concurrently running in synchronism: n");/*经过同步的计算进程和输出进程的并发执行:*/ printf("t Computing process 'cp' continuous calculate and put the result into a buffer,nn");/*计算进程cp不断计算数,并将其放入一缓冲区,*/ printf("t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论