当前位置:文档之家› 操作系统大作业

操作系统大作业

操作系统大作业Java语言实现班级:08级计二班姓名:湖南科技大学作业一:生产者与消费者——多线程与同步问题class MyThread {int id;MyThread(int id) {= id;}public String toString() {return"线程" + id;}}class MyStack {tart();new Thread(c).start();}}运行结果截图:作业二:多级反馈队列调度算法;public class Operation {;public class ProcessPCB {;importimportpublic class UserFrame extends JFrame{private static final long serialVersionUID = 1L;JLabel pnamelabel = new JLabel("进程名:");JTextField pnametext = new JTextField();JLabel listlabel1 = new JLabel("正在执行的进程:");JLabel listlabel2 = new JLabel("就绪队列1:");JLabel listlabel3 = new JLabel("就绪队列2:");JLabel listlabel4 = new JLabel("就绪队列3:");JLabel listlabel5 = new JLabel("完成队列:");JTextArea listarea1 = new JTextArea();JTextArea listarea2 = new JTextArea();JTextArea listarea3 = new JTextArea();JTextArea listarea4 = new JTextArea();JTextField executingarea = new JTextField();JButton createbtn = new JButton("创建进程");JButton dispatchbtn = new JButton("开始调度");JButton reset = new JButton("清空");Operation operation = new Operation(); quals("")){(null, "请输入进程标识符和进程名");}else{process = new ProcessPCB( ());(process);}("");ize() > 0){pcb = ().remove(0);ize() > 0){pcb = ().remove(0);ize() > 0){pcb = ().remove(0);;public class Bank {public static void main(String[] args) {int process;//定义进程数量int resource=3;//定义资源种类是3int[] available;//可利用的资源int[][] max,allocation,need;//分别是最大的需求数、已分配的资源、需求资源Scanner scanner=new Scanner;"请输入进程数>>");process=();"请输入可利用资源向量(已定义3个资源种类)>>");available=new int[resource];for (int i = 0; i < resource; i++) {available[i]=();}"请输入分配矩阵");allocation=new int[process][resource];for (int i = 0; i <process ; i++) {"请输入进程"+(i+1)+"已分配的资源数>>");for (int j = 0; j < resource; j++) {allocation[i][j]=();}}"请输入最大需求矩阵");max=new int[process][resource];for (int i = 0; i <process ; i++) {"请输入进程"+(i+1)+"最大需求的资源数>>");for (int j = 0; j < resource; j++) {max[i][j]=();}}need=new int[process][resource];for (int i = 0; i < process; i++) {for (int j = 0; j < resource; j++) {need[i][j]=max[i][j]-allocation[i][j];}}"To时刻的资源分配表");" 进程 max\t\tallocation\t need\t\tavailable");"P0 ");for (int i = 0; i <resource; i++) {" ");}" ");for (int i = 0; i <resource; i++) {" ");}" ");for (int i = 0; i <resource; i++) {" ");}" ");for (int i = 0; i <resource; i++) {" ");}for (int i = 1; i < process; i++) {"P"+i+" ");for (int j = 0; j < resource; j++) {" ");}" ");for (int j = 0; j < resource; j++) {" ");}" ");for (int j = 0; j < resource; j++) {" ");}}int[] work=new int[3];//定义一个数组work用来存放可利用的资源数目for (int i = 0; i < ; i++) {work[i]=available[i];//初始化work}boolean[] finish=new boolean[process];//定义标志finish,表示分配资源的置为true,没有非配的置为falsefor (int i = 0; i < process; i++) {finish[i]=false;//初始化数组finish}int[] array=new int[process];//定义一个数组保存安全序列int num=1;int count1=1;while(num<process){for (int i = 0; i < process; i++) {for (int j = 0; j < 3; j++) {if(finish[i]==false){if(need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[2]){for (int j2 = 0; j2 < resource; j2++) {work[j2]=work[j2]+allocation[i][j2];}finish[i]=true;array[count1-1]=i;count1++;}}}}num++;}int count=0;for (int i = 0; i < ; i++) {if(finish[i]==true){count++;}}if(count==process){"存在一个安全序列:");for (int i = 0; i < ; i++) {"P"+array[i]+" ");}}else{"系统处于不安全状态!");}boolean flag=true;while(flag){int[] req=new int[resource];"请输入您要请求资源的编号>>");int choose=();"请输入该进程的请求向量>>");for (int i = 0; i < resource; i++) {req[i]=();}if(req[0]<=need[choose][0]&&req[1]<=need[choose][1]&&req[2]<=need[choose][2]){ if(req[0]<=available[0]&&req[1]<=available[1]&&req[2]<=available[2]){for (int i = 0; i < resource; i++) {available[i]=available[i]-req[i];allocation[choose][i]=allocation[choose][i]+req[i];need[choose][i]=need[choose][i]-req[i];}int[] work1=new int[3];for (int i = 0; i < ; i++) {work1[i]=available[i];}boolean[] finish1=new boolean[process];for (int i = 0; i < process; i++) {finish1[i]=false;}int[] array1=new int[process];int num1=1;int count11=1;while(num1<process){for (int i = 0; i < process; i++) {for (int j = 0; j < 3; j++) {if(finish1[i]==false){if(need[i][0]<=work1[0]&&need[i][1]<=work1[1]&&need[i][2]<=work1[2]){for (int j2 = 0; j2 < resource; j2++) {work1[j2]=work1[j2]+allocation[i][j2];}finish1[i]=true;array1[count11-1]=i;count11++;}}}}num1++;}int count2=0;for (int i = 0; i < ; i++) {if(finish1[i]==true){count2++;}}if(count2==process){"存在一个安全序列:");for (int i = 0; i < ; i++) {"P"+array1[i]+" ");}}else{"系统处于不安全状态!");flag=false;}}else{"资源不够清等待!");}}//if结束else{"请求资源已超过所需资源!");}}//while结束}}运行效果截图:。

相关主题