写优先读写者问题Word格式.docx

上传人:b****1 文档编号:444132 上传时间:2023-04-28 格式:DOCX 页数:23 大小:245.34KB
下载 相关 举报
写优先读写者问题Word格式.docx_第1页
第1页 / 共23页
写优先读写者问题Word格式.docx_第2页
第2页 / 共23页
写优先读写者问题Word格式.docx_第3页
第3页 / 共23页
写优先读写者问题Word格式.docx_第4页
第4页 / 共23页
写优先读写者问题Word格式.docx_第5页
第5页 / 共23页
写优先读写者问题Word格式.docx_第6页
第6页 / 共23页
写优先读写者问题Word格式.docx_第7页
第7页 / 共23页
写优先读写者问题Word格式.docx_第8页
第8页 / 共23页
写优先读写者问题Word格式.docx_第9页
第9页 / 共23页
写优先读写者问题Word格式.docx_第10页
第10页 / 共23页
写优先读写者问题Word格式.docx_第11页
第11页 / 共23页
写优先读写者问题Word格式.docx_第12页
第12页 / 共23页
写优先读写者问题Word格式.docx_第13页
第13页 / 共23页
写优先读写者问题Word格式.docx_第14页
第14页 / 共23页
写优先读写者问题Word格式.docx_第15页
第15页 / 共23页
写优先读写者问题Word格式.docx_第16页
第16页 / 共23页
写优先读写者问题Word格式.docx_第17页
第17页 / 共23页
写优先读写者问题Word格式.docx_第18页
第18页 / 共23页
写优先读写者问题Word格式.docx_第19页
第19页 / 共23页
写优先读写者问题Word格式.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

写优先读写者问题Word格式.docx

《写优先读写者问题Word格式.docx》由会员分享,可在线阅读,更多相关《写优先读写者问题Word格式.docx(23页珍藏版)》请在冰点文库上搜索。

写优先读写者问题Word格式.docx

用高级语言编写和调试一个采用写优先策略的读者写者问题的模拟程序。

1.4时间安排

1)分析设计贮备阶段(1天)

2)编程调试阶段(7天)

3)写课程设计报告、考核(2天)

第2部分实验要求

1.读者与写者至少包括ID、进入内存时间、读写时间三项内容,可在界面上进行输入。

2.读者与写者均有两个以上,可在程序运行期间进行动态增加读者与写者。

3.可读取样例数据(要求存放在外部文件中),进行读者/写者、进入内存时间、读写时间的初始化。

4.要求将运行过程用可视化界面动态显示,可随时暂停,查看阅览室中读者/写者数目、读者等待队列、读写时间、等待时间。

5.读写策略:

读写互斥、写写互斥、写优先(只要写者到达,就阻塞后续的所有读者,一旦阅览室无人,写者能最快进入阅览室;

在写者未出阅读室之前,又有新的读者与写者到达,仍然是写者排在前面)

第3部分主要思想

3.1基本思想

进程结构体:

structprocess

{

intname;

//进程序号

chartype;

//进程类别(判断是读者还是写者线程)

intstarttime;

//进程开始时间

intneedtime;

//进程读写需要的时间

intruntime;

//进程在内存中已运行的时间

structprocess*next;

};

创建三个链表,分别为process1、process2、process3。

process1存储各个进程的信息,process2存储就绪队列的进程信息,process3存储正在执行的进程的信息。

用整形数i模拟时间片,每次时间片开始时先将需要进入就绪队列的进程用ready()函数拉入就绪队列。

判断就绪队列的开头是读者还是写者,然后调用相应的函数Reader()或Writer()。

如若调用的是Writer(),还要调用sort()函数对就绪队列进程排序,将写者排在写者前面。

时间片结束时,对各个进程的runtime++,并判断进程是否执行完毕,如果完毕就其离开process3链表。

最后将时间片i++,反复循环,直到所有进程执行完毕。

3.2.主要功能模块流程图

采用“写优先“策略的”“读者—写者”问题

3.3测试用例及运行结果

程序运行实例如下:

1.选择1,运行界面如下:

2.选择2,运行界面如下:

3.在执行过程中动态暂停。

4.在进程暂停的情况下,按下A可以增加进程。

5.选择3,退出界面如下

第4部分源代码

#include<

stdio.h>

stdlib.h>

#include"

windows.h"

conio.h>

intmutex=1;

//互斥读写的信号量

intreadcount=0;

//用readcount变量来记录读者数

structprocess

intname;

chartype;

//进程类别(判断是读者还是写者)

intstarttime;

intneedtime;

intruntime;

//进程在内存中已运行的时间

structprocess*next;

process*process1=NULL,*process2=NULL,*process3=NULL;

voidinput()

process*p,*q;

q=process1=(process*)malloc(sizeof(process));

//在内存中给q分配了一个动态的存储空间

inta;

FILE*fp;

//是声明,声明fp是指针,用来指向FILE类型的对象

printf("

\t\t\t1.键盘输入进程\n"

);

\t\t\t2.文件读取进程\n"

\t\t\t3.退出\n"

\n请选择:

"

bb:

fflush(stdin);

scanf("

%d"

&

a);

switch(a)

{

case1:

printf("

输入进程数:

inti;

scanf("

for(i=1;

i<

=a;

i++)

{

p=(process*)malloc(sizeof(process));

q->

next=p;

printf("

第%d个进程的ID:

"

i);

scanf("

p->

name);

读\\写:

getchar();

%c"

type);

\n"

进入内存时间:

starttime);

读写时间:

needtime);

p->

runtime=0;

q=q->

next;

next=NULL;

}

p=process1;

process1=process1->

p->

free(p);

//功能:

与malloc()函数配对使用,释放malloc函数申请的动态内存

break;

case2:

if((fp=fopen("

123.txt"

"

r"

))==NULL)

{

文件打开失败!

exit(0);

while(!

(feof(fp)))

fscanf(fp,"

%d%c%d%d"

name,&

type,&

starttime,&

case3:

exit(0);

default:

\t\t您输入的有错误,请重新输入:

gotobb;

}

}

voidready(inti)//每次时间片开始时先将需要进入就绪队列的进程用ready()函数拉入就绪队列,准备

process*p,*q,*j,*k;

p=process1;

q=process2;

intt=0;

if(process2==NULL)

{

q=process2=(process*)malloc(sizeof(process));

q->

t=1;

else

while(q->

next!

=NULL)

{

q=q->

}

j=(process*)malloc(sizeof(process));

j->

next=process1;

while(p!

if(p->

starttime==i)

k=j;

while(k->

=p)

{

k=k->

}

k->

next=p->

q->

p=p->

else

process1=j->

free(j);

if(t==1)

p=process2;

process2=process2->

p->

free(p);

intwait(int&

a)

if(a<

=0)

return0;

a--;

return1;

voidsignal(int&

a++;

voidReader()

process*p;

p=process3;

if(process3==NULL)

p=process3=(process*)malloc(sizeof(process));

while(p->

if(readcount>

0)

next=process2;

p=p->

readcount++;

if((readcount==0)&

&

(wait(mutex)==1))

p=process3;

process3=process3->

voidWriter()

if((wait(mutex)==1)&

(process3==NULL))

process3=process2;

process3->

voidsort()//排序

j=p=(process*)malloc(sizeof(process));

p->

k=q=(process*)malloc(sizeof(process));

q->

while(process2!

if((process2->

type=='

w'

)||(process2->

W'

))

p->

process2=process2->

process2=j->

q=k->

k->

free(k);

if(process2!

next=q;

process2=q;

voidadd(inti)//添加

q=(process*)malloc(sizeof(process));

进程序号:

q->

getchar();

//清空输入缓冲区

延迟时间:

starttime=a+i;

读写时间:

if(process1!

=NULL)

process1=q;

voidmain()

\t\t\t**==================**\n"

\t\t\t#写优先的读者写者问题#\n"

\t\t\t**==================**\n\n"

inti=0;

input();

\n以下输出各时间片进程状态\n"

<

运行时如需暂停请输入P>

Sleep(1000);

while((process1!

=NULL)||(process2!

=NULL)||(process3!

=NULL))

i++;

if(process1!

ready(i);

if(process3!

if((process3->

)||(process3->

sort();

sort();

if(process2!

while((process2->

r'

R'

Reader();

if(process3!

if((process3->

))

break;

if(process2==NULL)

if((process2->

Writer();

while(p!

runtime++;

printf("

\n时间%d:

\t正执行进程:

if(p==NULL)

printf("

空>

while(p!

%d"

p->

p=p->

\n\t就绪队列:

{

p=q=(process*)malloc(sizeof(process));

while(process3!

if(process3->

needtime!

=process3->

runtime)

next=process3;

else

readcount--;

if(readcount==0)

signal(mutex);

else

signal(mutex);

process3=process3->

process3=q->

free(q);

Sleep(2000);

if(kbhit()==1)//检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 

{

charch;

ch=getch();

if((ch=='

p'

)||(ch=='

P'

\n已暂停,继续请按P\n"

如需添加进程请按A\n"

aa:

fflush(stdin);

ch=getchar();

if((ch=='

A'

a'

add(i);

添加完成!

while

(1)

if((ch=='

break;

else

gotoaa;

\n\n所有进程执行完毕\n"

system("

pause"

//暂停

main();

while

(1);

第5部分总结及参考文献

5.1总结

程序可实现键盘输入,读取样例数据,文件初始化,动态暂停和添加等功能,能够正确利用写优先策略是想读者写者问题的模拟,基本实现任务书的要求。

5.2参考文献

【1】《操作系统》(第三版).汤小丹,梁红兵.西安电子科技大学出版社.2007

【2】《C程序设计语言》.(第三版)谭浩强.清华大学出版社.2004

【3】《数据结构》(C语言版).严蔚敏,吴伟民.清华大学出版社.2007

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2