操作系统作业调度算法Java-FCFS,SJF,HRN算法

package os.lesson2;

import java.util.ArrayList;
import java.util.List;

class JCB{
    
    
    private String name; // 作业名
    private char state; // 作业状态
    private int ts; // 提交时间
    private float _super; // 优先权
    private int tb; // 开始运行时间
    private int tc; // 完成时间
    private float ti; // 周转时间
    private float wi; // 带权周转时间
    private int ntime; // 作业所需运行时间
    private char[] resource; // 所需资源
    private JCB link;

    public JCB() {
    
    
    }

    public JCB(String name, int ts, int ntime, JCB link) {
    
    
        this.name = name;
        this.ts = ts;
        this.ntime = ntime;
        this.link = link;
    }

    public JCB(String name, char state, int ts, float _super, int tb, int tc, float ti, float wi, int ntime, char[] resource, JCB link) {
    
    
        this.name = name;
        this.state = state;
        this.ts = ts;
        this._super = _super;
        this.tb = tb;
        this.tc = tc;
        this.ti = ti;
        this.wi = wi;
        this.ntime = ntime;
        this.resource = resource;
        this.link = link;
    }
    public static void FCFS(JCB head){
    
    
        JCB p = head;
        int n = 0;
        while (p != null){
    
    
            p = p.link;
            n ++;
        }
        FCFS0(head, n);
    }

    /**
     * 先到先服务(FCFS)算法
     * 通过传入jcb作业进行作业调度的先到先服务算法运算
     * 可以格式化输出对应的结果分析
     * @param head 传入的作业jcb数据结构类
     * @return 返回作业的代权周转时间算数平均和
     */
    private static void FCFS0(JCB head, int n){
    
    
        if (head == null) return ;
        int now = head.ts, end = 0;
        float sum_ti = 0, sum_wi = 0;
        JCB p = head;
        while (p!=null){
    
    
            end = now + p.ntime;
            sum_ti = end - p.ts;
            sum_wi = sum_ti / p.ntime;
            p.tb = now;
            p.tc = end;
            p.ti = sum_ti;
            p.wi = sum_wi;
            p.state = 'f';
            now = end;
            p = p.link;
        }
        print(head);
    }

    public static void SJF(JCB head){
    
    
        JCB p = head;
        int n = 0;
        while (p != null){
    
    
            p = p.link;
            n ++;
        }
        SJF0(head, n);
    }
    /**
     * 短作业优先算法(SJF)
     * 根据已经到达的作业运行时间长短来决定是否运行此作业
     * @param head 作业的头指针
     * @param n 作业数
     */
    private static void SJF0(JCB head, int n){
    
    
        if(head == null) return;
        int now = head.ts, end = 0;
        float sum_ti = 0, sum_wi = 0;
        int pos = 0;
        while (pos < n){
    
    
            JCB p = head;
            List<JCB> queue = new ArrayList<>();
            // 找到当前的等待作业
            while (p!= null && p.ts <= now){
    
    
                if (p.state!='f') queue.add(p);
                p = p.link;
            }
            JCB min = queue.get(0);
            // 取出当前等待队列运行时间最短的等待作业
            for (int i = 1; i < queue.size(); i++) {
    
    
                if(queue.get(i).ntime < min.ntime){
    
    
                    min = queue.get(i);
                }
            }
            end = now + min.ntime;
            sum_ti = end - min.ts;
            sum_wi = sum_ti / min.ntime;
            min.tb = now;
            min.tc = end;
            min.ti = sum_ti;
            min.wi = sum_wi;
            min.state = 'f';
            now = end;
            pos ++;
        }
        print(head);
    }
    public static void HRN(JCB head){
    
    
        int n = 0;
        JCB p = head;
        while (p != null){
    
    
            n ++;
            p = p.link;
        }
        HRN0(head, n);
    }
    private static void HRN0(JCB head, int n){
    
    
        if (head == null) return;
        int now = head.ts, end = 0;
        float sum_ti = 0, sum_wi = 0;
        int pos = 0;
        while (pos < n){
    
    
            JCB p = head;
            List<JCB> queue = new ArrayList<>();
            // 找到等待作业的队列
            while (p != null && p.ts <= now) {
    
    
                if (p.state != 'f') queue.add(p);
                p = p.link;
            }
            JCB max = queue.get(0);
            float max_val = (float) (now - max.ts) / max.ntime;
            // 找到等待作业响应比最大的作业
            for (int i = 1; i < queue.size(); i++){
    
    
                float temp_val = (float)(now - queue.get(i).ts) / queue.get(i).ntime;
                if (temp_val > max_val){
    
    
                    max = queue.get(i);
                    max_val = temp_val;
                }
            }
            // 执行作业
            end = now + max.ntime;
            sum_ti = end - max.ts;
            sum_wi = sum_ti / max.ntime;
            max.tb = now;
            max.tc = end;
            max.ti = sum_ti;
            max.wi = sum_wi;
            max.state = 'f';
            now = end;
            pos ++;
        }
        print(head);
    }
    public static void print(JCB head){
    
    
        if (head == null) return;
        System.out.println("作业\t\t运行时间\t提交时间\t开始时间\t结束时间\t周转时间\t带权周转时间");
        JCB p = head;
        while (p != null) {
    
    
            System.out.println(p.name + "\t\t" + p.ntime + "\t\t" + p.ts + "\t\t" + p.tb + "\t\t" + p.tc + "\t\t" + p.ti + "\t\t" + p.wi);
            p = p.link;
        }
    }
    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public char getState() {
    
    
        return state;
    }

    public void setState(char state) {
    
    
        this.state = state;
    }

    public int getTs() {
    
    
        return ts;
    }

    public void setTs(int ts) {
    
    
        this.ts = ts;
    }

    public float get_super() {
    
    
        return _super;
    }

    public void set_super(float _super) {
    
    
        this._super = _super;
    }

    public int getTb() {
    
    
        return tb;
    }

    public void setTb(int tb) {
    
    
        this.tb = tb;
    }

    public int getTc() {
    
    
        return tc;
    }

    public void setTc(int tc) {
    
    
        this.tc = tc;
    }

    public float getTi() {
    
    
        return ti;
    }

    public void setTi(float ti) {
    
    
        this.ti = ti;
    }

    public float getWi() {
    
    
        return wi;
    }

    public void setWi(float wi) {
    
    
        this.wi = wi;
    }

    public int getNtime() {
    
    
        return ntime;
    }

    public void setNtime(int ntime) {
    
    
        this.ntime = ntime;
    }

    public char[] getResource() {
    
    
        return resource;
    }

    public void setResource(char[] resource) {
    
    
        this.resource = resource;
    }

    public JCB getLink() {
    
    
        return link;
    }

    public void setLink(JCB link) {
    
    
        this.link = link;
    }
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        JCB jcb5 = new JCB("5", 35, 15, null);
        JCB jcb4 = new JCB("4", 30, 20, jcb5);
        JCB jcb3 = new JCB("3", 20, 20, jcb4);
        JCB jcb2 = new JCB("2", 20, 10, jcb3);
        JCB head = new JCB("1", 0, 25, jcb2);
//        JCB.FCFS(head, 5);
//        JCB.SJF(head);
        JCB.HRN(head);
//        JCB.sjf(jcb1, jcb2, jcb3, jcb4, jcb5);
    }
}

猜你喜欢

转载自blog.csdn.net/AlexanderRon/article/details/117135783
今日推荐