作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
更新:解决了我关于排序的第一个问题,但现在我无法弄清楚如何显示具有空闲时间的最早截止日期优先算法的正确图表。
到目前为止,这是我的代码:
import java.util.*;
class deadlineprototype
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter no. of processes : ");
int n=sc.nextInt();
int job[]=new int[n+1];
int burst[]=new int[n+1];
int newburst[]=new int[n+1];
int arrival[]=new int[n+1];
int deadline[]=new int[n+1];
int wt[]=new int[n+1];
int turn[]=new int[n+1];
int tot_turn=0;
int tot_wait=0;
float avg_turn=0;
float avg_wait=0;
int j;
for(int m=1;m<=n;m++)
{
arrival[m]=m;
}
for(int m=1;m<=n;m++)
{
job[m]=m;
}
for(int m=1;m<=n;m++)
{
System.out.println("enter arrival time, burst time and deadline of process "+(m)+"(0 for none):");
arrival[m]=sc.nextInt();
burst[m]=sc.nextInt();
deadline[m]=sc.nextInt();
if (deadline[m]==0){
deadline[m]=1000;
}
}
int temp;
for(int i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(arrival[j+1] == arrival[j])
{
if(deadline[j+1] < deadline[j])
{
temp=deadline[j+1];
deadline[j+1]=deadline[j];
deadline[j]=temp;
temp=job[j+1];
job[j+1]=job[j];
job[j]=temp;
temp=burst[j+1];
burst[j+1]=burst[j];
burst[j]=temp;
temp=arrival[j+1];
arrival[j+1]=arrival[j];
arrival[j]=temp;
}
}
}
}
turn[1]=burst[1];
for(int i=2;i<=n;i++)
{
turn[i]=burst[i]+turn[i-1];
wt[i]=turn[i]-burst[i];
}
for(int i=1;i<=n;i++)
{
tot_turn+=(wt[i]+burst[i])-arrival[i];
avg_turn=(float)tot_turn/n;
tot_wait+=wt[i]-arrival[i];
avg_wait=(float)tot_wait/n;
}
System.out.println("----------Earliest Deadline Scheduling Diagram----------");
for(int m=1;m<=n;m++)
{
if(deadline[m]==1000){
deadline[m]=0;
}
if(wt[m]==0){
System.out.println("0"+wt[m]+" _____");
}
else{
System.out.println(wt[m]+" _____");
}
System.out.println(" | |");
System.out.println(" |job "+job[m]+"|");
System.out.println(" |_____|");
try
{
//newburst[m]=(burst[m]*1000);
Thread.sleep(1000);
}catch (InterruptedException ie)
{
System.out.println(ie.getMessage());
}
}
System.out.println((wt[wt.length-1]+burst[burst.length-1]));
如果我输入 2 个没有空闲的进程,那么它将显示正确的输出:
enter no. of processes :
2
enter arrival time, burst time and deadline of process 1(0 for none):
0 17 0
enter arrival time, burst time and deadline of process 2(0 for none):
0 13 10
----------Earliest Deadline Scheduling Diagram----------
00 _____
| |
|job 2|
|_____|
13 _____
| |
|job 1|
|_____|
30
但是如果它有空闲时间那么它会输出:
enter no. of processes(5-10):
2
enter arrival time, burst time and deadline of process 1(0 for none):
0 5 0
enter arrival time, burst time and deadline of process 2(0 for none):
10 10 10
----------Earliest Deadline Scheduling Diagram----------
00 _____
| |
|job 1|
|_____|
5 _____
| |
|job 2|
|_____|
15
最佳答案
首先根据到达时间
对Processes进行如下排序,
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(arrival[j+1] < arrival[j])
{
temp=deadline[j+1];
deadline[j+1]=deadline[j];
deadline[j]=temp;
temp=job[j+1];
job[j+1]=job[j];
job[j]=temp;
temp=burst[j+1];
burst[j+1]=burst[j];
burst[j]=temp;
temp=arrival[j+1];
arrival[j+1]=arrival[j];
arrival[j]=temp;
}
}
}
然后,如果进程的到达时间
相等,则根据截止时间
对它们进行排序,如下所示,
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(arrival[j+1] == arrival[j])
{
if(deadline[j+1] < deadline[j])
{
temp=deadline[j+1];
deadline[j+1]=deadline[j];
deadline[j]=temp;
temp=job[j+1];
job[j+1]=job[j];
job[j]=temp;
temp=burst[j+1];
burst[j+1]=burst[j];
burst[j]=temp;
temp=arrival[j+1];
arrival[j+1]=arrival[j];
arrival[j]=temp;
}
}
}
}
关于java - 最早期限cpu调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16620762/
我有一个 MySQL 表(其中包括)以下列: [name] [member_since_date] [member_until_date] 当某人的成员(member)资格结束时,[member_un
我是一名优秀的程序员,十分优秀!