コード目的
このコードは、CPUスケジューリングアルゴリズムをシミュレートすることになっています。現在、FCFS(先入れ先出し)とSJF(最短ジョブ優先が作成されています)のみ
問題
コードを実行すると、FCFS'pathway 'を使用すると次のエラーが発生します
*** Error in `./test2': double free or corruption (!prev): 0x000055f54ecc7830 ***
私がオンラインで見つけたものから、これはメモリオーバーフローの問題に関連していますが、これを私のパーソナルPCで実行すると、パスウェイのエラーは発生しません。ループとアレイの減速を確認しましたが、問題を見つけることができません。
私の問題は次のforループに関連している可能性があると思います
//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です。
BTでのPID
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;
}
ではFCFS()あなたは2回同じファイルを閉じます
fclose(fp); //compare values in arr time to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
そして
fclose(fp);
//新しいファイルを開いて出力を印刷します
2番目のfcloseを削除します
2番目のfcloseがないと、-Dataset3_のvalgrindによって何も通知されません。
valgrind ./a.out
==22836== Memcheck, a memory error detector
==22836== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==22836== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==22836== Command: ./a.out
==22836==
(null)
Please select the Scheduling Algorithm you would like to use
1. First Come First Served (FCFS)
2. Shortest Job First (SJF)
3. Round Robin (RR)
4. Quit
Selection:
1
(null)
Please select the dataset you would like to use
1. Dataset1
2. Dataset2
3. Dataset3
4. Quit
Selection:
3
You have selected Dataset3
PID AT BT WT ST TAT CT
3 0 25 0 0 25 25
1 1 5 24 25 29 30
2 2 20 28 30 48 50
0 3 4 47 50 51 54
4 6 14 48 54 62 68
5 8 6 60 68 66 74
Average Waiting Time:34.500000
Average Turn Around Time:46.833332
The results for the dataset simulated are stored in: datasets/output.txt==22836==
==22836== HEAP SUMMARY:
==22836== in use at exit: 0 bytes in 0 blocks
==22836== total heap usage: 4 allocs, 4 frees, 2,272 bytes allocated
==22836==
==22836== All heap blocks were freed -- no leaks are possible
==22836==
==22836== For counts of detected and suppressed errors, rerun with: -v
==22836== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加