湖北师范大学——操作系统实训(java版)

湖北师范大学——操作系统实训(java版)

看了蛮多答案发现基本都是用c写的,虽然c确实有时间复杂度上的优势,但是用java实现也并非不可能,除了第三题其他都可以通过,此答案全部为原创并非最优解,希望有优化可以评论指出

题目1:进程调度1—静态非剥夺式优先级调度计算平均作业周转时间

问题描述:要求输入3个进程的信息,假设这些进程均是在0时刻同时到达,若进程调度采用非剥夺式静态优先级(优先数数值大的表示优先级比较高;如果遇到优先级一样,按照输入顺序执行。),计算并输出平均作业周转时间。

输入格式:程序要求输入3行,以回车符号作为分隔,每行有3个数据,以空格作为分隔。首先输入一个字符串(长度小于等于10),为进程名,第2个数据类型为整型,表示进程的优先数,第3个数据类型为整型,表示进程的运行时间。

输出格式:输出结果为一个浮点数,保留到小数点后一位,为系统的平均作业周转时间。

样例输入1:

P1 1 1

P2 2 2

P3 3 3

样例输出1:

4.7

import java.util.Scanner;
//p1 10 10
//p2 100 100
//p3 100 100
//ProcessA 3 8
//ProcessB 1 5
//ProcessC 2 9
//15.7
public class First {
   
    public static void main(String[] args) {
   
        Scanner scanner = new Scanner(System.in);
        String line1=scanner.nextLine();
        String line2=scanner.nextLine();
        String line3 = scanner.nextLine();
        String[] s1 = line1.split(" ");
        String[] s2 = line2.split(" ");
        String[] s3 = line3.split(" ");
        String[][] process={
   s1,s2,s3};
        sortByRank(process);
        int end1=Integer.parseInt(process[0][2]);
        int end2=Integer.parseInt(process[1][2])+end1;
        int end3=Integer.parseInt(process[2][2])+end2;
        float result=(end1+end2+end3)/3f;
        String remain1=String.format("%.1f",result);
        System.out.println(remain1);
    }
    public static void sortByRank(String[][] process){
   
        String[] temp;
        for (int i = 0; i < process.length - 1; i++) {
   
            for (int j = 0; j < process.length-1-i; j++) {
   
                if (Integer.parseInt(process[j][1])<Integer.parseInt(process[j+1][1])){
   
                    temp=process[j];
                    process[j]=process[j+1];
                    process[j+1]=temp;
                }
                //优先级相等就按照先后顺序排
                int i1 = process[j][0].compareTo(process[j + 1][0]);
                if ((Integer.parseInt(process[j][2])==Integer.parseInt(process[j+1][2]))&&(i1 >0)){
   
                    temp=process[j];
                    process[j]=process[j+1];
                    process[j+1]=temp;
                }
            }
        }
    }
}

题目2:进程调度2–最高响应比优先计算每个作业的周转时间

问题描述:要求输入3个进程的信息,按照最高响应比优先的调度算法计算并输出每个进程的周转时间。(若两个进程的响应比相同,则优先选择先进入的进程。若两个进程的响应比相同,而且进入时刻也相同,则按照输入的顺序执行,如:P4和P6的响应比相同且进入时刻也相同,如P4先输入则选择P4先执行)

输入格式:程序要求输入3行,以回车符号作为分隔,每行有3个数据,以空格作为分隔。首先输入一个字符串(长度小于等于10),为进程名,第2个数据类型为整型,表示进程的进入时刻,第3个数据类型为整型,表示进程的运行时间。

输出格式:输出三个整数之间,整数之间用空格作为分隔,为每个进程的周转时间。

样例输入1:

P1 1 1

P2 2 2

P3 3 3

样例输出1:

1 2 4

import java.util.Scanner;
//周转时间=完成时间-提交时间(等待时间+处理时间)
//Process1 2 5
//Process2 2 2
//Process3 3 2
//9 2 3
public class Second {
   
    public static void main(String[] args) {
   
        Scanner scanner = new Scanner(System.in);
        String line1 = scanner.nextLine();
        String line2 = scanner.nextLine();
        String line3 = scanner.nextLine();
        String[] s1 = line1.split(" ");
        String[] s2 = line2.split(" ");//进入时刻
        String[] s3 = line3.split(" ");//运行时间
        String[][] process = {
   s1, s2, s3};
        sortByIn(process);
        int time = 0;//等待时间=进入时刻-time
        if (Integer.parseInt(process[0][1]) > time) {
   
            time = Integer.parseInt(process[0][1]);
        }
        //前两个同时进入线程,比较响应比
        if (Integer.parseInt(process[0][1])==Integer.parseInt(process[1][1])){
   
            if (Integer.parseInt(process[0][2])>Integer.parseInt(process[1][2])) {
   
                String[] temp = process[0];
                process[0] = process[1];
                process[1] = temp;
            }
        }
        //前三个都同时到达
        if (Integer.parseInt(process[0][1])==Integer.parseInt(process[1][1])&&Integer.parseInt(process[2][1])==Integer.parseInt(process[1][1])){
   
            sortByRemainTime(process);
        }
        time = time + Integer.parseInt(process[0][2]);
        process[0][2]=String.valueOf(time-Integer.parseInt(process[0][1]));
        if (time > Integer.parseInt(process[1][1]) && time > Integer.parseInt(process[2][1])) {
   //最早到到进程完成之后,剩余的进程都到达
            int waitTime1 =time - Integer.parseInt(process[1][1])  ;
            int waitTime2 = time - Integer.parseInt(process[2][1]);
            double ratio1 = waitTime1 / Double.parseDouble(process[1][2]);
            double ratio2 = waitTime2 / Double.parseDouble(process[2][2]);
            if (ratio1 < ratio2) {
   //后一个的响应比要大,就交换顺序
                String[] temp = process[1];
                process[1] = process[2];
                process[2] = temp;
            }
        } else if (time < Integer.parseInt(process[1][1]) && time < Integer.parseInt(process[2][1])) {
   //都没到
            time = Integer.parseInt(process[1][1]);
        }
        time = time + Integer.parseInt(process[1][2]);
        process[1][2]=String.valueOf(time-Integer.parseInt(process[1][1]));
        if (time < Integer.parseInt(process[2][1])) {
   
            time = Integer.parseInt(process[2][1]);
        }
        time = time + Integer.parseInt(process[2][2]);
        process[2][2]=String.valueOf(time-Integer.parseInt(process[2][1]));
        sortByName(process);
        for (String[] strings : process) {
   
            System.out.print(strings[2]+" ");
        }
    }
    public static void sortByIn(String[][] process){
   
        String[] temp;
        for (int i = 0; i < process.length - 1; i++) {
   
            for (int j = 0; j < process.length-1-i; j++) {
   
                if (Integer.parseInt(process[j][1])>Integer.parseInt(process[j
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值