Reputation: 21
Experts, this is the program I made for First Come First Serve Scheduling in C language.
For the inputs like -
4
0 0 0 0
1 2 3 4
and inputs like -
5
0 1 2 3 4
4 3 1 2 5
My program is giving Segmentation fault (Core dumped) when I tried running it on my Ubuntu but it worked fine on GeeksForGeeks IDE.
And for the inputs like -
6
4 3 2 1 2 3
4 3 2 3 4 5
My program is working fine on my Ubuntu as well as on GeeksForGeeks IDE.
#include<stdio.h>
void main(){
int n,i;
printf("\nEnter the number of jobs:\n");
scanf("%d",&n);
int at[n],bt[n],at_copy[n];
printf("\nEnter the arrival time:\n");
for(i=0;i<n;i++){
scanf("%d",&at[i]);
at_copy[i]=at[i];
}
printf("\nEnter the burst time:\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
int priority[n],min=at_copy[0],k,j;
for(j=0;j<n;j++){
min=at_copy[0];
for(i=0;i<n;i++){
if(at_copy[i]<min){
min=at_copy[i];
k=i;
}
}
at_copy[k]=999;
priority[j]=k;
}
int ct[n],wt[n],tat[n],t=0;
for(i=0;i<n;i++){
if(at[i]<t)
k=0;
else
k=at[i];
t+=bt[i]+k;
ct[i]=t;
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
}
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for(i=0;i<n;i++){
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
}
In the output, I am expecting a table like structure displaying the arrival time, burst time, completion time, burst time, turnaround time and waiting time of all the processes.
Upvotes: 1
Views: 69
Reputation: 18420
Here:
int priority[n],min=at_copy[0],k,j;
for(j=0;j<n;j++){
min=at_copy[0];
for(i=0;i<n;i++){
if(at_copy[i]<min){
min=at_copy[i];
k=i;
}
}
at_copy[k]=999;
...
Imagine what happens, when at_copy[0]
is the minimal value. Then, the condition atcopy[i]<min
is never true and k remains uninitialized resulting in an out-of-bounds access here:
at_copy[k]=999;
You have to initialize k with 0
, since you assume in the first iteration, that at_copy[0]
is the minimum.
int priority[n],min=at_copy[0],k=0,j;
With Geeks-IDE, you might have been "lucky" and k had the value 0 without being initialized.
Upvotes: 2