Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

EXPERIMENT 5

Aim:- A program to simulate the FCFS CPU scheduling algorithm.


a) without arrival time
b) with arrival time

Theory:- Given n processes with their burst times, the task is to find average waiting time and
average turn around time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling
algorithm. FIFO simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after the
previous gets fully executed/

a)
Here we are considering that arrival time for all processes is 0.

Code:-
#include<iostream>
using namespace std;

// Function to find the waiting time for all


// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

// 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 wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//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;
}

// Driver code
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};

findavgTime(processes, n, burst_time);
return 0;
}

Output:-

b)
Here we consider different arrival time.

Code:-
#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 at[])
{
int service_time[n];
service_time[0] = 0;
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++)
{
// Add burst time of previous processes
service_time[i] = service_time[i-1] + bt[i-1];

// Find waiting time for current process =


// sum - at[i]
wt[i] = service_time[i] - at[i];

// If waiting time for a process is in negative


// that means it is already in the ready queue
// before CPU becomes idle so its waiting time is 0
if (wt[i] < 0)
wt[i] = 0;
}
}

// 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 waiting and turn-around


// times.
void findavgTime(int processes[], int n, int bt[], int at[])
{
int wt[n], tat[n];

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, at);

// 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 " << " Arrival Time "
<< " Waiting Time " << " Turn-Around Time "
<< " Completion Time \n";
int total_wt = 0, total_tat = 0;
for (int i = 0 ; i < n ; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
int compl_time = tat[i] + at[i];
cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"
<< at[i] << "\t\t" << wt[i] << "\t\t "
<< tat[i] << "\t\t " << compl_time << endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
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[] = {5, 9, 6};

// Arrival time of all processes


int arrival_time[] = {0, 3, 6};

findavgTime(processes, n, burst_time, arrival_time);

return 0;
}

Output:-

EXPERIMENT 6
Aim:- To write a program to stimulate the CPU scheduling algorithm Shortest job first (Non- Pre-
emptive)

Theory: - Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting
process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.

Code:-
#include<iostream>
using namespace std;
int mat[10][6];

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void arrangeArrival(int num, int mat[][6])


{
for(int i=0; i<num; i++)
{
for(int j=0; j<num-i-1; j++)
{
if(mat[j][1] > mat[j+1][1])
{
for(int k=0; k<5; k++)
{
swap(mat[j][k], mat[j+1][k]);
}
}
}
}
}

void completionTime(int num, int mat[][6])


{
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for(int i=1; i<num; i++)


{
temp = mat[i-1][3];
int low = mat[i][2];
for(int j=i; j<num; j++)
{
if(temp >= mat[j][1] && low >= mat[j][2])
{
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for(int k=0; k<6; k++)
{
swap(mat[val][k], mat[i][k]);
}
}
}

int main()
{
int num, temp;

cout<<"Enter number of Process: ";


cin>>num;

cout<<"...Enter the process ID...\n";


for(int i=0; i<num; i++)
{
cout<<"...Process "<<i+1<<"...\n";
cout<<"Enter Process Id: ";
cin>>mat[i][0];
cout<<"Enter Arrival Time: ";
cin>>mat[i][1];
cout<<"Enter Burst Time: ";
cin>>mat[i][2];
}

cout<<"Before Arrange...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<num; i++)
{
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";
}

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";
}
return 0;
}

Output:-

EXPERIMENT 7

Aim: - To simulate the CPU scheduling algorithm SRTF.

Theory:- In the Shortest Remaining Time First (SRTF) scheduling algorithm, the process with the
smallest amount of time remaining until completion is selected to execute. Since the currently
executing process is the one with the shortest amount of time remaining by definition, and since
that time should only reduce as execution progresses, processes will always run until they
complete or a new process is added that requires a smaller amount of time.
Code:-
#include <bits/stdc++.h>
using namespace std;

struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];

// Copy the burst time into rt[]


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;

// Process until all processes gets


// completed
while (complete != n) {

// Find process with minimum


// remaining time among the
// processes that arrives till the
// current time`
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;
}

// Reduce remaining time by one


rt[shortest]--;

// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

// If a process gets completely


// executed
if (rt[shortest] == 0) {

// Increment complete
complete++;
check = false;

// Find finish time of current


// process
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 turn around time


void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// 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);

// Display processes along with all


// details
cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";

// Calculate total waiting time and


// total turnaround time
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 << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
Process proc[] = { { 1, 6, 1 }, { 2, 8, 1 },
{ 3, 7, 2 }, { 4, 3, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);

findavgTime(proc, n);
return 0;
}
Output:-

EXPERIMENT 8

Aim: - To simulate the the CPU scheduling priority algorithm


a) without preemption
b) with preemption

Theory:- Priority Scheduling is a method of scheduling processes that is based on priority. In this
algorithm, the scheduler selects the tasks to work as per the priority.

The processes with higher priority should be carried out first, whereas jobs with equal priorities
are carried out on a round-robin or FCFS basis. Priority depends upon memory requirements,
time requirements, etc.

a)
In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy, will release the CPU either by switching context or terminating. It is
the only method that can be used for various hardware platforms. That's because it doesn't
need special hardware (for example, a timer) like preemptive scheduling.

Code:-
#include<bits/stdc++.h>
using namespace std;

struct Process
{
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};

// Function to sort the Process acc. to priority


bool comparison(Process a, Process b)
{
return (a.priority > b.priority);
}

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n,
int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++ )
wt[i] = proc[i-1].bt + wt[i-1] ;
}

// Function to calculate turn around time


void findTurnAroundTime( Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = proc[i].bt + wt[i];
}

//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);

//Display processes along with all details


cout << "\nProcesses "<< " 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 << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t " << wt[i]
<< "\t\t " << tat[i] <<endl;
}

cout << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

void priorityScheduling(Process proc[], int n)


{
// Sort processes by priority
sort(proc, proc + n, comparison);

cout<< "Order in which processes gets executed \n";


for (int i = 0 ; i < n; i++)
cout << proc[i].pid <<" " ;

findavgTime(proc, n);
}

// Driver code
int main()
{
Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}

Output: -

b)
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the
lower priority task is still running. The lower priority task holds for some time and resumes
when the higher priority task finishes its execution.

Code:-
#include<iostream>

using namespace std;


int main()
{
int a[10],b[10],x[10];
int waiting[10],turnaround[10],completion[10],p[10];
int i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;

cout<<"\nEnter the number of Processes: ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter arrival time of process: ";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"\nEnter burst time of process: ";
cin>>b[i];
}
for(i=0;i<n;i++)
{
cout<<"\nEnter priority of process: ";
cin>>p[i];
}
for(i=0; i<n; i++)
x[i]=b[i];

p[9]=-1;
for(time=0; count!=n; time++)
{
smallest=9;
for(i=0; i<n; i++)
{
if(a[i]<=time && p[i]>p[smallest] && b[i]>0 )
smallest=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"<<"\t"<<"Priority"<<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]<<"\t\t"<<p[i]<<endl;
avg = avg + waiting[i];
tt = tt + turnaround[i];
}
cout<<"\n\nAverage waiting time ="<<avg/n;
cout<<" Average Turnaround time ="<<tt/n<<endl;
}

Output:-

EXPERIMENT 9

Aim:- To simulate the CPU scheduling algorithm round-robin.

Theory:- Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time
slot in a cyclic way.
 It is simple, easy to implement, and starvation-free as all processes get fair share of CPU.
 One of the most commonly used technique in CPU scheduling as a core.
 It is preemptive as processes are assigned CPU only for a fixed slice of time at most.
 The disadvantage of it is more overhead of context switching.

Code:-
#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;
}

// Driver code
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:-
EXPERIMENT 11

Aim:- Implement deadlock avoidance (Banker’s Algorithm) and deadlock detection algorithms

Theory: - The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that
tests for safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.

Code:-
#include <iostream>
using namespace std;

int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix


{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

cout << "Following is the SAFE Sequence" << endl;


for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;

return (0);
}

Output:-
EXPERIMENT 12

Aim:- Simulate the working of following Memory allocation algorithms and compare their
performance on the basis of different parameters.
a. First fit b. Best fit c. Worst fit

a)
Theory:-
In the first fit, the partition is allocated which is first sufficient from the top of Main Memory.

Code:-
#include<bits/stdc++.h>
using namespace std;

// Function to allocate memory to


// blocks as per First fit algorithm
void firstFit(int blockSize[], int m,
int processSize[], int n)
{
// Stores block id of the
// block allocated to a process
int allocation[n];

// Initially no block is assigned to any process


memset(allocation, -1, sizeof(allocation));

// pick each process and find suitable blocks


// according to its size ad assign to it
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
// allocate block j to p[i] process
allocation[i] = j;

// Reduce available memory in this block.


blockSize[j] -= processSize[i];
break;
}
}
}

cout << "\nProcess No.\tProcess Size\tBlock no.\n";


for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t"
<< processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(blockSize, m, processSize, n);

return 0 ;
}

Output:-

b)
Theory:-
Best fit allocates the process to a partition which is the smallest sufficient partition among the
free available partitions.
Code:-
#include<bits/stdc++.h>
using namespace std;

// Function to allocate memory to blocks as per Best fit


// algorithm
void bestFit(int blockSize[], int m, int processSize[], int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];

// Initially no block is assigned to any process


memset(allocation, -1, sizeof(allocation));

// pick each process and find suitable blocks


// according to its size ad assign to it
for (int i=0; i<n; i++)
{
// Find the best fit block for current process
int bestIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIdx == -1)
bestIdx = j;
else if (blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}

// If we could find a block for current process


if (bestIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = bestIdx;
// Reduce available memory in this block.
blockSize[bestIdx] -= processSize[i];
}
}

cout << "\nProcess No.\tProcess Size\tBlock no.\n";


for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);

bestFit(blockSize, m, processSize, n);

return 0 ;
}

Output:-

c)
Theory:-
Worst Fit allocates a process to the partition which is largest sufficient among the freely available
partitions available in the main memory. If a large process comes at a later stage, then memory
will not have space to accommodate it.
Code:-
#include<bits/stdc++.h>
using namespace std;

// Function to allocate memory to blocks as per worst fit


// algorithm
void worstFit(int blockSize[], int m, int processSize[],
int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];

// Initially no block is assigned to any process


memset(allocation, -1, sizeof(allocation));

// pick each process and find suitable blocks


// according to its size ad assign to it
for (int i=0; i<n; i++)
{
// Find the best fit block for current process
int wstIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}

// If we could find a block for current process


if (wstIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = wstIdx;
// Reduce available memory in this block.
blockSize[wstIdx] -= processSize[i];
}
}

cout << "\nProcess No.\tProcess Size\tBlock no.\n";


for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);

worstFit(blockSize, m, processSize, n);

return 0 ;
}
Output:-

EXPERIMENT 13
Aim:- Simulate the working of following Page replacement algorithms and compare their
performance on the basis of different parameters.
a. FCFS b. Optimal c. LRU d. Second chance

a)
Theory:-
This is the simplest page replacement algorithm. In this algorithm, the operating system keeps
track of all pages in the memory in a queue, the oldest page is in the front of the queue. When
a page needs to be replaced page in the front of the queue is selected for removal.

Code:-
#include<bits/stdc++.h>
using namespace std;

// Function to find page faults using FIFO


int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

// To store the pages in FIFO manner


queue<int> indexes;

// Start from initial page


int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (s.find(pages[i])==s.end())
{
// Insert the current page into the set
s.insert(pages[i]);

// increment page fault


page_faults++;

// Push the current page into the queue


indexes.push(pages[i]);
}
}

// If the set is full then need to perform FIFO


// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Store the first page in the
// queue to be used to find and
// erase the page from the set
int val = indexes.front();

// Pop the first page from the queue


indexes.pop();

// Remove the indexes page from the set


s.erase(val);

// insert the current page in the set


s.insert(pages[i]);

// push the current page into


// the queue
indexes.push(pages[i]);

// Increment page faults


page_faults++;
}
}
}

return page_faults;
}

// Driver code
int main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}

Output:-

b)
Theory:-

In this algorithm, pages are replaced which would not be used for the longest duration of time
in the future.

Code:-
#include <bits/stdc++.h>
using namespace std;

// Function to check whether a page exists


// in a frame or not
bool search(int key, vector<int>& fr)
{
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}

// Function to find the frame that will not be used


// recently in future after given index in pg[0..pn-1]
int predict(int pg[], vector<int>& fr, int pn, int index)
{
// Store the index of pages which are going
// to be used recently in future
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}

// If a page is never referenced in future,


// return it.
if (j == pn)
return i;
}

// If all of the frames were not in future,


// return any of them, we return 0. Otherwise
// we return res.
return (res == -1) ? 0 : res;
}

void optimalPage(int pg[], int pn, int fn)


{
// Create an array for given number of
// frames and initialize it as empty.
vector<int> fr;

// Traverse through page reference array


// and check for miss and hit.
int hit = 0;
for (int i = 0; i < pn; i++) {

// Page found in a frame : HIT


if (search(pg[i], fr)) {
hit++;
continue;
}

// Page not found in a frame : MISS

// If there is space available in frames.


if (fr.size() < fn)
fr.push_back(pg[i]);

// Find the page to be replaced.


else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
cout << "No. of hits = " << hit << endl;
cout << "No. of misses = " << pn - hit << endl;
}

// Driver Function
int main()
{
int pg[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };
int pn = sizeof(pg) / sizeof(pg[0]);
int fn = 4;
optimalPage(pg, pn, fn);
return 0;
}

Output:-

c)
Theory:-
In this algorithm page will be replaced which is least recently used.

Code:-
#include<bits/stdc++.h>
using namespace std;
// Function to find page faults using indexes
int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

// To store least recently used indexes


// of pages.
unordered_map<int, int> indexes;

// Start from initial page


int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (s.find(pages[i])==s.end())
{
s.insert(pages[i]);

// increment page fault


page_faults++;
}

// Store the recently used index of


// each page
indexes[pages[i]] = i;
}

// If the set is full then need to perform lru


// i.e. remove the least recently used page
// and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Find the least recently used pages
// that is present in the set
int lru = INT_MAX, val;
for (auto it=s.begin(); it!=s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}

// Remove the indexes page


s.erase(val);

// insert the current page


s.insert(pages[i]);

// Increment page faults


page_faults++;
}

// Update the current page index


indexes[pages[i]] = i;
}
}

return page_faults;
}

// Driver code
int main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}

Output:-

d)
Theory:-
In the Second Chance page replacement policy, the candidate pages for removal are considered
in a round robin matter, and a page that has been accessed between consecutive considerations
will not be replaced. The page replaced is the one that, when considered in a round robin matter,
has not been accessed since its last consideration.

Code:-
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;

// If page found, updates the second chance bit to true


static bool findAndUpdate(int x,int arr[],
bool second_chance[],int frames)

{
int i;

for(i = 0; i < frames; i++)


{

if(arr[i] == x)
{
// Mark that the page deserves a second chance
second_chance[i] = true;

// Return 'true', that is there was a hit


// and so there's no need to replace any page
return true;
}
}

// Return 'false' so that a page for replacement is selected


// as he reuested page doesn't exist in memory
return false;

// Updates the page in memory and returns the pointer


static int replaceAndUpdate(int x,int arr[],
bool second_chance[],int frames,int pointer)
{
while(true)
{

// We found the page to replace


if(!second_chance[pointer])
{
// Replace with new page
arr[pointer] = x;

// Return updated pointer


return (pointer + 1) % frames;
}

// Mark it 'false' as it got one chance


// and will be replaced next time unless accessed again
second_chance[pointer] = false;

//Pointer is updated in round robin manner


pointer = (pointer + 1) % frames;
}
}

static void printHitsAndFaults(string reference_string,


int frames)
{
int pointer, i, l=0, x, pf;

//initially we consider frame 0 is to be replaced


pointer = 0;

//number of page faults


pf = 0;

// Create a array to hold page numbers


int arr[frames];

// No pages initially in frame,


// which is indicated by -1
memset(arr, -1, sizeof(arr));

// Create second chance array.


// Can also be a byte array for optimizing memory
bool second_chance[frames];

// Split the string into tokens,


// that is page numbers, based on space

string str[100];
string word = "";
for (auto x : reference_string)
{
if (x == ' ')
{
str[l]=word;
word = "";
l++;
}
else
{
word = word + x;
}
}
str[l] = word;
l++;
// l=the length of array

for(i = 0; i < l; i++)


{
x = stoi(str[i]);

// Finds if there exists a need to replace


// any page at all
if(!findAndUpdate(x,arr,second_chance,frames))
{
// Selects and updates a victim page
pointer = replaceAndUpdate(x,arr,
second_chance,frames,pointer);

// Update page faults


pf++;
}
}
cout << "Total page faults were " << pf << "\n";
}

// Driver code
int main()
{
string reference_string = "";
int frames = 0;

// Test 1:
reference_string = "0 4 1 4 2 4 3 4 2 4 0 4 1 4 2 4 3 4";
frames = 3;

// Output is 9
printHitsAndFaults(reference_string,frames);

// Test 2:
reference_string = "2 5 10 1 2 2 6 9 1 2 10 2 6 1 2 1 6 9 5 1";
frames = 4;

// Output is 11
printHitsAndFaults(reference_string,frames);
return 0;
}
Output:-

You might also like