版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
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); /*定義了一個函數(shù)指針類型*/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;/;/*頭文件的這個數(shù)據(jù)結(jié)構(gòu)修改成這樣 */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);/*用于判斷當(dāng)前DOS是否處于繁忙狀態(tài)*/void InitInDos() union REGS regs; st
8、ruct SREGS segregs; regs.h.ah=GET_INDOS; /*使用34H號系統(tǒng)功能調(diào)用*/ intdosx(®s,®s,&segregs); indos_ptr=MK_FP(segregs.es,regs.x.bx); if(_osmajor<3) crit_err_ptr=indos_ptr+1; /*嚴重錯誤在INDOS后一字節(jié)處*/ else if(_osmajor=3&&_osmajor=0) crit_err_ptr=indos_ptr-1; /*嚴重錯誤在INDOS前一字節(jié)處*/ else regs.
9、x.ax=GET_GRIT_ERR; intdosx(®s,®s,&segregs); crit_err_ptr=MK_FP(segregs.ds,regs.x.si); /*初始化DOS,取得INDOS標(biāo)志和嚴重錯誤標(biāo)志地址*/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:線程撤銷函數(shù)*/ disable(); free(tcbid.stack); tcbid.stack=NULL; tcbid.state=FINISHED; printf("n * Thread %s is terminated。 *n",); 0='0' enable();void over(void) /*me:撤銷線程并重新進行調(diào)度*/ 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");/*接收進程沒確認,需重新發(fā)送消息*/ goto loop; else printf("Committed,Communication is finished!n");/*發(fā)送者得到接收者的確認,通信結(jié)束*/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);/* 發(fā)送確認消息 */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");/*退出多任務(wù)系統(tǒng)*/ printf("t* 1.Threads perform in sequence way: *n");/*線索的"順序"執(zhí)行*/ pr
35、intf("t* 2.Threads perform in concurrent way: *n");/*線索的并發(fā)執(zhí)行*/ 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");/*不剝奪方式下兩個線索的并發(fā)執(zhí)行*/ 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");/*按任意鍵繼續(xù)*/ 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");/*時間片輪轉(zhuǎn)方式下兩個線索的并發(fā)執(zhí)行*/ 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):");/*請設(shè)置時間片的大小(單位為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的值應(yīng)為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的值應(yīng)為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");/*沒同步的計算進程和輸出進程的并發(fā)執(zhí)行*/ printf("t Computing process 'cp' continuous calculate and put the result into a buffer,n");/*并將其放入一緩沖區(qū)*/ printf("t Printing process 'iop' orderly fetch 'cp' and output it.n");/*打印進程iop依次取出cp放
47、入緩沖區(qū)中的數(shù),將它輸出出來*/ printf("t Here ,'cp' computed 10 results:0,1,2,3,4,5,6,7,8,9n");/*本例中,cp計算出十個數(shù),分別為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");/*經(jīng)過同步的計算進程和輸出進程的并發(fā)執(zhí)行:*/ printf("t Computing process 'cp' continuous calculate and put the result into a buffer,nn");/*計算進程cp不斷計算數(shù),并將其放入一緩沖區(qū),*/ printf("t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廢棄資源的綜合利用技術(shù)與水資源管理研究考核試卷
- 城市綠化管理的濕地公園與自然保護區(qū)考核試卷
- 蘇州科技大學(xué)天平學(xué)院《企業(yè)戰(zhàn)略管理》2023-2024學(xué)年第一學(xué)期期末試卷
- 營銷投放年終總結(jié)
- 生物競賽答辯
- 信息系統(tǒng)的數(shù)據(jù)科學(xué)與數(shù)據(jù)分析考核試卷
- 公共設(shè)施管理中的供應(yīng)鏈管理考核試卷
- 時尚服飾品牌的消費心理學(xué)研究考核試卷
- 慢一點也沒關(guān)系讀書分享
- 2023年梧州市教育局直管學(xué)校招聘緊缺專任教師筆試真題
- 醫(yī)療影像技術(shù)部門KPI設(shè)計
- 高分子職業(yè)生涯規(guī)劃
- 人工流產(chǎn)術(shù)健康宣教課件
- 老年高血壓管理指南
- 急性腦卒中的護理
- ?;愤\輸安全檢測與監(jiān)控
- 2024年耐高溫尼龍行業(yè)分析報告及未來發(fā)展趨勢
- 碳咨詢服務(wù)行業(yè)報告
- 化學(xué)品管理中的危險化學(xué)品替代
- 地鐵撞人事故應(yīng)急措施及救援預(yù)案
- 商務(wù)展會禮儀培訓(xùn)
評論
0/150
提交評論