0% found this document useful (0 votes)
82 views16 pages

Ex - No:7 (A) Date: Aim:: Cpu Scheduling Algorithms

The document describes the algorithm for implementing Round Robin CPU scheduling. It involves reading the number of processes and their burst times and time quantum as input. It then calculates the waiting times by simulating time slices of length quantum for each process until all bursts are completed. It outputs the waiting times, turnaround times, and calculates the average waiting and turnaround times. A Gantt chart is also drawn to represent the process execution.

Uploaded by

Maheshwaran
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)
82 views16 pages

Ex - No:7 (A) Date: Aim:: Cpu Scheduling Algorithms

The document describes the algorithm for implementing Round Robin CPU scheduling. It involves reading the number of processes and their burst times and time quantum as input. It then calculates the waiting times by simulating time slices of length quantum for each process until all bursts are completed. It outputs the waiting times, turnaround times, and calculates the average waiting and turnaround times. A Gantt chart is also drawn to represent the process execution.

Uploaded by

Maheshwaran
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/ 16

Ex.

No:7(A) CPU SCHEDULING ALGORITHMS


DATE:

AIM:
To write a C program to perform SJF scheduling algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of requests.
Step 3: Input the processes along with their burst time (bt).
Step 4: Sort all the processes in increasing order according to burst time.( (i.e. the job
having least burst time is executed first).
Step 5: Print the order in which process gets executed.
Step 6: Find waiting time (wt) for all processes.
Step 7: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 8: Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
Step 9: Find turnaround time = waiting_time + burst_time for all processes.
Step 10: Find average waiting time = total_waiting_time / no_of_processes.
Step 11: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 12: The gantt chart is drawn representing the process execution.
Step 13: Stop the process.
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//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];
total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

printf("\nProcess\t Burst 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\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
OUTPUT:

Gantt chart

P2(3) P3(3) P1(24)

0 3 6 30

RESULT: Thus the C program to perform SJF scheduling algorithm has been executed successfully.

PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF THE FACULTY
Ex.No :7(B) IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM

DATE:
AIM:
To write a C program to perform FCFS Scheduling algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the number of requests.
Step 3: Input the processes along with their burst time (bt).
Step 4: Find waiting time (wt) for all processes.
Step 5: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 6: Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
Step 7: Find turnaround time = waiting_time + burst_time for all processes.
Step 8: Find average waiting time = total_waiting_time / no_of_processes.
Step 9: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 10: The gantt chart is drawn representing the process execution.
Step 11: Stop the process.
PROGRAM:

#include<stdio.h>

int main()

{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Timen");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcessttBurst TimetWaiting TimetTurnaround Time");

for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}
OUTPUT:

Gantt chart

P1(24) P2(3) P3(3)

0 24 27 30

RESULT:
Thus the C program to perform FCFS scheduling algorithm has been executed successfully.

PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100

INITIAL OF THE FACULTY


Ex. No:7(c) IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM
DATE:

AIM:

To write a C program to implement the priority scheduling algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of requests.
Step 3: Input the processes along with their burst time (bt) and priority.
Step 4: Sort the processes, burst time and priority according to the priority.
Step 5: Print the order in which process gets executed.
Step 6: Find waiting time (wt) for all processes.
Step 7: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 8: Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
Step 9: Find turnaround time = waiting_time + burst_time for all processes.
Step 10: Find average waiting time = total_waiting_time / no_of_processes.
Step 11: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 12: The gantt chart is drawn representing the process execution.
Step 13: Stop the process.
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}

OUTPUT:

Gantt chart

P4 P3 P2 P1

0 6 11 15 18

RESULT: Thus the C program to perform PRIORITY scheduling algorithm has been executed
Successfully

PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100

INITIAL OF THE FACULTY


Ex.No:7(D) IMPLEMENTATION OF ROUND ROBIN ALGORITHM
DATE:

AIM:

To write a C program to implement the round robin algorithm

ALGORITHM:

Step 1: Start the program.


Step 2: Read the number of process.
Step 3: Input the processes along with their burst time (bt) and time quantum.
Step 4: Find waiting time (wt) for all processes.
Step 5: Create an array rem_bt[] to keep track of remaining burst time of processes. This
array is initially a copy of bt[] (burst times array).
Step 6: Create another array wt[] to store waiting times.Initialize this array as 0.
Step 7: Initialize time : t = 0
Keep traversing the all processes while all processes are not done.
Do following for i'th process if it is not done yet.
a- If rem_bt[i] > quantum
(i) t = t + quantum
(ii) bt_rem[i] - = quantum;
c- Else // Last cycle for this process.
(i) t = t + bt_rem[i];
(ii) wt[i] = t - bt[i]
(ii) bt_rem[i] = 0; // This process is over
Step 8: Find turnaround time = waiting_time + burst_time for all processes.
Step 9: Find average waiting time = total_waiting_time / no_of_processes.
Step 10: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 11: The gantt chart is drawn representing the process execution.
Step 12: Stop the process.
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];
}

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();
}

Gantt chart:

P1 P2 P3 P4 P1 P3 P4

0 6 11 17 23 25 29 34
OUTPUT:

RESULT: Thus the C program to perform ROUND ROBIN scheduling algorithm has been executed
Successfully.

PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100

INITIAL OF THE FACULTY

You might also like