0% found this document useful (0 votes)
4 views3 pages

Fcfsandsjf

The document contains two C programs that implement CPU scheduling algorithms: First-Come, First-Served (FCFS) and Shortest Job First (SJF). Both programs prompt the user to enter process IDs and burst times, calculate waiting and turnaround times, and display the results including average waiting and turnaround times. The FCFS program processes tasks in the order they arrive, while the SJF program sorts tasks by burst time before processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Fcfsandsjf

The document contains two C programs that implement CPU scheduling algorithms: First-Come, First-Served (FCFS) and Shortest Job First (SJF). Both programs prompt the user to enter process IDs and burst times, calculate waiting and turnaround times, and display the results including average waiting and turnaround times. The FCFS program processes tasks in the order they arrive, while the SJF program sorts tasks by burst time before processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FCFS:

#include<stdio.h>
#include<stdlib.h>
#include<float.h>
int main()
{
int bt[20],pid[20],tat[20];
int i,j,n,wt[20];
float att,awt,tat1=0.0;
printf("First come first served\n");
printf("Enter the number of processes: ");
scanf("%d",&n);
wt[0]=0;
printf("Enter process id of all the processes: ");
for(i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}
printf("Enter burst time of all the processes: ");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
//for calculating waiting time of each process
for(i=1; i<n; i++)
{
wt[i]= bt[i-1]+ wt[i-1];
}
printf("Process ID Burst Time Waiting Time TurnAround Time\n");
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);
//calculating and printing turnaround time of each process
printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
//for calculating total waiting time
awt += wt[i];
//for calculating total turnaround time
tat1 += (wt[i]+bt[i]);
}
//for calculating average waiting time
printf("Total waiting time=%f\n",awt);
printf("total turn around time=%f\n",tat1);
awt = awt/n;
//for calculating average turnaround time
att = tat1/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time=%f\n",att);
}

SJF:
#include<stdio.h>
#include<stdlib.h>
#include<float.h>

void main()
{
int p[20],bt[20],pos,temp,pid[20],wt[20],tat[20];
int i,j,n,total=0,choice,total1=0;
float avg_wt,avg_tat,twt=0.0,tat1=0.0;
printf("---SJF Scheduling Algorithm---\n ");
printf("Enter number of process:");
scanf("%d",&n);
printf("Enter process id of all the processes: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\nEnter Burst Time of all processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total1+=wt[i];
}
avg_wt=(float)total1/n;
total=0;
printf("\nProcess\tBurst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\nTotal waiting time:%d",total1);
printf("\nTotal Turnaround Time:%d",total);
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

You might also like