[Link].
3(A) First Come First Scheduling
Aim:
To write a C program to implement the concept of First Come First Scheduling.
Algorithm:
1. Start.
2. Read the [Link] process.
3. Read the processes arrival time and burst time.
4. Based on the first come first scheduling display the job scheduling process.
5. Stop.
Program:
#include <stdio.h>
int wt[10],bt[10],at[10],tat[10],n;
float awt,atat;
void input()
{
printf("Enter Number of processes:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("Enter Burst Time of process %d:",i+1);
scanf("%d",&bt[i]);
printf("Enter Arrival Time of process %d:",i+1);
scanf("%d",&at[i]);
}
}
void calculate()
{
wt[0]=0;
atat=tat[0]=bt[0];
int btt=bt[0];//to store total burst time sum
int i;
for(i=1;i<n;i++){
wt[i]=btt-at[i];
btt+=bt[i];
awt+=wt[i];
tat[i]= wt[i]+bt[i];
atat+=tat[i];
}
atat/=n;
awt/=n;
}
void display()
{
int i;
printf("PROCESS NO\tARRIVAL TIME.\tBURST TIME.\tWAITING TIME\tTURN AROUND TIME.\
n");
for(i=0;i<n;i++)
{
printf("%3d\t%11d\t%11d\t%11d\t%11d\n",i+1,at[i],bt[i],wt[i],tat[i]);
}
printf("Average Waiting Time: %f\nAverage Turn Around Time:%f",awt,atat);
}
int main()
{
printf("FCFS CPU Scheduling Algorithm\n");
input();
calculate();
display();
}
[Link]. 3(B) Shortest Job First Scheduling
Aim:
To write a C program to implement the concept of Shortest Job First Scheduling.
Algorithm:
1. Start.
2. Read the [Link] process.
3. Read the processes arrival time and burst time.
4. Based on the shortest job first scheduling display the job scheduling process.
5. Stop.
Program:
#include<stdio.h>
void main()
{
int i,j,temp,index[20],b[20],t[20],w[20],tot_burst=0,tot_wait=0,tot_turn=0;
float avg_wait,avg_turn,n;
int a[20],count=0,enter[20],c_wait[20],time=0,name[20];
printf("Enter the no of process : ");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for p%d : ",i+1);
scanf("%d",&b[i]);
printf("Enter the arrival time for p%d : ",i+1);
scanf("%d",&a[i]);
tot_burst+=b[i];
enter[i]=0;
}
for(i=0;i<n;i++)
{ temp=0;
for(j=0;j<n;j++)
{
if((i!=j)&&(b[i]>=b[j]))
{
temp++;
}
if((i<j)&&(b[i]==b[j]))
{
temp--;
}
}
index[temp]=i;
}
for(i=0;count<n;i++)
{
for(j=0;j<n;j++)
{
if((time>=a[index[j]])&&(enter[index[j]]!=1))
{
enter[index[j]]=1;
time+=b[index[j]];
name[count]=index[j];
count++;
break;
}
}
}
c_wait[name[0]]=w[name[0]]=0;
tot_turn=t[name[0]]=b[name[0]];
for(i=1;i<n;i++)
{ c_wait[name[i]]=c_wait[name[i-1]]+b[name[i-1]];
w[name[i]]=c_wait[name[i]]-a[name[i]];
t[name[i]]=w[name[i]]+b[name[i]];
tot_turn+=t[name[i]];
tot_wait+=w[name[i]];
}
avg_wait=tot_wait/n;
avg_turn=tot_turn/n;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
{
printf("\t\tp%d",name[i]+1);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",c_wait[name[i]]);
}
printf("%d",tot_burst);
printf("\nProcess\tBurst Time\tArrival Time\tWait Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("p%d\t%10d\t%12d\t%9d\t%15d\n",i,b[i],a[i],w[i],t[i]);
}
printf("Average Waiting Time = %f ms\nAverage Turnaround Time=%f ms\n",avg_wait,avg_turn);
}
[Link] 3(C) Round Robin Scheduling
Aim:
To write a C program to implement the concept of Round Robin Scheduling.
Algorithm:
1. Start.
2. Read the [Link] process, time quantum.
3. Read the processes arrival time and burst time.
4. Based on the round robin scheduling display the job scheduling process.
5. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t");
scanf("%d", &at[i]);
printf(" \nBurst time is: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}