+ 2
FCFS CPU scheduling in C programming.
Calculate Completion time, Turn around time TAT, waiting time WT, avg TAT, avg WT ( with arrival time).
10 ответов
+ 2
This is the correct code for fcfs
#include <stdio.h>
int main()
{
int n;
printf("Enter number of jobs to schedule - \n");
scanf("%d",&n);
int burst[n],arrival[n],index[n];
int i,tot=0;
printf("Enter the burst time of the jobs - \n");
for(i=0;i<n;i++)
{
index[i]=i+1;
scanf("%d",&burst[i]);
}
printf("Enter the arrival time of the jobs - \n");
for(i=0;i<n;i++)
{
scanf("%d",&arrival[i]);
}
int j;
for(i=0;i<n;i++)
{
int min=arrival[i];
int pos=i;
for(j=i+1;j<n;j++)
{
if(arrival[j]<min)
{
min=arrival[j];
pos=j;
}
}
int temp=arrival[i];
arrival[i]=min;
arrival[pos]=temp;
temp=burst[i];
burst[i]=burst[pos];
burst[pos]=temp;
temp=index[i];
index[i]=index[pos];
index[pos]=temp;
}
int time=0;
double avgtat=0;
for(i=0;i<n;i++)
{
while(time<arrival[i])
time++;
time+=burst[i];
avgtat+=(time-arrival[i]);
}
avgtat/=n;
printf("The jobs will be processed in this order - ");
for(i=0;i<n;i++)
printf("%d\t",index[i]);
printf("\nThe total time taken to complete all the jobs will be - %d",time);
printf("\nAverage Turnaround Time is - %lf",avgtat);
return 0;
}
+ 1
This is without arrival time. I want to take arrival time input and sort it 1st then perform the fcfs.
+ 1
When there is a table for fcfs, the arrival time of process 1 is not necessarily 1, so arrival time matters.
+ 1
https://www.gatevidyalay.com/first-come-first-serve-cpu-scheduling/
Solve problem 1 and enter the process in sequence ( from process 1 to last).
+ 1
I'm still puzzled. Doesn't its position in the queue establish the order of processing?
+ 1
We have to arrange it manually after taking input from user. Visit the site and try to solve problem 1.
+ 1
Solved it? I am stuck in the sorting section of the code. I have to sort process according to arrival time and then the burst time then I will do the rest.
+ 1
Find the error in this
#include <stdio.h>
struct pcb
{
int pid, arrival, burst, turnaround;
};
void pline(int x);
void main()
{
int i, num, j;
float avg = 0.0, sum = 0.0;
struct pcb p[10], temp;
printf("\nEnter the number of processes:\t");
scanf("%d", &num);
for (i=0;i<num;i++)
{
printf("\nEnter the arrival time and burst time of the process %d:\t", i+1);
scanf("%d %d", &p[i].arrival,&p[i].burst);
p[i].pid = i+1;
}
for (i=0;i<num-1;i++)
{
for (j=0;j<num-i-1;j++)
{
if (p[j].arrival > p[j+1].arrival)
{
temp = p[j];
p[j] =p[j+1];
p[j+1]=temp;
}
}
}
for(i=0;i<num;i++)
{
sum = sum + p[i].burst;
p[i].turnaround = sum;
}
pline(44);
printf("\nPID\tArrival\tBurst\tTurnaround\n");
pline(44);
for(i=0;i<num;i++);
{
printf("\n%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst, p[i].turnaround);
sum = sum + p[i].turnaround;
}
pline(44);
avg = sum/num;
printf("\nTotal turnaround time: %f", sum);
printf("\nAverage turnaround time: :%f", avg);
}
void pline(int x)
{
int i;
for(i=0;i<x;i++)
{
printf("-");
}
printf("\n");
}
0
What does arrival time give you? If it is FCFS there is no need to use timestamps or sort. Just assign a SEQ ID and start processing.