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

Ex No 3A Algorithm For Fcfs

The document describes algorithms for four CPU scheduling techniques: First Come First Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). For each technique, the algorithm is presented in steps: 1) accept processes into the ready queue, 2) assign IDs and burst times, 3) calculate waiting times and turnaround times by iterating through the queue. The RR algorithm also includes calculating time slices for each process based on the time quantum. A sample C program implementing RR scheduling is provided along with example output.

Uploaded by

Jasmine Mary
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)
51 views

Ex No 3A Algorithm For Fcfs

The document describes algorithms for four CPU scheduling techniques: First Come First Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). For each technique, the algorithm is presented in steps: 1) accept processes into the ready queue, 2) assign IDs and burst times, 3) calculate waiting times and turnaround times by iterating through the queue. The RR algorithm also includes calculating time slices for each process based on the time quantum. A sample C program implementing RR scheduling is provided along with example output.

Uploaded by

Jasmine Mary
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/ 6

Ex No 3a

Algorithm for FCFS

1. Accept the number of processes in the ready Queue


2. For each process in the -ready Q, assign the process id and accept the CPU burst time
3. Set the waiting of the first process as 0 and its burst time as its turn around time
4. for each process in the Ready Q calculate
a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
5. Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Ex.no 3b

Algorithm for SJF

1. Accept the number of processes in the ready Queue


2. For each process in the ready Q, assign the process id and accept the CPU burst time
3. Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
4. Set the waiting time of the first process as 0 and its turnaround time as its burst time.
5. For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
6. Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Ex.no 3c
Algorithm for priority Scheduling
1. Accept the number of processes in the ready Queue
2. For each process in the ready Q, assign the process id and accept the CPU burst time
3. Sort the ready queue according to the priority number.
4. Set the waiting of the first process as 0 and its burst time as its turn around time
5. For each process in the Ready Q calculate
1

a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
6. Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process

Ex.No 3d
Algorithm for RR
1. Accept the number of processes in the ready Queue and time quantum (or) time slice
2. For each process in the ready Q, assign the process id and accept the CPU burst time
3. Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
4. If the burst time is less than the time slice then the no. of time slices =1.
5. Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of
process(n-1 ) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
6. Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
PROGRAM:

/*CPU-SCHEDULING by ROUND ROBIN*/

#include<stdio.h>
#include<conio.h>
void main()
{
int tq,ct[10],s=0,totwt=0,turnt=0,status=0;
int jremaint[10],pburst[10],wt[10],pt[10],n,i;
2

clrscr();
printf("\n CPU-SCHEDULING by ROUND ROBIN\n");
printf(" .............................\n\n");
printf("Enter the no of process ");
scanf("%d",&n);
printf("\nEnter the process burst time\n");
for(i=0;i<n;i++)
{
scanf("%d",&pburst[i]);
jremaint[i]=pburst[i];
pt[i]=wt[i]=0;
}
printf("Enter the Time Quantum ");
scanf("%d",&tq);
do
{
status=0;
for(i=0;i<n;i++)
{
if(jremaint[i]!=0)
{
wt[i]+=(s-pt[i]);
if(jremaint[i]<tq)
{
pt[i]=s=s+jremaint[i];
jremaint[i]=0;
}
else
{
3

pt[i]=s;
s=s+tq;
jremaint[i]-=tq;
}
if(jremaint[i]==0)
ct[i]=s;
status=1;
}
}
}
while(status);
printf("\nProcess BurstTime WaitingTime TurnArroundTime \n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t %d\t\t%d\n",i+1,pburst[i],wt[i],wt[i]+pburst[i]);
totwt+=wt[i];
turnt+=(wt[i]+pburst[i]);
}
printf("\nTotal waiting time =%d\n",totwt);
printf("\nAverage waiting time =%f \n",(float)totwt/n);
printf("\nTotal turn around time =%d\n",turnt);
printf("\nAverage turnaround time =%f\n",(float)turnt/n);
getch();
}

OUTPUT:

CPU-SCHEDULING by ROUND ROBIN


4

.............................

Enter the no of process 5

Enter the process burst time


5(1)
6(2)
3
4
3
Enter the Time Quantum 2
Gantt chart

0 || p[1] || 2 || p[2] || 4 || p[3] || 6 || p[4] 8 || p[5] || 10 || p[1] || 12 || p[2] || 14 || p[3] || 15 || p[4] ||


17 || p[5] || 18 || p[1] || 19 || p[2] 21

Process BurstTime WaitingTime TurnArroundTime

18

23

19

25

14

17

15

19

17

20

Total waiting time =83

Average waiting time =16.600000

Total turn around time =104

Average turnaround time =20.800000

You might also like