0% found this document useful (0 votes)
213 views19 pages

OS Record

The document contains source code for implementing four different CPU scheduling algorithms: First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). For each algorithm, the code takes input like number of processes and their burst times, performs the scheduling and calculates metrics like waiting times and turnaround times. It then prints the Gantt chart and average waiting/turnaround times.

Uploaded by

KAVIARASU.D
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views19 pages

OS Record

The document contains source code for implementing four different CPU scheduling algorithms: First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). For each algorithm, the code takes input like number of processes and their burst times, performs the scheduling and calculates metrics like waiting times and turnaround times. It then prints the Gantt chart and average waiting/turnaround times.

Uploaded by

KAVIARASU.D
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

SOURCE CODE:

//FIRST COME FIRST SERVED


#include<stdio.h>
#include<conio.h>
main()
{
int n,bt[10],wt,w[10],temp,p[10],i,j,av;
float avg;
long avg1=0,sum=0; av=0; wt=0; temp=0;
printf("\n Enter the number of Process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i+1;
printf("\n Enter the Burst time of Process %d: ",i+1);
scanf("%d",&bt[i]);
}
printf("\n---------------------------------------------");
printf("\n|Process No. |\t Burst Time |\tWaiting Time |");
printf("\n---------------------------------------------");
for(i=0;i<n;i++)
{
w[i]=wt;
printf("\np%d \t\t%d \t\t%d \t\t%d ", p[i],bt[i],wt);
printf("\n--------------------------------------------");
wt=wt+bt[i];
}

printf("\n\n");
printf("\n***Gantt Chart***");
printf("\n~~~~~~~~~~~~~~~~~\n");
for(i=0;i<n;i++)
{
printf(" |P%d",p[i]);
}
printf("|");
printf("\n");
for(i=0;i<n;i++)
{
printf(" %d",w[i]);
avg=avg+w[i];
}
printf(" %d",wt);
// Average turn arround time Starts Here
printf("\n\nCalculating Average Turn Arround Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
sum+=w[i]+bt[i];
}
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]+bt[i]);
}
av=sum/n;
printf("\n Average Turn Arround Time:%d ",av);

//End Average turn arround time


//Average Waiting time starts here
printf("\n\nCalculating Average Waiting Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]);
}
avg/=n;
printf("\n Average waiting time:%d ",avg);
//End Average Waiting time
getch();
}

Output:

SOURCE CODE:
//SHORTEST JOB FIRST
#include<stdio.h>
#include<conio.h>
main()
{
int n,bt[10],p[10],w[10],wt,avg,temp,i,j,av;
long sum=0;
wt=0;avg=0;temp=0;
printf("\n\t\t Welcome to Shortest Job First Scheduling");
printf("\n\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\nEnter the Number of process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i+1;
printf("\n Enter the Burst time of P%d : ",i+1);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(bt[i]<bt[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;

temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
}
}
printf("\n-------------------------------------------");
printf("\n|\tProcess No. |\t Burst Time |\t Waiting Time |");
printf("\n-------------------------------------------";
for(i=0;i<n;i++)
{
w[i]=wt;
printf("\np%d \t\t%d \t\t%d \t\t%d ", p[i],bt[i],wt);
printf("\n--------------------------------------------");
wt=wt+bt[i];
}
printf("\n\n");
printf("\n***Gantt Chart***");
printf("\n~~~~~~~~~~~~~~~~~\n");
for(i=0;i<n;i++)
{
printf(" |P%d",p[i]);
}
printf("|");
printf("\n");
for(i=0;i<n;i++)
{
printf(" %d",w[i]);

avg=avg+w[i];
}
printf(" %d",wt);
// Average turnaround time Starts Here
printf("\n\calculating Average Turn Around Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
sum+=w[i]+bt[i];
}
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]+bt[i]);
}
av=sum/n;
printf("\n Average Turn Arround Time:%d ",av); //End ATAT
//Average Waiting time starts here
printf("\n\nCalculating Average Waiting Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]);
}
avg/=n;
printf("\n Average waiting time:%d ",avg);
//End Average Waiting time
getch();
}

OUTPUT:

SOURCE CODE:
// PRIORITY SCHEDULING
#include<stdio.h>
#include<conio.h>
main()
{
int n,bt[10],p[10],w[10],wt,avg,temp,i,j,av;
long sum=0;
wt=0;avg=0;temp=0;
printf("\n\t\t Welcome to Priority Scheduling");
printf("\n\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\nEnter the Number of process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i+1;
printf("\n Enter the Burst time of P%d : ",i+1);
scanf("%d",&bt[i]);
printf("\n Enter the priority of P%d : ",i+1);
scanf(%d,&pr[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pr[i]<pr[j])
{
temp=p[i];

p[i]=p[j];
p[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
printf("\n-------------------------------------------");
printf("\n|\tProcess No. |\t Burst Time|\tPriority |\t Waiting Time |");
printf("\n-------------------------------------------";
for(i=0;i<n;i++)
{
w[i]=wt;
printf("\np%d \t\t%d \t\t%d \t\t%d \t\t%d ", p[i],bt[i],pr[i],wt);
printf("\n--------------------------------------------");
wt=wt+bt[i];
}
printf("\n\n");
printf("\n***Gantt Chart***");
printf("\n~~~~~~~~~~~~~~~~~\n");
for(i=0;i<n;i++)
{
printf(" |P%d",p[i]);
}

printf("|");
printf("\n");
for(i=0;i<n;i++)
{
printf(" %d",w[i]);
avg=avg+w[i];
}
printf(" %d",wt);
// Average turnaround time Starts Here
printf("\n\calculating Average Turn Around Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
sum+=w[i]+bt[i];
}
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]+bt[i]);
}
av=sum/n;
printf("\n Average Turn Arround Time:%d ",av); //End ATAT
//Average Waiting time starts here
printf("\n\nCalculating Average Waiting Time");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],w[i]);
}

avg/=n;
printf("\n Average waiting time:%d ",avg);
//End Average Waiting time
getch();
}

OUTPUT:

SOURCE CODE:
// ROUND ROBIN SCHEDULING
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int n,br[10],i,tq,j,l,k,wt[20],p[20],tb,sum,av;
sum=0;
int ow,cwt[20],avg,tbr[20];
printf("\n\t\t WELCOME TO ROUND ROBIN SCHEDULING");
printf("\n\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n Enter the no.of Process: ");
scanf(%d,&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of p%d : ", i+1);
scanf(%d,&br[i]);
}
printf("\n Enter the Time Quantum: ");
scanf(%d,&tq);
j=0;

k=0;

wt[j]=0;

for(i=0;i<n;i++)

j++;

tb=0;

{
tb+=br[i];
tbr[i]=br[i];
}
while(tb!=0)
{
for(i=0;i<n;i++)
{
if(br[i]!=0)
{
if(br[i]>=tq)
{
tb-=tq;
br[i]-=tq;
p[k]=i+1;
wt[j]=wt[j-1]+tq;
k++; j++;
}
else if(br[i]<tq)
{
tb-=br[i];
p[k]=i+1;
wt[j]=wt[j-1]+br[i];

br[i]=0;
k++;
j++;
}
}
}
}
printf("\n\n Gantt Chart");
printf("\n ~~~~~~~~~~~\n";
for(i=0;i<k;i++)
printf("|P%d|",p[i]);
printf("\n");
for(i=0;i<j;i++)
printf(" %d",wt[i]);
printf("\n---------------------------------------------");
printf("\n\n| PROCESS

| BURST TIME | WAITING TIME|\n";

printf("\n---------------------------------------------";
for(i=0;i<n;i++)
{
ow=0;
cwt[i]=0;
for(l=0;l<j;l++)
{

if(i+1==p[l])
{
cwt[i]+=ow;
ow=0;
}
else
ow=ow+(wt[l+1]-wt[l]);
}
printf("\np%d \t\t%d \t\t%d \t\t%d ", p[i],tbr[i],cwt[i]);
printf("\n---------------------------------------------";
}
avg=0;
for(i=0;i<n;i++)
avg+=cwt[i];
avg/=n;
printf("\nAverage Waiting Time: %d",avg);

printf("\n\nCalculating Average Turn Arround Time");


printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(i=0;i<n;i++)
{
sum+=cwt[i]+tbr[i];
}

for(i=0;i<n;i++)
{
printf("\n\tP%d = %d",p[i],cwt[i]+tbr[i]);
}
av=sum/n;
printf("\n Average Turn Arround Time: %d",av);
getch();
}

OUTPUT

You might also like