操作系统实验报告readerwriter.docx

上传人:b****4 文档编号:13896289 上传时间:2023-06-19 格式:DOCX 页数:31 大小:45.57KB
下载 相关 举报
操作系统实验报告readerwriter.docx_第1页
第1页 / 共31页
操作系统实验报告readerwriter.docx_第2页
第2页 / 共31页
操作系统实验报告readerwriter.docx_第3页
第3页 / 共31页
操作系统实验报告readerwriter.docx_第4页
第4页 / 共31页
操作系统实验报告readerwriter.docx_第5页
第5页 / 共31页
操作系统实验报告readerwriter.docx_第6页
第6页 / 共31页
操作系统实验报告readerwriter.docx_第7页
第7页 / 共31页
操作系统实验报告readerwriter.docx_第8页
第8页 / 共31页
操作系统实验报告readerwriter.docx_第9页
第9页 / 共31页
操作系统实验报告readerwriter.docx_第10页
第10页 / 共31页
操作系统实验报告readerwriter.docx_第11页
第11页 / 共31页
操作系统实验报告readerwriter.docx_第12页
第12页 / 共31页
操作系统实验报告readerwriter.docx_第13页
第13页 / 共31页
操作系统实验报告readerwriter.docx_第14页
第14页 / 共31页
操作系统实验报告readerwriter.docx_第15页
第15页 / 共31页
操作系统实验报告readerwriter.docx_第16页
第16页 / 共31页
操作系统实验报告readerwriter.docx_第17页
第17页 / 共31页
操作系统实验报告readerwriter.docx_第18页
第18页 / 共31页
操作系统实验报告readerwriter.docx_第19页
第19页 / 共31页
操作系统实验报告readerwriter.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

操作系统实验报告readerwriter.docx

《操作系统实验报告readerwriter.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告readerwriter.docx(31页珍藏版)》请在冰点文库上搜索。

操作系统实验报告readerwriter.docx

操作系统实验报告readerwriter

实验题目:

ReaderWriter实验

 

一.实验目的:

1.通过编写和调试程序以加深对进程、线程管理方案的理解

2.熟悉Windows多线程程序设计方法

二.实验原理:

读者-写者允许多个读者同时读一个数据对象,因为读文件不会使数据发生混乱,但不允许一个写者进程与其他读者进程或写者进程同时访问该数据对象。

文件是各个进程能互斥访问临界资源,读者进程和写者进程之间互斥。

在读者进程中,可以有多个读者读数据库,在读者进程的计数要互斥,避免发生错误,同时注意当第一个读者进程读时,一定要封锁写者进程。

当读者进程逐渐撤离时,要针对计数变量进行互斥操作,若当前为最后一个读者进程,读完后应唤醒写者进程。

所以应该分四类可能性:

1.读者优先权比写者高,并且不用调配。

2.在一个读者已经占有文件的时候,全体读者的优先权才比写者高

3.写者的优先权比读者的优先权高

4.所有写者的和所有读者有相同的优先权

三.实验代码:

DataBaseBuffer:

importjava.awt.*;

publicclassDataBaseBufferextendsCanvas{

int[]readWantQ;

int[]writerWantQ;

String[]wantQ;

intframeDelay=1560;

privateintwriterID;

privateintreaderCount;

privateintwantCount;

privateintreaderWantCount,writerWantCount;

privateintwriterCount;

privateintwn,rn;//thenumberofreadersandwriters

privateintreadTop,readBottom,writerTop,writerBottom;

privateintwantTop,wantBottom;

privateFontfont;

privateFontMetricsfm;

privatebooleanreadLock=false;

privatebooleanwriteLock=false;

publicDataBaseBuffer(){

resize(500,300);

setBackground(Color.white);

font=newFont("TimesRoman",Font.BOLD,18);

fm=getFontMetrics(font);

}

publicvoidsetSize(intreaderN,intwriterN)

{

rn=readerN;

wn=writerN;

readTop=readBottom=writerTop=writerBottom=0;

readerCount=writerCount=readerWantCount=writerWantCount=0;

wantTop=wantBottom=0;

wantCount=0;

writerID=0;

readLock=false;

writeLock=false;

wantQ=newString[rn+wn];

writerWantQ=newint[wn];

readWantQ=newint[rn];

repaint();

}

publicsynchronizedvoidenterQueue(Strings,intid)

{

wantQ[wantTop]=s+id;

wantTop++;

wantCount++;

repaint();

}

publicsynchronizedvoiddequeue(Strings,intid)

{

Stringstr;

str=s+id;

while(!

wantQ[0].equals(str))

{

try{wait();}catch(InterruptedExceptione){}

}

for(inti=0;i<(wantTop-1);i++)

{

wantQ[i]=wantQ[i+1];

}

wantTop--;

wantCount--;

repaint();

}

publicsynchronizedvoidchangePosition(Strings,intid)

{

Stringstr;

Stringtmp;

intpos=0;//tofindthepostingof1stwriter

Stringwtr;

wtr=s+id;

 

while(!

(wantQ[pos].equals(wtr)))

{

pos++;

}

for(inti=0;i

{

System.out.println(wantQ[i]);

}

str=wantQ[pos];

for(inti=pos;i>0;i--)

{

wantQ[i]=wantQ[i-1];

}

wantQ[0]=str;

repaint();

for(inti=0;i

{

System.out.println(wantQ[i]);

}

}

publicsynchronizedbooleanhasWriterWant()

{

return(writerWantCount>0);

}

publicsynchronizedbooleanhasReaderWant()

{

return(readerWantCount>0);

}

publicsynchronizedvoidacquireReadLock(ReaderWriterAppletapplet,intid)

{

readWantQ[readTop]=id;//numsistheindexforreadWantQ

readTop=(readTop+1)%rn;

readerWantCount++;

repaint();

notifyAll();

enterQueue("R",id);

applet.r[id].status=1;//wantin

applet.mc.println(applet.r[id].status,"r",id);

try{applet.r[id].sleep(frameDelay);}catch(InterruptedExceptione){}

if(applet.writerPriority)

{

while(hasWriterWant()||writeLock)

{

applet.r[id].status=3;

applet.mc.println(applet.r[id].status,"r",id);

try{wait();}catch(InterruptedExceptione){}

}//endofwhileloop

}//endofifstatement

elseif(applet.readerPriority)

{

while(readWantQ[readBottom]!

=id)

{

try{wait();}catch(InterruptedExceptione){}

}

changePosition("R",id);

}

else

{

applet.r[id].status=3;

applet.mc.println(applet.r[id].status,"r",id);

while(!

wantQ[wantBottom].equals("R"+id))

{

try{wait();}catch(InterruptedExceptione){}

}

}

while(writeLock)//ifthereisanywriteriswriting

{

applet.r[id].status=3;

applet.mc.println(applet.r[id].status,"r",id);

try{wait();}catch(InterruptedExceptione){}

}

if(readLock==false)

{

readLock=true;

notifyAll();

}

readBottom=(readBottom+1)%rn;

readerWantCount--;

dequeue("R",id);

readerCount++;

repaint();

applet.r[id].status=2;

applet.mc.println(applet.r[id].status,"r",id);

System.out.println("Reader"+id+"isreading");

notifyAll();

}

publicsynchronizedbooleanhasReader()

{

return(readerCount>0);

}

publicsynchronizedvoidreleaseReadLock(ReaderWriterAppletapplet,intid)

{

readerCount--;

notifyAll();

if(!

hasReader())

{

readLock=false;

notifyAll();

applet.r[id].status=4;

applet.mc.println(applet.r[id].status,"r",id);

}

repaint();

}

publicsynchronizedvoidacquireWriteLock(ReaderWriterAppletapplet,intid)

{

writerWantQ[writerTop]=id;

writerTop=(writerTop+1)%wn;

writerWantCount++;

notifyAll();

repaint();

enterQueue("W",id);

applet.w[id].status=1;//wantin

applet.mc.println(applet.w[id].status,"w",id);

try{applet.w[id].sleep(frameDelay);}catch(InterruptedExceptione){}

 

if(applet.writerPriority)

{

while(writerWantQ[writerBottom]!

=id)

{

try{wait();}catch(InterruptedExceptione){}

}

changePosition("W",id);

while(readLock||writeLock)

{

try{wait();}catch(InterruptedExceptione){}

}

}

elseif(applet.readerPriority)

{

while(!

(wantQ[wantBottom].equals("W"+id))||hasReaderWant()||readLock||writeLock)

{

try{wait();}catch(InterruptedExceptione){}

}

System.out.println("Writer"+id+"moveforward");

}

else

{

while(!

(wantQ[wantBottom].equals("W"+id)))

{

try{wait();}catch(InterruptedExceptione){}

}

while(readLock||writeLock)

{

try{wait();}catch(InterruptedExceptione){}

}

}

writeLock=true;

System.out.println("Writer"+id+"Gotthelock******");

notifyAll();

dequeue("W",id);

writerBottom=(writerBottom+1)%wn;

writerID=id;

writerWantCount--;

writerCount++;

notifyAll();

repaint();

applet.w[id].status=2;

applet.mc.println(applet.w[id].status,"w",id);

}

publicsynchronizedvoidreleaseWriteLock(ReaderWriterAppletapplet,intid)

{

System.out.println("Writer"+id+"releasedthelock");

writerCount--;

writerID=0;

writeLock=false;

notifyAll();

repaint();

}

 

publicvoidclear()

{

writerBottom=writerTop=0;

readBottom=readTop=0;

writerWantCount=0;

readerWantCount=0;

readerCount=0;

writerCount=0;

readLock=writeLock=false;

writerWantQ=newint[wn];

readWantQ=newint[rn];

wantQ=newString[wn+rn];

wantTop=wantBottom=0;

}

publicvoidpaint(Graphicsg){

intxpos=630;

intypos=5;

g.setFont(newFont("TimesRoman",Font.BOLD,11));

g.setColor(Color.green);

g.draw3DRect(xpos,ypos,10,10,true);

g.fillRect(xpos,ypos,10,10);

g.drawString("Reading",xpos+15,ypos+10);

g.setColor(Color.red);

g.draw3DRect(xpos,ypos+14,10,10,true);

g.fillRect(xpos,ypos+14,10,10);

g.drawString("Writing",xpos+15,ypos+25);

g.setColor(Color.blue);

g.draw3DRect(xpos,ypos+28,10,10,true);

g.fillRect(xpos,ypos+28,10,10);

g.drawString("Empty",xpos+15,ypos+40);

g.setFont(newFont("TimesRoman",Font.BOLD,14));

g.setColor(Color.blue);

xpos=40;

ypos=50;

g.drawString("WaitingQueue",xpos-5,ypos-20);

inti=wantBottom;

for(intj=0;j

{

if(wantQ[i].equals("W1")||wantQ[i].equals("W2")

||wantQ[i].equals("W3")||wantQ[i].equals("W4")

||wantQ[i].equals("W5"))

{

g.setColor(Color.red);

g.drawString(wantQ[i],xpos+450-30*j,ypos-18);

g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);

}

if(wantQ[i].equals("R1")||wantQ[i].equals("R2")

||wantQ[i].equals("R3")||wantQ[i].equals("R4")

||wantQ[i].equals("R5"))

{

g.setColor(Color.green);

g.drawString(wantQ[i],xpos+450-30*j,ypos-18);

g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);

}

i=(i+1)%(wn+rn);

}

if(readLock)g.setColor(Color.green);

elseif(writeLock)g.setColor(Color.red);

elseg.setColor(Color.blue);

g.draw3DRect(xpos+250,ypos+20,100,100,true);

g.fillRect(xpos+250,ypos+20,100,100);

if(readLock)

{

g.setColor(Color.black);

g.drawString("Reading",xpos+270,ypos+60);

}

elseif(writeLock)

{

g.setColor(Color.black);

g.drawString("W"+Integer.toString(writerID),xpos+280,ypos+45);

g.drawString("Writing",xpos+270,ypos+60);

}

}

}

MessageCanvas:

importjava.awt.*;

classMessageCanvasextendsCanvas

{

privateFontfont;

privateFontMetricsfm;

privateint[]writerStatus;

privateint[]readerStatus;

privateintmsgHeight;

privateintmsgWidth;

privateintpn,cn;

privateintframeDelay=256;

publicMessageCanvas()

{

resize(size().width,50);

setBackground(Color.green);

font=newFont("TimesRoman",1,18);

fm=getFontMetrics(font);

msgHeight=fm.getHeight();

}

publicvoidsetMessage(intwriterN,intreaderN)

{

pn=writerN;

cn=readerN;

writerStatus=newint[pn+1];

readerStatus=newint[cn+1];

repaint();

}

voidprintln(Strings)

{

msgWidth=fm.stringWidth(s);

repaint();

}

voidprintln(ints,Stringst,intid)

{

if(st.equals("w"))

writerStatus[id]=s;

else

readerStatus[id]=s;

repaint();

}

voidprintln(ints,intnumber,Stringst,intid)

{

if(st.equals("w"))

{

writerStatus[id]=s;

}

else

{

readerStatus[id]=s;

}

repaint();

}

publicvoidpaint(Graphicsg)

{

g.setFont(font);

intxpos=60;

intypos=40;

g.drawString("StatusofReaders:

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

当前位置:首页 > 经管营销 > 经济市场

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

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