Код Назначение
Код должен симулировать алгоритмы планирования ЦП. В настоящее время написаны только FCFS (First First First Served) и SJF (Shortest Job First)
Задача
При выполнении кода я получаю следующую ошибку при использовании FCFS 'pathway'
*** Error in `./test2': double free or corruption (!prev): 0x000055f54ecc7830 ***
Из того, что я нашел в Интернете, это связано с проблемами переполнения памяти, хотя при запуске этого на моем персональном компьютере я не получаю ошибок для пути. Я проверил свои циклы и замедления массива, но не могу найти проблему.
Я полагаю, что моя проблема может касаться следующих циклов
//compare values in at to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
for(i=0; i<processes; i++)
{
for(j=0; j<processes; j++)
{
if(at[i]<at[j]) //if the value of i is smaller than j (basically gets the smallest value in array)
{
temp=at[i]; //temp int equals arrival time of i
at[i]=at[j]; //arrival time i changes to value of arrival time j
at[j]=temp; //arrival time j becomes original value of arrival time i (basically switching the values of i and j)
temp=bt[i]; //temp becomes value of burst time i
bt[i]=bt[j]; //burst time i becomes values of burst time j
bt[j]=temp; //burst time j becomes the original value of burst time i (basically switching the values of i and j)
temp=pid[i]; //t changes to value of pid i
pid[i]=pid[j]; //pid i becomes value of pid j
pid[j]=temp; //pid j becomes value of t (basically switching the values of i and j)
}
}
}
Основной набор данных, который я тестировал, - это набор данных 3, который содержит следующие данные
PID AT BT
0 3 4
1 1 5
2 2 20
3 0 25
4 6 14
5 8 6
Все файлы, относящиеся к этому коду, можно найти по следующей ссылке (извинения, я знаю, ссылки не являются предпочтительными, но это самый простой способ поделиться наборами данных с правильным форматированием)
Полный код и местоположение файла
Полный код
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char filename[100];
char *buffer = NULL;
int schedtoken;
char entries[10];
int fcfs()
{
int pid[10],at[10],bt[10],st[10],ft[10],tat[10],wt[10],i=0,j=0,processes=6,temp,n1,n2,n3;
int totwt=0,tottat=0;
char c1,c2,c3,fcfsselection;
printf("\n\n\nPlease select the dataset you would like to use\n\n");
printf(" 1. Dataset1\n 2. Dataset2\n 3. Dataset3\n 4. Quit\n\nSelection:\n");
scanf(" %c",&fcfsselection);
// Get data set user wants and amend filename based on selection
switch(fcfsselection)
{
case '1':
printf("\nYou have selected Dataset1\n");
strcpy(filename, "datasets/dataset1.txt");
break;
case '2':
printf("\nYou have selected Dataset2\n");
strcpy(filename, "datasets/dataset2.txt");
break;
case '3':
printf("\nYou have selected Dataset3\n");
strcpy(filename, "datasets/dataset3.txt");
break;
case '4':
printf("\nThank you for using this tool!");
break;
default:
printf("\nERROR!: Incorrect selection - Returning to Menu\n");
fcfs();
}
//Import dataset file, store the first line of char's (crashes if only checking for ints) and the rest of the ints
FILE *fp;
fp=fopen(filename,"r");
if (fp == NULL)
{
printf("Cannot open file at %s, try again.", filename);
fcfs();
}
else
{
fscanf(fp,"%s%s%s",&c1,&c2,&c3);
while(fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
{
pid[i]=n1;
at[i]=n2;
bt[i]=n3;
i++;
}
}
fclose(fp);
//compare values in arr time to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
for(i=0; i<processes; i++)
{
for(j=0; j<processes; j++)
{
if(at[i]<at[j]) //if the value of i is smaller than j (basically gets the smallest value in array)
{
temp=at[i]; //temp int equals arrival time of i
at[i]=at[j]; //arrival time i changes to value of arrival time j
at[j]=temp; //arrival time j becomes original value of arrival time i (basically switching the values of i and j)
temp=bt[i]; //temp becomes value of burst time i
bt[i]=bt[j]; //burst time i becomes values of burst time j
bt[j]=temp; //burst time j becomes the original value of burst time i (basically switching the values of i and j)
temp=pid[i]; //t changes to value of pid i
pid[i]=pid[j]; //pid i becomes value of pid j
pid[j]=temp; //pid j becomes value of t (basically switching the values of i and j)
}
}
}
//complete calculations
for(i=0; i<processes; i++)
{
if(i==0)
st[i]=at[i]; //if i equals 0 (basically the beggining of the sim) then the start time equals the arrival time of the first entry (so 0)
else
st[i]=ft[i-1]; //otherwise the start value equals the finish value of the last entry run -1
wt[i]=st[i]-at[i]; //wait time equals the start time of the process minus the arrival time
ft[i]=st[i]+bt[i]; //finish time equals start time plus run time
tat[i]=ft[i]-at[i]; // turn around time equals finish time minus arrival time
}
tottat=0;
//print results
printf("\nPID\t AT\t BT\t WT\t ST\t TAT\t CT");
for(i=0; i<processes; i++)
{
printf("\n%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pid[i],at[i],bt[i],wt[i],st[i],tat[i],ft[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\n\nAverage Waiting Time:%f",(float)totwt/processes);
printf("\nAverage Turn Around Time:%f",(float)tottat/processes);
fclose(fp);
//Open new file to print output
FILE *f = fopen("datasets/output.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
//Print output to file
fprintf(f, "PID\t AT\t BT\t WT\t ST\t TAT\t CT");
for(i=0; i<processes; i++)
{
fprintf(f,"\n%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pid[i],at[i],bt[i],wt[i],st[i],tat[i],ft[i]);
}
fprintf(f,"\n\nAverage Waiting Time:%f",(float)totwt/processes);
fprintf(f,"\nAverage Turn Around Time:%f",(float)tottat/processes);
printf("\n\nThe results for the dataset simulated are stored in: datasets/output.txt");
fclose(f);
return 0;
}
char* getfile(char *filename)
{
int string_size, read_size;
FILE *file = fopen(filename, "r");
if (file)
{
// Seek the last byte of the file
fseek(file, 0, SEEK_END);
// Offset from the first to the last byte, or in other words, filesize
string_size = ftell(file);
// go back to the start of the file
rewind(file);
// Allocate a string that can hold it all
buffer = (char*) malloc(sizeof(char) * (string_size + 1) );
// Read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, file);
// fread doesn't set it so put a \0 in the last position
// and buffer is now officially a string
buffer[string_size] = '\0';
if (string_size != read_size)
{
// Something went wrong, throw away the memory and set
// the buffer to NULL
free(buffer);
buffer = NULL;
}
// Always remember to close the file.
fclose(file);
}
// printf("%s",buffer);
return buffer;
}
void schedintro(int schedtoken)
{
//take the value of schedtoken, change the value of filename to relavent file path and use getfile to open and then print the file. Call relavent scheduling function to do calculations
if(schedtoken==1)
{
strcpy(filename, "headers/fcfsheader.txt");
getfile(filename);
printf("%s",buffer);
fcfs();
}
else if(schedtoken==2)
{
strcpy(filename, "headers/sjfheader.txt");
getfile(filename);
printf("%s",buffer);
}
else if(schedtoken==3)
{
strcpy(filename, "headers/rrheader.txt");
getfile(filename);
printf("%s",buffer);
}
}
int schedselect()
{
//function to display what algorithms can be selected. User input
// obtained based on these options and user filtered based on switch
// cases to relevant function path
char selection;
// print users options and take their input for switch
printf("%s",buffer);
printf("\n\n\nPlease select the Scheduling Algorithm you would like to use\n\n");
printf(" 1. First Come First Served (FCFS)\n 2. Shortest Job First (SJF)\n 3. Round Robin (RR)\n 4. Quit\n\nSelection:\n");
scanf("%c",&selection);
// direct user to specific algorithm function path
switch(selection)
{
case '1':
//printf("\nYou have selected First Come First Served (FCFS)\n");
schedintro(schedtoken=1);
break;
case '2':
//printf("\nYou have selected Shortest Job First (SJF)\n");
schedintro(schedtoken=2);
break;
case '3':
//printf("\nYou have selected Round Robin (RR)\n");
schedintro(schedtoken=3);
break;
case '4':
printf("\nThank you for using this tool!");
break;
default:
printf("\nERROR!: Incorrect selection - Returning to Menu\n");
schedselect();
}
//printf("%d", schedoption);
return schedtoken;
}
int main()
{
strcpy(filename, "headers/introheader.txt");
getfile(filename);
schedselect();
return 0;
}