1、2.2算法思想 三个线性链表,分别为h1、h2、h3。h1为写入序列,h2为就绪序列,h3为执行序列。其中h1用来存放输入进去的读者和写者的信息,h2根据读入内存的时间将h1中的读者写者的信息进行排序,并将排完序的读者与写者信息存在h2中,把h2的内容调入到执行序列h3中,从而执行要求的内容。2.3主要功能模块流程图第3部分 主要的功能模块 3.1 数据结构 主要的数据结构及数据:int Wmutex=1;/互斥读写的信号量int readcount=0; /读者数目struct process/进程结构体 int ID; /进程序号 char type; /进程类别(判断是读者还是写者) i
2、nt starttime; /进程开始时间 int needtime; /进程读写需要的时间 int runtime; /进程在内存中已运行的时间 struct process *next;process *h1=NULL,*h2=NULL,*h3=NULL;/三个链表函数成员:void input()/输入信息函数void main()/主函数void choose()/选择函数void ready(int i)/进入就绪队列函数int wait(int &a)/等待队列函数void reader()/读写平等下的读者信息函数void writer()/读写平等下的写者信息函数void ad
3、d(int i)/动态增加函数void print(int i)/输出函数void leave()/离开执行队列3.2 测试用例及运行结果运行程序进入界面后,选择1,运行界面如图1-1;选择2,运行完后查看文件显示如图1-2;选择3查看信息,运行界面如图1-3;在执行过程中按下s可以暂停进程的执行,在进程暂停的情况下,按下a可以增加进程,运行结果如图1-4所示。 图 1-1图1-2图1-3图1-4第4部分 源代码#include stdlib.hconio.h#include windows.hvoid input();void main();struct processvoid choose
4、()/选择 int a; process *p,*q; q=h1=(process *)malloc(sizeof(process); FILE *fp; scanf(%d,&a); switch(a) case 1: /手动输入 int i,j; printf(tt输入进程数:); fp=fopen(file.txt,w+ scanf(i); fprintf(fp,%dn,i); for(j=1;i0;i-,j+) p=(process *)malloc(sizeof(process); q-next=p; printf(tt第%d个进程:n,j);tt进程序号t读或写tt开始时间t执行时间
5、ntt%d %c %d %dp-ID,&type,&starttime,&needtime); fprintf(fp,%d %c %d %dn,p-ID,p-type,p-starttime,p- p-runtime=0; q=q-next;next=NULL; fclose(fp); p=h1; h1=h1- p- free(p); break; case 2: /文件读入 if(fp=fopen(r)=NULL)文件打开失败! system(pause system(cls main(); fscanf(fp, for(j=1; fscanf(fp, p- break; case 3: i
6、nt k; if(fp=fopen( printf(tt进程序号t读或写tt开始时间t执行时间n fscanf(fp, k=0; for(j=0; if(p-type=r|p-R) k+;tt%dtt%ctt%dtt%dn j=j-k;tt读者数目:t%dn,k);tt写者数目: p=h1; case 4: exit(0); default : printf(tt您输入的有错误,请重新输入: system( void input() /输入函数tt*读写平等策略*nttt1.请输入进程信息nttt2.文件载入进程信息nttt3.查看进程信息nttt4.退出ntt*ntt请选择: choose(
7、);void ready(int i) /进入就绪队列 process *p,*q,*j,*k; q=h2; int t=0; if(h2=NULL) q=h2=(process *)malloc(sizeof(process); q- t=1; else while(q-next!=NULL) q=q- j=(process *)malloc(sizeof(process); j-next=head1; while(h1-starttime=i) h1=h1- q=q- j- if(h1=NULL) break; while(p! if(p- k=j; while(k-=p) k=k- k-
8、next=p- q- p=p- else h1=j- free(j); if(t=1) p=h2; h2=h2- free(p);a) a-; if(a if(readcountnext=h2; p=p- readcount+; if(readcount=0)&(wait(Wmutex)=1) else Wmutex+; p=h3; h3=h3-void writer() /读写平等下的写者信息 if(wait(Wmutex)=1)&(head3=NULL) h3=h2; h3-void add(int i) /动态增加 p=head1; q=(process *)malloc(sizeof(
9、process);tt进程序号: scanf(q-ID);ttt读或写: fflush(stdin);%ctype);tt开始时间: q-starttime=a+i;tt执行时间: if(h1!next=q; h1=q;void print(int i) /输出函数 process *p; while(p! p-runtime+; printf(ntt执行 %d :tt执行队列: if(p=NULL) while(p!%d p=p-ntt等待队列:void leave() /离开执行队列 p=q=(process *)malloc(sizeof(process); while(h3! if(h
10、3-needtime!=h3-runtime)next=h3; else if(h3-)|(h3-) readcount-; if(readcount=0) Wmutex=1; signal(Wmutex); h3=h3- h3=q- free(q);void main() int i=0; input(); while(h1!=NULL)|(h2!=NULL)|(h3!=NULL) i+; if(h1! ready(i); if(h2! while(h2- reader(); if(h3! if(h3-w break; if(h2=NULL) if(h2- writer(); print(i
11、); leave(); Sleep(1000); /交出线程占用CPU时间一秒钟 fflush(stdin);/清空缓冲区 char ch= ; if (kbhit()=1)/检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 ch=getch(); if(ch=S)|(ch=s tt已暂停,任意键继续.n添加新的进程输入an ch=getchar(); if(ch=a add(i);nntt执行完毕n Wmutex=1; readcount=0; while(1); 第5部分 总结及参考文献5.1 总结通过这次课设,我学到了很多课堂上学不到的知识,这次我做的是平等策略下的读者写者问题,执行读者与写者在公平竞争下的同步进程问题,另外可以实现进程的可视化显示、暂停和动态增加最后退出,实现了报告的要求,同时非常感谢老师的帮助,我会在这次课设之后,继续完善这个程序,提高自己的动手能力和实践能力。5.2 参考文献 1)汤小丹著 计算机操作系统第三版 西安电子科技大学出版社 2)谭浩强著 C语言程序设计 清华大学出版社 3)严蔚敏著 数据结构视频教程 清华大学出版社 4)张丽芬 刘利雄 王金玉编著 操作系统实验教程 清华大学出版社
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1