Assignment 2
Assignment 2
Write generic code in C of CPU Scheduling Algorithms, FCFS, SJF, SRTF, RR,
nonPreemptive Priority, and Preemptive Priority. In the program first user will input
data in sequence like, process no., arrival time, burst time, and priority and then select
any one of the algorithm to get TAT, WT and RT for each process and their averages.
(If RR is selected by user then also take time quantum as input).
FCFS code:
// C++ program for implement FCFS
#include<bits/stdc++.h>
using namespace std
struct Process
findavgTime(proc, n)
// Driver code
int main(
priorityScheduling(proc, n)
return 0
SJF Code:
// C++ program to implement Shortest Job first with Arrival
// Time
#include <iostream>
using namespace std
int mat[10][6]
int temp = *a
*a = *b
*b = temp
int main(
arrangeArrival(num, mat)
completionTime(num, mat)
cout << "Final Result...\n"
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n"
for (int i = 0; i < num; i++)
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n"
}
SRTF Code:
#include<iostream>
int a[10],b[10],x[10]
int waiting[10],turnaround[10],completion[10]
int i,j,smallest,count=0,time,n
double avg=0,tt=0,end
b[9]=9999
for(time=0; count!=n; time++
smallest=9
for(i=0; i<n; i++
b[smallest]--
if(b[smallest]==0
count++
end=time+1
completion[smallest] = end
waiting[smallest] = end - a[smallest] - x[smallest]
turnaround[smallest] = end - a[smallest]
{
cout<<"Process"<<"\t"<< "burst-time"<<"\t"<<"arrival-
time" <<"\t"<<"waiting-time" <<"\t"<<"turnaround-
time"<< "\t"<<"completion-time"<<endl
for(i=0; i<n; i++
cout<<"p"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<
<"\t\t"<<turnaround[i]<<"\t\t"<<completion[i]<<endl
avg = avg + waiting[i]
tt = tt + turnaround[i]
RR code:
#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], t
emp[10];
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 details of the process like Arrival time a
nd 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"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept 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();
}
#include<stdio.h>
int main(
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_w
t,avg_tat
printf("Enter Total Number of Process:")
scanf("%d",&n)
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 usin
g 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[i]=0
for(j=0;j<i;j++
{
wt[i]+=bt[j]
total+=wt[i]
return 0
struct Process
int processID
int burstTime
int tempburstTime
int responsetime
int arrivalTime
int priority
int outtime
int intime
}
<
<
>
if (min.responsetime == -1
min.responsetime = *currentTime - min.arrivalTime
--(*heapsize)
if (*heapsize >= 1)
Heap[0] = Heap[*heapsize]
order(Heap, heapsize, 0)
return min
if (heapsize == 0
return
Process Heap[4 * n]
// Driver code
}
int main(
int n, i
Process a[5]
a[0].processID = 1
a[0].arrivalTime = 4
a[0].priority = 2
a[0].burstTime = 6
a[1].processID = 4
a[1].arrivalTime = 5
a[1].priority = 1
a[1].burstTime = 3
a[2].processID = 2
a[2].arrivalTime = 5
a[2].priority = 3
a[2].burstTime = 1
a[3].processID = 3
a[3].arrivalTime = 1
a[3].priority = 4
a[3].burstTime = 2
a[4].processID = 5
a[4].arrivalTime = 3
a[4].priority = 5
a[4].burstTime = 4
priority(a, 5)
return 0
2.
Code:
// C++ program for implementation of RR scheduling
#include<iostream>
using namespace std
// Driver code
int main(
// process id's
int processes[] = { 1, 2, 3,4,5}
}
// Time quantum
int quantum = 2
findavgTime(processes, n, burst_time, quantum)
return 0
}