问题描述
我正在尝试从 input.txt
文件读取输入的最短作业优先调度(批处理,非抢占式),但它抛出 segmentation fault(core duped)
这是我的代码:
#include <stdio.h>
int main()
{
FILE *dir;
char string[100];
int pId,arrival,cpuBurst,priority,i,j,t,nJobs;
int pid[30],pt[30],burstTime[30],arrTime[30],waitTime[30],turnAroundTime[30],comp[30];
float avgWaitTime = 0,avgTurnAroundTime = 0,resTime = 0,time = 0,throughput = 0;
int countValue[7],responseTime[30];
dir = fopen("input.txt","r");
fgets(string,100,dir);
j = 0;
while (fscanf(dir,"%d %d %d %d",&pId,&arrival,&cpuBurst,&priority) != EOF)
{
arrTime[j] = arrival;
pid[j] = pId;
burstTime[j] = cpuBurst;
pt[j] = priority;
j += 1;
}
nJobs = j;
for (i = 0; i < nJobs; i++)
{
for (j = i; j < nJobs; j++)
{
if (arrTime[i] > arrTime[j])
{
t = arrTime[i];
arrTime[i] = arrTime[j];
arrTime[j] = t;
t = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = t;
t = pid[j];
pid[j] = pid[i];
pid[i] = t;
}
}
}
comp[0] = arrTime[0] + burstTime[0];
int numCount = 0;
for (i = 0; i < nJobs; i++)
{
int count = 0;
for (j = i + 1; j < nJobs; j++)
{
if (comp[i] > arrTime[j])
{
countValue[count] = j;
count++;
}
}
for (int a = 0; a < nJobs; a++)
{
int resVal = countValue[a];
int resValSec = countValue[a + 1];
if (resVal != 0 && resValSec != 0)
{
if (burstTime[resVal] > burstTime[resValSec])
{
t = arrTime[resVal];
arrTime[resVal] = arrTime[resValSec];
arrTime[resValSec] = t;
t = burstTime[resVal];
burstTime[resVal] = burstTime[resValSec];
burstTime[resValSec] = t;
t = pid[resValSec];
pid[resValSec] = pid[resVal];
pid[resVal] = t;
}
comp[numCount + 1] = comp[numCount] + burstTime[numCount + 1];
++numCount;
}
}
turnAroundTime[i] = comp[i] - arrTime[i];
avgTurnAroundTime = avgTurnAroundTime + turnAroundTime[i];
waitTime[i] = turnAroundTime[i] - burstTime[i];
avgWaitTime = avgWaitTime + waitTime[i];
resTime = avgWaitTime;
}
printf("Process \t arrival \t completion");
for (i = 0; i < nJobs; i++)
{
printf("\n%d\t\t%d\t\t%d",pid[i],arrTime[i],comp[i]);
}
avgTurnAroundTime = avgTurnAroundTime / nJobs;
resTime = resTime / nJobs;
time = (resTime / avgTurnAroundTime);
throughput = time / nJobs;
printf("\nRun Stats");
printf("\nThroughput=%.2f\n",throughput);
printf("Average Response Time=%.2f\n",resTime);
printf("Average Turn Around Time=%.2f\n",avgTurnAroundTime);
}
input.txt 文件包含:
PID ARRIVAL BURST PRIORITY
100 0 10 1
101 6 10 1
102 8 4 1
103 12 20 1
104 19 15 1
105 30 5 1
106 35 10 1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)