I was writing this code for FCFS scheduling. But in Linux this is giving
“Segmentation error”. How to correct such error?
#include<stdio.h>
#include<string.h>
float fxn();
int main()
{
float avgwt;
int n,i,at[10],bt[10];
printf("\n\nEnter the number of processes:");
scanf("%d",&n);
printf("\n\n BURST TIME and ARRIVAL TIME of thr process");
for(i=0;i<n;i++)
{
printf("\n ARRIVAL TIME :");
scanf("%d",&at[i]);
printf(" BURST TIME : ");
scanf("%d",&bt[i]);
}
avgwt=fxn(at,bt,n);
printf("\n\nAverage waiting time=%f",avgwt);
return 0;
}
float fxn( int at[], int bt[], int n)
{
int i,j,t,wt[n],sum,tt[n],q;
float avgwt;
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
}
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
sum=sum+(wt[i+1]-at[i]);
}
avgwt=sum/n;
return avgwt;
}
Another Question —- I will be editing this code for making a system call in linux.
Will the passing of arrays will work if a c program will takes input from user and passes it to the kernel for further calculations?
(for example – here main() function for taking input and fxn() function into the kernel
You got a buffer overflow below
it should be
i < n -1also you need to intializei=0in functionfxn