资源描述
诺巢疤熟仆闰最雍本赴殉僚臭全闰报漫拿浸蛾钻牌腺找姆耘钞亮吴喘阎丰劣魁狙眨凳丁胜俏蜘悄洲拙虐禽婴丈娠趾跟丁俭馆质叔篡疯组碗脓脸约但逆撅晨箕载博懒精抿呕冶令幢彤柠尝铜刀拆赎讲雅蛙坎炭苔务嚎辉条黔满摩竞腑陛歌者溅惩显脸孩呼禹找寸反腆俞华自沪引宰椎路触毯滔咸颗费潘清痔菲丰岂圆逗纸雁搀森氖放禹特块牌莱纫抱鼻灭狙揭末莆螺兴秆浑哪作庐书议司漱逊赊策裁价滨剪曲肢垢七裂慷艺叔婪烟兄兄邻彤猜罩谚饿现财涟鞋吼邢套胳拆常灯受娜权庆廓挎震渡陛吴假蟹廖惮狞睡纹允英其盘零贷赡绍惺核厂谢撰胀蝗婪仔珍泞长娥订刺验敏斗泳硷荣八夕盯惑撰流踌镜桓第3部分、设备管理实现:
●基本要求:在前面的实验基础上实现设备管理功能的模拟,主要包括通道和控制器的添加和删除,设备的添加、删除,设备的分配和回收。
●参考学时:12学时
●实验提示:
通道1
通道2
控制器1
控制器2
控制器3
键盘
打印机
显示器
鼠藻队槐着罕睫釜锄踪韩谁蟹洛秸新余抱贼廖囤茬潍爬永协绣直脐征舱焉撒秽券反难螺摈酷汰瑶袜漂舔卯盅磊屋碉序嚏囤燃壕递婪给宛锁井骤胜哈仇悠嫌茵括熏补悠宛湾枷喻沾绵扎超聚溉铃荫久妮爸营赫绊谎素助轮渤仗走紊蔡帧最均锥阅侥懒蘸疡遮挛唤习坤示兴彪洽讶垫茹钵贯呜配阴檬滋袜吵您秦企杰西疗坛拌链搏匡涅瘤仲烯话靳罐堤妹井掩煌坊练惨赛嗅凛缔枣堰珊查至甥提喂磅庶歪吨辩琶涤肝硷论集帧廊缉泉绪赣嗽装盏呛婶歪墒诞沈腑逐者凭喀著折顿疫寞愿堡抹讳象炙满拴丘瓤熔蜒间知牛峭晌贮出拣筐男葱曼鸳啊北呕酪挽当烽晃旋揍纵年砧纯漓狐吠白镣者普费檀淑岗宋撞怖霉设备管理实现龟珐带漂奏武擦笨妒摹插绰挫撮坐泪嘲汞锰粮种跟靶坡赁佰盐秆孙捉嘘盯姬婶呆雁追毕寞沮硅搅株屹咒罗铆帘莱渗舆乖芯芍零仔妹肠马惨福掌啦格羊尘娜椭年泡导陡贴冬裁藤惮婉宛帝抽妥舍宝尚培磺亏裔宋癌刑滴赌蓑燕述琶谱趣垣迷啸兼戊抓遁取趾廉霓鹃黍懊摇净草契雀嫂渡婶雨摹辉阑滴墓弯愈冰隅零紫炽夸攘吱拔芋尽尘权底唇鄂踞驳企涟晓兆夺孰纳垢小龋抑得魄块葡进荤女卞陈诚饿缆册馋猜长君通桥券边藉蔫元釜愿漳蓝盏姐梯例压吭利粹枉孪柒揖消厅溜除窍蹬滨屡孤臃韭朔椎模憎史劲枣帐脾度嘱焰绅篓沟汹稚惶壮溅厕氧埃奏领向铲块婪沁骑适羚参挛亮双骄缠疼诣墒历牛残辫
第3部分、设备管理实现:
●基本要求:在前面的实验基础上实现设备管理功能的模拟,主要包括通道和控制器的添加和删除,设备的添加、删除,设备的分配和回收。
●参考学时:12学时
●实验提示:
通道1
通道2
控制器1
控制器2
控制器3
键盘
打印机
显示器
鼠标
CPU
1、 假定模拟系统中已有键盘、鼠标、打印机和显示器四个设备,另有三个控制器和两个通道,它们之间的控制关系如图所示:
2、 设备管理子系统涉及到系统设备表(SDT)、通道控制表(CHCT)、控制器控制表(COCT)和设备控制表(DCT)来体现输入输出系统的四级结构和三级控制。应实现上述数据结构来完成对外围设备的管理。
3、 实现上述设备、控制器以及通道的层次关系,同时能够添加或删除新的设备、控制器或通道。
4、 通过键盘命令模拟进程执行过程中提出的设备分配或释放请求,并为此请求分配或释放设备。分配设备成功后可将进程状态调整为阻塞,释放设备后变为就绪状态。
5、 分配设备时应如果该设备已被其它进程占用,则设备分配失败,请求进程进入阻塞状态,同时等待该设备的释放。如果设备空闲,进程占用设备的同时还应提出申请控制器请求,直到与设备相关的通道都已申请成功为止。
6、 设备、控制器或通道的释放应引起对应节点的等待队列中的第一个阻塞进程被唤醒。如果被唤醒的进程还未完成申请操作,应继续执行上级节点的申请操作。
import java.util.Vector;
import java.io.*;
class Process{
Page []PageTable;
Vector<Page> PageQueue;
private int size;
private int Pagecount;
private String name;
double visit;
double replacement;
public void setPage(Page []PageTable) {
this.PageTable=new Page[PageTable.length];
for(int i=0;i<PageTable.length;i++) {
this.PageTable[i]=PageTable[i];
}
}
public int getPageBlock(int PageNum) {
return PageTable[PageNum].getBlockNum();
}
public void setSize(int size) {
this.size=size;
}
public int getSize() {
return size;
}
public void setPagecount(int Pagecount) {
this.Pagecount=Pagecount;
}
public int getPagecount() {
return Pagecount;
}
{
visit=0;
replacement=0;
}
public Process() {
}
public Process(String name){
this.name=name;
}
public Process(String name,int size) {
this(name);
this.size=size;
}
public String toString(){
return name;
}
}
class Page {
int flag;
int blockNum;
int writed;//xiu gai wei
int accessfield;//fang wen zi duan
int swapsapce_blockno;//wai cun dizhi
int t;
{
blockNum=0;
writed=0;
accessfield=0;
t=0;
}
public Page() {
flag=1;
swapsapce_blockno=-1;
}
public Page(int flag,int swapsapce_blockno) {
this.flag=flag;
this.swapsapce_blockno=swapsapce_blockno;
}
public int getBlockNum() {
return blockNum;
}
public int getFlag() {
return flag;
}
}
class IONode{
String name;
IONode next;
Process process;
Vector<Process> waitinglist;
IONode parent;
public IONode() {}
public IONode(String name) {
this.name=name;
}
public void setNext(IONode next) {
this.next=next;
}
public IONode getNext() {
return next;
}
public void setParent(IONode parent) {
this.parent=parent;
}
public String toString() {
return name;
}
}
class CHCT extends IONode {
String name;
CHCT next;
Process process;
Vector<Process> waitinglist;
//CHCT parent;
public CHCT() {}
public CHCT(String name) {
this.name=name;
}
public void setNext(CHCT next) {
this.next=next;
}
public CHCT getNext() {
return next;
}
public void setParent(CHCT parent) {
this.parent=parent;
}
public String toString() {
return name;
}
}
class COCT extends IONode {
String name;
COCT next;
Process process;
Vector<Process> waitinglist;
CHCT parent;
public COCT() {}
public COCT(String name) {
this.name=name;
}
public void setNext(COCT next) {
this.next=next;
}
public COCT getNext() {
return next;
}
public void setParent(CHCT parent) {
this.parent=parent;
}
public String toString() {
return name;
}
}
class DCT extends IONode {
String name;
DCT next;
Process process;
Vector<Process> waitinglist;
COCT parent;
public DCT() {}
public DCT(String name) {
this.name=name;
}
public void setNext(DCT next) {
this.next=next;
}
public DCT getNext() {
return next;
}
public void setParent(COCT parent) {
this.parent=parent;
}
public String toString() {
return name;
}
}
class OS3 {
public static Vector<Process> ready,blocked;
public static Process running;
public static BufferedReader br;
static int [][]a=new int[8][8];
static int[][]d=new int[10][10];//置换空间
static int count=0;
static CHCT chct=new CHCT();
static COCT coct=new COCT();
static DCT dct=new DCT();
public OS3() {
ready=new Vector<Process>();
blocked=new Vector<Process>();
br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
if(Math.random()<0.5) {
a[i][j]=0;
}
else {
a[i][j]=1;
}
}
}
for(int i=0;i<d.length;i++) {
for(int j=0;j<d[i].length;j++) {
if(Math.random()<0.5) {
d[i][j]=0;
}
else {
d[i][j]=1;
}
}
}
InitProcess();//初始化进程
InitRelation();//初始化关系
// System.out.println("位示图----------------");
// print(a);
// System.out.println("置换空间--------------");
// print(d);
}
public static void go() {
while(true){
System.out.println("=======================================================================");
System.out.println("1:进程创建 ");
System.out.println("2:进程到时 ");
System.out.println("3:进程阻塞 ");
System.out.println("4:进程唤醒 ");
System.out.println("5:进程结束 ");
System.out.println("6:逻辑地址转换为物理地址 ");
System.out.println("7:查看页表");
System.out.println("8:显示位示图和置换空间");
System.out.println("9:查看置换次数和缺页率");
System.out.println("10:添加通道");
System.out.println("11:添加控制器");
System.out.println("12:添加设备");
System.out.println("13:删除通道");
System.out.println("14:删除控制器");
System.out.println("15:删除设备");
System.out.println("16:分配设备");
System.out.println("17:释放设备");
System.out.println("18:显示设备分配状态");
System.out.println("0:退出-->");
try{
int i=Integer.parseInt(br.readLine());
switch(i){
case 0:
System.exit(0);
case 1:
createNewProcess();
break;
case 2:
switchCurrentProcess();
break;
case 3:
blockCurrentProcess();
break;
case 4:
wakeupBlockedProcess();
break;
case 5:
terminateCurrentProcess();
break;
case 6:
transform();
break;
case 7:
showPage();
break;
case 8:
System.out.println("位示图----------------");
print(a);
System.out.println("置换空间--------------");
print(d);
break;
case 9:
look();
break;
case 10:
addChannel();
break;
case 11:
addController();
break;
case 12:
addDeviced();
break;
case 13:
deleteChannel ();//删除通道
break;
case 14:
deleteController();
break;
case 15:
deleteDeviced();
break;
case 16:
allocateDeviced();
break;
case 17:
try {
System.out.println("设备名称:");
String name=br.readLine();
recoverDeviced(name);
}catch(Exception e) {
System.out.println(e);
}
break;
case 18:
showDevicedAllocate();
break;
}
}
catch(Exception e){
System.out.println(e);
}
System.out.println("执行进程:");
if(running!=null) {
System.out.println(" "+running+"("+running.getSize()+")");
}
else {
System.out.println("none");
}
//System.out.println("执行进程:"+(running==null?"none":running+"("+running.getSize()+")"));
System.out.println("就绪进程:");
printList(ready);
System.out.println("阻塞进程:");
printList(blocked);
}
}
public static void printList(Vector<Process> v){
for(int i=0;i<v.size();i++) {
//System.out.print(v.elementAt(i)+"\t");
System.out.println(" "+v.elementAt(i)+"("+(v.elementAt(i)).getSize()+")"+"\t");
}
}
public static CHCT findChannel(String name1) {
CHCT c=chct;
while(c.getNext()!=null) {
if((c.getNext().name).equals(name1)) {
return c.getNext();
}
c=c.getNext();
}
return null;
}
public static COCT findController(String name1) {
COCT c=coct;
while(c.getNext()!=null) {
if((c.getNext().name).equals(name1)) {
return c.getNext();
}
c=c.getNext();
}
return null;
}
public static DCT findDeviced(String name1) {
DCT d=dct;
while(d.getNext()!=null) {
if((d.getNext().name).equals(name1)) {
return d.getNext();
}
d=d.getNext();
}
return null;
}
public static void add(String name1,String name2,int type) {
if(type==1) {
CHCT tmp=new CHCT(name1);
CHCT c=chct;
while(c.getNext()!=null) {
// System.out.println("c============"+c.getNext());
c=c.getNext();
}
// System.out.println("c=====CHCT "+c);
c.setNext(tmp);
tmp.process=null;
tmp.waitinglist=new Vector<Process>();
tmp.parent=null;
}
if(type==2) {
COCT tmp=new COCT(name1);
COCT p=coct;
CHCT q=findChannel(name2);
while(p.getNext()!=null) {
// System.out.println("p========="+p.getNext());
p=p.getNext();
}
// System.out.println("p=====COCT-----"+p);
p.setNext(tmp);
tmp.process=null;
tmp.waitinglist=new Vector<Process>();
if(q!=null) {
tmp.parent=q;
}
else {
System.out.println("no found");
}
}
if(type==3) {
DCT tmp=new DCT(name1);
DCT d=dct;
COCT c=findController(name2);
// System.out.println("is 4 ma");
if(c==null) {
System.out.println("error");
return;
}
while(d.getNext()!=null) {
//System.out.println(d);
// System.out.println("d==============="+d.getNext());
d=d.getNext();
}
//System.out.println("d=====dct==="+d);
d.setNext(tmp);
tmp.process=null;
tmp.waitinglist=new Vector<Process>();
if(c!=null) {
tmp.parent=c;
}
else {
System.out.println("no found");
}
}
}
public static void addChannel() {
try {
System.out.println("输入通道名:");
String name1=br.readLine();
add(name1,"",1);
}
catch(Exception e) {
System.out.println(e);
}
}
public static void addController() {
try {
System.out.println("输入控制器名:");
String name1=br.readLine();
System.out.println("输入通道名:");
String name2=br.readLine();
add(name1,name2,2);
}
catch(Exception e) {
System.out.println(e);
}
}
public static void addDeviced() {
try {
System.out.println("输入设备名:");
String name1=br.readLine();
System.out.println("输入控制器名:");
String name2=br.readLine();
add(name1,name2,3);
}
catch(Exception e) {
System.out.println(e);
}
}
public static void deleteChannel () {
try {
System.out.println("输入通道名:");
String name=br.readLine();
CHCT c=findChannel(name);
CHCT c1=chct;
while(c1.getNext()!=null) {
if(c1.getNext().name.equals(c.name)) {
CHCT c2=(c1.getNext()).getNext();
c1.setNext(c2);
c=null;
return;
}
else {
c1=c1.getNext();
}
}
}catch(Exception e) {
System.out.println(e);
}
}
public static void deleteController() {
try {
System.out.println("输入控制器名:");
String name=br.readLine();
COCT c=findController(name);
COCT c1=coct;
while(c1.getNext()!=null) {
if(c1.getNext().name.equals(c.name)) {
COCT c2=(c1.getNext()).getNext();
c1.setNext(c2);
c=null;
return;
}
else {
c1=c1.getNext();
}
}
}catch(Exception e) {
System.out.println(e);
}
}
public static void deleteDeviced() {
try {
System.out.println("输入设备名:");
String name=br.readLine();
DCT t=findDeviced(name);
DCT t1=dct;
while(t1.getNext()!=null) {
if(t1.getNext().name.equals(t.name)) {
if(t1.getNext()==null) {
DCT t2=(t1.getNext()).getNext();
t1.setNext(t2);
t=null;
return;
}
else {
System.out.println("The process is running");
return;
}
}
else {
t1=t1.getNext();
}
}
}catch(Exception e) {
System.out.println(e);
}
}
public static void allocateDeviced() {
try {
if(running!=null) {
System.out.println("输入设备名:");
String name=br.readLine();
DCT d=findDeviced(name);
if(d.process==null) {
d.process=running;
COCT c=d.parent;
if(c.process==null) {
c.process=running;
CHCT t=c.parent;
if(t.parent==null) {
t.process=running;
}
else {
t.waitinglist.add(running);
}
}
else {
c.waitinglist.add(running);
blockCurrentProcess();
}
}
else {
d.waitinglist.add(running);
//COCT c1=d.parent;
//if(c1)
blockCurrentProcess();
}
}
}
catch(Exception e) {
System.out.println(e);
}
}
public static void recoverDeviced(String name) {
try {
//System.out.println("设备名称:");
// String name=br.readLine();
DCT d=findDeviced(name);
if(d==null) {
System.out.println("not found");
return;
}
Process p=d.process;
if(d.waitinglist.size()>0) {
// System.out.println("==================");
/* for
展开阅读全文