Java SJF抢占式CPU调度

时间:2018-12-06 03:53:32

标签: java

如何使用以下代码使用sjf抢先式调度的applet显示甘特图?如何执行循环以打印applet中的每个过程以生成甘特图?如果cpu处于空闲状态,则在甘特图中生成一个空闲矩形。我知道如何在纸上制作甘特图,但是在Java中却很难。

有人可以帮我吗?谢谢。

import java.util.*;

public class SRTF {
public static void main (String args[])
{
    Scanner sc=new Scanner(System.in);
    System.out.println ("enter no of process:");
    int n= sc.nextInt();
    int pid[] = new int[n]; // it takes pid of process
    int at[] = new int[n]; // at means arrival time
    int bt[] = new int[n]; // bt means burst time
    int ct[] = new int[n]; // ct means complete time
    int ta[] = new int[n];// ta means turn around time
    int wt[] = new int[n];  // wt means waiting time
    int f[] = new int[n];  // f means it is flag it checks process is completed or not
    int k[]= new int[n];   // it is also stores brust time
    int i, st=0, tot=0;
    float avgwt=0, avgta=0;

    for (i=0;i<n;i++)
    {
        pid[i]= i+1;
        System.out.println ("enter process " +(i+1)+ " arrival time:");
        at[i]= sc.nextInt();
        System.out.println("enter process " +(i+1)+ " burst time:");
        bt[i]= sc.nextInt();
        k[i]= bt[i];
        f[i]= 0;
    }

    while(true){
        int min=99,c=n;
        if (tot==n)
            break;

        for ( i=0;i<n;i++)
        {
            if ((at[i]<=st) && (f[i]==0) && (bt[i]<min))
            {   
                min=bt[i];
                c=i;
            }
        }

        if (c==n)
            st++;
        else
        {
            bt[c]--;
            st++;
            if (bt[c]==0)
            {
                ct[c]= st;
                f[c]=1;
                tot++;
            }
        }
    }

    for(i=0;i<n;i++)
    {
        ta[i] = ct[i] - at[i];
        wt[i] = ta[i] - k[i];
        avgwt+= wt[i];
        avgta+= ta[i];
    }

    System.out.println("pid  arrival  burst  complete turn waiting");
    for(i=0;i<n;i++)
    {
        System.out.println(pid[i] +"\t"+ at[i]+"\t"+ k[i] +"\t"+ ct[i] +"\t"+ ta[i] +"\t"+ wt[i]);
    }

    System.out.println("\naverage tat is "+ (float)(avgta/n));
    System.out.println("average wt is "+ (float)(avgwt/n));
    sc.close();
}

}

0 个答案:

没有答案