0% found this document useful (0 votes)
42 views

Scheduling Algm Os Pgms

The document describes C code implementations of priority scheduling and round robin scheduling algorithms. The priority scheduling code sorts processes by priority and burst time, then calculates waiting times and turnaround times. The round robin code implements a time quantum for scheduling processes and calculates average waiting and turnaround times.

Uploaded by

aarthi dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Scheduling Algm Os Pgms

The document describes C code implementations of priority scheduling and round robin scheduling algorithms. The priority scheduling code sorts processes by priority and burst time, then calculates waiting times and turnaround times. The round robin code implements a time quantum for scheduling processes and calculates average waiting and turnaround times.

Uploaded by

aarthi dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CPU SCHEDULING ALGORITHMS

Ex.No:
PRIORITY SCHEDULING
AIM:
To write a C program for implementation of Priority scheduling algorithms.

// Online C compiler to run C program online


#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n"); for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using //selection
sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}

OutPut:
Enter Total Number of Process:4
Enter Burst Time and Priority

P[1]
Burst Time:6
Priority:3

P[2]
Burst Time:2
Priority:2
P[3]
Burst Time:4
Priority:1

P[4]
Burst Time:6
Priority:4

Process Burst Time Waiting Time Turnaround Time


P[3] 4 0 4
P[2] 2 4 6
P[1] 6 6 12
P[4] 6 12 18
Average Waiting Time=5
Average Turnaround Time=10
CPU SCHEDULING ALGORITHMS
Ex.No:
ROUND ROBIN SCHEDULING

AIM:

To write a C program for implementation of Round Robin scheduling


algorithms.

#include<stdio.h>
#include<conio.h>

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];

// at= arrival time, bt=burst time ,NOP=no of processes


float avg_wt, avg_tat;

printf(" Total number of process in the system: ");


scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y

// Use for loop to enter the processes Arrival time and the Burst Time

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"); // read arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // read the Burst
time scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}

// Read the Time qunat


printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);

// Display the process No, burst time, Turn Around Time and the waiting time
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) // define the conditions
{
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--; //decrement the process no.
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;
}
}
// represents the average waiting time and Turn Around time
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();
}
2
CPU SCHEDULING ALGORITHMS
Ex.No:1 FCFS ALGORITHM

AIM:
To write a C program for implementation of FCFS and SJF scheduling
algorithms.
#include<stdio.h>

int main(){

int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;

printf("Enter number of processes ");


scanf("%d",&n);

printf("Enter arrival time and burst time for each process\n\n");

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

printf("Arrival time of process[%d] ",i+1);


scanf("%d",&at[i]);

printf("Burst time of process[%d] ",i+1);


scanf("%d",&bt[i]);

printf("\n");
}

//calculate completion time of processes


for(int j=0;j<n;j++)
{
sum+=bt[j];
ct[j]+=sum;
}

//calculate turnaround time and waiting times

for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}

for(int k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalWT+=wt[k];
}

printf("Solution: \n\n");
printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");

for(int i=0;i<n;i++)
{
printf("P%d\t %d\t %d\t %d\t %d\t
%d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n);
printf("Average WT = %f\n\n",totalWT/n);

return 0;
}

Arrival time of process[3] 0


Burst time of process[3] 3

Solution:

P# AT BT CT TAT WT

P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27

Average Turnaround Time = 27.000000


Average WT = 17.000000

You might also like