0% found this document useful (0 votes)
53 views9 pages

Ex3 FCFS SJF PS RR

The document contains code for implementing different CPU scheduling algorithms: 1) First Come First Serve (FCFS) scheduling algorithm 2) Shortest Job First (SJF) scheduling algorithm 3) Priority scheduling algorithm 4) Round Robin (RR) scheduling algorithm. The codes take input like number of processes, burst times, priorities and implement the scheduling algorithms to calculate waiting times and turnaround times.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
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)
53 views9 pages

Ex3 FCFS SJF PS RR

The document contains code for implementing different CPU scheduling algorithms: 1) First Come First Serve (FCFS) scheduling algorithm 2) Shortest Job First (SJF) scheduling algorithm 3) Priority scheduling algorithm 4) Round Robin (RR) scheduling algorithm. The codes take input like number of processes, burst times, priorities and implement the scheduling algorithms to calculate waiting times and turnaround times.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
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/ 9

3)a.

PROGRAM FOR FCFS SCHEDULING ALGORITHM

#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
int main()
{
int t=0,i,j,n,pid[20],bt[20],wt[20],tt[20],twt,ttt;
printf("\n Enter the no. of processes to be scheduled");
scanf("%d",&n);
printf("Enter the process details");
for(i=1;i<=n;i++)
{
printf("\n Enter theprocess ID:");
scanf("%d",&pid[i]);
printf("\n Enter the burst time for the process %d",pid[i]);
scanf("%d",&bt[i]);
}
wt[1]=0;
tt[1]=bt[1];
twt=0;
ttt=tt[1];
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
twt+=wt[i];
tt[i]=wt[i]+bt[i];
ttt+=tt[i];
}
printf("\n FirstComeFirstServe");
printf("\n........");
printf("\n\n.....");
printf("\n process id\t burst time\t waiting time\t turnaround time");
printf("\n..............");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d",pid[i],bt[i],wt[i],tt[i]);
}
printf("\n Gantt chart");
printf("0");
for(i=1;i<=n;i++)
{
t=t+bt[i];
for(j=1;j<=bt[i];j++)
printf("#");
printf("%d",t);
}
printf("\ntotal waiting time %d",twt);
printf("\nturnaround time %d",ttt);
printf("\navg waiting time %d",twt/n);
printf("\navg turnaround time %d",ttt/n);
}

ccetstudent@bcse17:~$ gcc fcfs.c


ccetstudent@bcse17:~$ ./a.out

Enter the no. of processes to be scheduled3


Enter the process details
Enter theprocess ID:1

Enter the burst time for the process 1 5

Enter theprocess ID:2

Enter the burst time for the process 2 6

Enter theprocess ID: 3

Enter the burst time for the process 3 9

FirstComeFirstServe
........

.....
process id burst time waiting time turnaround time
..............
1 5 0 5
2 6 5 11
3 9 11 20
Gantt chart0#####5######11#########20
total waiting time 16
turnaround time 36
avg waiting time 5
avg turnaround time 12ccetstudent@bcse17:~$

3)b.PROGRAM FOR SJF SCHEDULING ALGORITHM

#include<stdio.h>
int main()
{
int t=0,i,temp,j,n,pid[20],bt[20],wt[20],tt[20],twt,ttt;
printf("\nEnter the number of process to be scheduled:");
scanf("%d",&n);
printf("\n Enter the process details\n");
for(i=1;i<=n;i++)
{
printf("\n Enter the process id:");
scanf("%d",&pid[i]);
printf("\n Enter the burst time for the process d",pid[i]);
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(bt[j]<bt[i])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pid[i];
pid[i]=pid[j];
pid[j]=temp;
}
}
}
wt[1]=0;
tt[1]=bt[1];
twt=0;
ttt=tt[1];
for( i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
twt+=wt[i];
tt[i]=wt[i]+bt[i];
ttt+=tt[i];
}
printf("\n\t shortest job scheduling algorithm");
printf("\n\t\t-----------------------------------------------");
printf("\n\n-------------------------------------------------");
printf("\n process ID\t burst time\twaiting time\tturnaround time");
printf("\n----------------------------------------------------------");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t",pid[i],bt[i],wt[i],tt[i]);
}
printf("\n gant chart");
printf("0");
for(i=1;i<=n;i++)
{
t=t+bt[i];
for(j=1;j<=bt[i];j++)
printf("#");
printf("%d",t);
}
printf("\n the total waitting time:%d",twt);
printf("\n the total turnaround time:%d",ttt);
printf("\n avarage waiting time :%d",twt/n);
printf("\n the average turnaround time:%d",ttt/n);
}

Enter the number of process to be scheduled: 2

Enter the process details

Enter the process id:1

Enter the burst time for the process d23

Enter the process id:2

Enter the burst time for the process d63

shortest job scheduling algorithm


-----------------------------------------------

-------------------------------------------------
process ID burst time waiting time turnaround time
----------------------------------------------------------
1 23 0 23
2 63 23 86

gantchart0#######################23##############################################
#################86
the total waitting time:23
the total turnaround time:109
avarage waiting time :11
the average turnaround time:54ccetstudent@bcse17:
3)c.PROGRAM:

/*Priority Scheduling*/

#include<stdio.h>
struct process
{
char pname[10];
int ex_time,wt_time,st_time,end_time,turn_time,priority;
}p[10],temp;
main()
{
int n,i,j,k;
float avgwaittime=0.0,avgturnaroundtime=0.0;
float totalwaittime=0.0;
int totalexectime=0,totalturnaroundtime=0;
printf("\n enter number of process");
scanf("%d",&n);
p[0].st_time=0;
p[0].wt_time=0;
for(i=0;i<n;i++)
{
printf("\n enter process name");
scanf("%s",p[i].pname);
printf("\n enter process priority 0 with highest priority");
scanf("%d",&p[i].priority);
printf("enter bursttime");
scanf("%d",&p[i].ex_time);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(p[i].priority>p[j].priority)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
for(j=0;j<n;j++)
{
if(j==0)
{
p[j].wt_time=0;
p[j].st_time=0;
p[j].end_time=p[j].ex_time;
p[j].turn_time=p[j].ex_time+p[j].wt_time;
}
if(j>0)
{
p[j].wt_time=p[j-1].end_time;
p[j].st_time=p[j-1].end_time;
p[j].end_time=p[j].st_time+p[j].ex_time;
p[j].turn_time=p[j].ex_time+p[j].wt_time;
}
totalexectime+=p[j].ex_time;
totalwaittime+=p[j].wt_time;
totalturnaroundtime+=p[j].turn_time;
}
avgwaittime=(float)totalwaittime/n;
avgturnaroundtime=(float)totalturnaroundtime/n;
printf("\n\n name burst start end waittime turnaroundtime\n");
for(k=0;k<n;k++)
printf("\n%s\t%d\t%d\t%d\t%d\t
%d",p[k].pname,p[k].ex_time,p[k].st_time,p[k].end_time,p[k].wt_time,p[k].t
urn_time);
printf("\n averagewaitingtime%f",avgwaittime);
printf("\n averageturnaroundtime%f",avgturnaroundtime);
}
3)d.PROGRAM:

/*Round Robin Scheduling*/

#include<stdio.h>
main()
{
int i,j=0,k,bt[10],b[10],pn[10],bt1[15];
int wt[10],tt[10],temp;
float avgw,avgt,sumw=0.0,sumt=0.0;
int q,n;
printf("enter the number of process:");
scanf("%d",&n);
printf("enter the quantum time:");
scanf("%d",&q);
for(i=0;i<n;i++)
{
printf("enter the burst time of process %d:",i+1);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
b[i]=bt[i];
}
bt1[0]=0;
pn[0]=0;
for(k=0;k<n;k++)
{
do
{
for(i=0;i<n;i++)
{
if(bt[i]>=q&&bt[i]!=0)
{
bt[i]=bt[i]-q;
bt1[j+1]=bt1[j]+q;
pn[j+1]=i+1;
j++;
}
else if(bt[i]!=0)
{
bt1[j+1]=bt1[j]+bt[i];
bt[i]=0;
pn[j+1]=i+1;
j++;
}
}
}while(bt[k]>0);
}
printf("\n");
printf("******************gantt chart***********************\n\n");
for(i=1;i<=j;i++)
{
printf("p%d\t",pn[i]);
}
printf("\n");
for(i=0;i<j+1;i++)
{
printf("%d\t",bt1[i]);
}
for(i=0;i<n;i++)
{
wt[i]=bt1[i];
temp=bt1[i+1];
for(k=n;k<j;k++)
{
if(pn[k+1]==pn[k])
{
temp=bt1[k];
}
if(pn[k+1]==i+1)
{
wt[i]=wt[i]+(bt1[k]-temp);
temp=bt1[k];
}
}
}
for(i=0;i<n;i++)
{
tt[i]=b[i]+wt[i];
}
printf("\n\n\n PROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME");
for(i=0;i<n;i++)
{
printf("\n p%d\t",i+1);
printf("%d\t\t",b[i]);
printf("%d\t\t",wt[i]);
printf("%d",tt[i]);
sumw=sumw+wt[i];
sumt=sumt+tt[i];
}
avgw=sumw/n;
avgt=sumt/n;
printf("\n\n\n average waiting time:%f",avgw);
printf("\n average turn around time:%f",avgt);
}

You might also like