0% found this document useful (0 votes)
4 views13 pages

OS Practical 04

The document presents a menu-driven program for implementing various CPU scheduling algorithms including FCFS, SJF, Priority, and Round Robin. Each algorithm is accompanied by C++ code that demonstrates how to calculate waiting time, turnaround time, and average times for processes. The document includes detailed code snippets and explanations for each scheduling method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

OS Practical 04

The document presents a menu-driven program for implementing various CPU scheduling algorithms including FCFS, SJF, Priority, and Round Robin. Each algorithm is accompanied by C++ code that demonstrates how to calculate waiting time, turnaround time, and average times for processes. The document includes detailed code snippets and explanations for each scheduling method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: Vaishnavi Ganeshkar

Roll No.: 80

PRACTICAL 04
Write a menu driven program for implementing CPU Scheduling
Algorithms-FCFS, SJF, Priority & Round Robin.

• FCFS(With arrival time)


CODE:
#include<iostream>
#define MAX_PROCESS 10
using namespace std;
class process
{
public:
int process_num;
int burst_time;
int arrival_time;
int response_time;
int waiting_time;
int turnaround_time;
void input_process(int);
int get_at()
{
return arrival_time;
}
};
void process::input_process(int count)
{
process_num=count+1;
cout<<"\nENTER BURST TIME FOR PROCESS "<<count+1<<" : ";
cin>>burst_time;
cout<<"ENTER ARRIVAL TIME FOR PROCESS "<<count+1<<" : ";
cin>>arrival_time;
}
void calc_wait_tat(process*,int);
void average(process*,int);
void display(process*,int);
int main()
{
process p[MAX_PROCESS],temp;
int num,i,j;
cout<<"ENTER NUMBER OF PROCESSES : ";
cin>>num;
for(i=0;i<num;++i)
p[i].input_process(i);
for(i=0;i<num;++i)
{
for(j=i+1;j<num;++j)
{
if(p[i].get_at()>p[j].get_at())
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
calc_wait_tat(p,num);
display(p,num);
return 0;
}
void calc_wait_tat(process *p,int n)
{
int i;
p[0].response_time=0;
for(i=1;i<n;++i)
{
p[i].response_time=p[i-1].burst_time+p[i-1].response_time;
if(p[i].response_time<p[i].arrival_time)
p[i].response_time=p[i].arrival_time;
}
p[0].waiting_time=0;
for(i=1;i<n;++i)
p[i].waiting_time=p[i].response_time-p[i].arrival_time;
for(i=0;i<n;++i)
p[i].turnaround_time=p[i].waiting_time+p[i].burst_time;
}
void average(process *p,int n)
{
float avg_wt=0,avg_tat=0;
for(int i=0;i<n;++i)
{
avg_wt+=(float)p[i].waiting_time;
avg_tat+=(float)p[i].turnaround_time;
}
avg_wt/=n;
avg_tat/=n;
cout<<"\n\nAVERAGE WAITING TIME : "<<avg_wt;
cout<<"\nAVERAGE TURN AROUND TIME : "<<avg_tat;
}
void display(process *p,int n)
{
cout<<"Processes "<<" Burst time "<<" Waiting time "<<" Turn around time\n";
for (int i=0;i<n;i++)
{
cout<<"\n "<<p[i].process_num<<"\t\t"<<p[i].burst_time<<"\t
"<<p[i].waiting_time<<"\t\t "<<p[i].turnaround_time;
}
average(p,n);
}

OUTPUT:

• FCFS(With arrival time)


CODE:
#include<iostream>

using namespace std;


int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<"Enter total number of processes(maximum 20):";
cin>>n;

cout<<"\nEnter Process Burst Time\n";


for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


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

cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

//calculating turnaround time


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

avwt/=i;
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;

return 0;
}

OUTPUT:
• Non-Preemptive SJF
CODE:
#include <bits/stdc++.h>
using namespace std;
//structure for every process
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
void findTurnAroundTime(Process proc[], int n, int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
//waiting time of all process
void findWaitingTime(Process proc[], int n, int wt[]) {
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
// decrementing the remaining time
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// If a process gets completely
// executed
if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate average time
void findavgTime(Process proc[], int n) {
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
// Function to find waiting time of all
// processes
findWaitingTime(proc, n, wt);
// Function to find turn around time for
// all processes
findTurnAroundTime(proc, n, wt, tat);
cout << "Processes " << " Burst time " << " Waiting time " << " Turn around
time\n";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t" << proc[i].bt << "\t\t " << wt[i] << "\t\t " <<
tat[i] << endl;
}
cout << "Average waiting time = " << (float)total_wt / (float)n; cout << "Average
turn around time = " << (float)total_tat / (float)n;
}
// main function
int main() {
Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 }, { 3, 6, 2 }, { 4, 5, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}

OUTPUT:

• Round Robin Scheduling


• CODE:

// C++ program for implementation of RR scheduling


#include<iostream>
using namespace std;
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner
// until all of them are not done.
while (1)
{
bool done = true;
// Traverse all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];
// Waiting time is current time minus time
// used by this process
wt[i] = t - bt[i];
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == true)
break;
}
}
// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
// process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// Burst time of all processes
int burst_time[] = {10, 5, 8};
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

OUTPUT:

• PRIORITY SCHEDULING
CODE:
#include<iostream>

using namespace std;

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:";
cin>>n;

cout<<"\nEnter Burst Time and Priority\n";


for(i=0;i<n;i++)
{
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>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;

cout<<"\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];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}

avg_tat=total/n; //average turnaround time


cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;

return 0;
}

OUTPUT:

You might also like