KEMBAR78
Os File | PDF | Namespace | Algorithms And Data Structures
0% found this document useful (0 votes)
6 views29 pages

Os File

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)
6 views29 pages

Os File

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/ 29

INDEX

S.NO NAME OF EXPERIMENT DATE REMARK SIGNATUR


E
S
1 Write a program to illustrate First
Come First Serve Scheduling
algorithm.
2 Write a program to illustrate Shortest
Job First Scheduling algorithm.

3 Write a program to illustrate Priority


Scheduling algorithm.

4 Write a program to illustrate Round


Robin Scheduling algorithm.

5 Write a program to illustrate Least


Recent Used Page replacement
algorithm.
6 Write a program to illustrate First In
First Out Page replacement
algorithm.
7 Write a program to illustrate
Optimal Page replacement
algorithm.
8 Write a program to illustrate First Fit
Memory Management.

9 Write a program to illustrate Best Fit


Memory Management.

10 Write a program to illustrate Worst


Fit Memory Management.

PROGRAM : 1
AIM : Write a program to illustrate First Come First Serve Scheduling.

CODE :

#include <iostream>
using namespace std;
Shree bhagwan 0
03513302723
int main() {
int n, bt[20], wt[20], tat[20], i, j;
float avwt = 0, avtat = 0;

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

// Calculate 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\n";

// Calculate turnaround time and average times


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

avwt /= n;
avtat /= n;

cout << "\nAverage Waiting Time: " << avwt;


cout << "\nAverage Turnaround Time: " << avtat;

return 0;
}

OUTPUT

Shree bhagwan 1
03513302723
PROGRAM : 2
AIM : Write a program to illustrate Shortest Job First Scheduling.

Shree bhagwan 2
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int bt[20], p[20], wt[20], tat[20];
int n, i, j, pos, temp, total = 0;
float avg_wt, avg_tat;

cout << "Enter Total Number of Processes: ";


cin >> n;

cout << "\nEnter Burst Time for each process\n";


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

// Sorting according to Burst Time (SJF logic)


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

// Swap burst time


temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;

// Swap process number


temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}

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

// 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 = (float)total / n;
total = 0;

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

Shree bhagwan 3
03513302723
// Calculate turnaround time and print details
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total += tat[i];
cout << "P[" << p[i] << "]\t " << bt[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\n";
}

avg_tat = (float)total / n;

cout << "\nAverage Waiting Time = " << avg_wt;


cout << "\nAverage Turnaround Time = " << avg_tat;

return 0;
}

OUTPUT

Shree bhagwan 4
03513302723
PROGRAM : 3
AIM : Write a program to illustrate Priority Scheduling algorithm.
Shree bhagwan 5
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int bt[20], p[20], wt[20], tat[20], pr[20];
int i, j, n, total = 0, pos, temp;
float avg_wt, avg_tat;

cout << "Enter Total Number of Processes: ";


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

// Sorting based on priority (lower value = higher priority)


for (i = 0; i < n; i++) {
pos = i;
for (j = i + 1; j < n; j++) {
if (pr[j] < pr[pos])
pos = j;
}

// Swap priority
temp = pr[i];
pr[i] = pr[pos];
pr[pos] = temp;

// Swap burst time


temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;

// Swap process number


temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}

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

// Calculate waiting time


for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++)
Shree bhagwan 6
03513302723
wt[i] += bt[j];
total += wt[i];
}

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

cout << "\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n";

// Calculate turnaround time


for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total += tat[i];
cout << "P[" << p[i] << "]\t " << bt[i] << "\t\t" << pr[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\n";
}

avg_tat = (float)total / n;

cout << "\nAverage Waiting Time = " << avg_wt;


cout << "\nAverage Turnaround Time = " << avg_tat;

return 0;
}

OUTPUT

Shree bhagwan 7
03513302723
PROGRAM : 4
AIM : Write a program to illustrate Round Robin Scheduling algorithm.
Shree bhagwan 8
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int num, quantum;
int bt[10], wt[10], tat[10], rem_bt[10];
int time = 0;
float avg_wt = 0, avg_tat = 0;

cout << "Enter number of processes (MAX 10): ";


cin >> num;

cout << "Enter burst time for each process:\n";


for (int i = 0; i < num; i++) {
cout << "P[" << i + 1 << "]: ";
cin >> bt[i];
rem_bt[i] = bt[i]; // remaining burst time
wt[i] = 0;
}

cout << "\nEnter time quantum: ";


cin >> quantum;

int rp = num; // remaining processes

cout << "\nGantt Chart:\n";


cout << "0";

while (rp != 0) {
bool done = true;
for (int i = 0; i < num; i++) {
if (rem_bt[i] > 0) {
done = false; // there is a pending process

if (rem_bt[i] > quantum) {


time += quantum;
rem_bt[i] -= quantum;
cout << " | P[" << i + 1 << "] | " << time;
} else {
time += rem_bt[i];
wt[i] = time - bt[i]; // waiting time = finish time - burst time
rem_bt[i] = 0;
cout << " | P[" << i + 1 << "] | " << time;
rp--;
}
}
}
if (done) break;
}

// Calculate turnaround time and averages


Shree bhagwan 9
03513302723
for (int i = 0; i < num; i++) {
tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}

avg_wt /= num;
avg_tat /= num;

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


for (int i = 0; i < num; i++) {
cout << "P[" << i + 1 << "]\t " << bt[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\n";
}

cout << "\nAverage Waiting Time = " << avg_wt;


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

return 0;
}

OUTPUT

Shree bhagwan 10
03513302723
PROGRAM : 5
AIM : Write a program to illustrate Least Recent Used Page replacement algorithm.
Shree bhagwan 11
03513302723
CODE :

#include <iostream>
using namespace std;

// Function to find the least recently used page


int findLRU(int counter[], int nFrames) {
int minimum = counter[0], pos = 0;
for (int i = 1; i < nFrames; i++) {
if (counter[i] < minimum) {
minimum = counter[i];
pos = i;
}
}
return pos;
}

int main() {
int pageString[50], n, frames[10], counter[10];
int recent = 0; // keeps track of usage time
int pageFault = 0, nFrames;

cout << "Enter the number of pages: ";


cin >> n;

cout << "Enter the page reference string: ";


for (int i = 0; i < n; i++)
cin >> pageString[i];

cout << "Enter the number of frames: ";


cin >> nFrames;

// Initialize frames and counters


for (int i = 0; i < nFrames; i++) {
frames[i] = -1; // empty frame
counter[i] = 0;
}

for (int i = 0; i < n; i++) {


int flag = 0;

// Check if page is already in frame


for (int j = 0; j < nFrames; j++) {
if (frames[j] == pageString[i]) {
flag = 1;
counter[j] = recent++;
break;
}
}

// If page not found


if (flag == 0) {
// Check for empty frame
Shree bhagwan 12
03513302723
int placed = 0;
for (int j = 0; j < nFrames; j++) {
if (frames[j] == -1) {
frames[j] = pageString[i];
counter[j] = recent++;
pageFault++;
placed = 1;
break;
}
}

// If no empty frame, replace LRU page


if (!placed) {
int pos = findLRU(counter, nFrames);
frames[pos] = pageString[i];
counter[pos] = recent++;
pageFault++;
}
}

// Print frame status


cout << "\nAfter inserting " << pageString[i] << ": ";
for (int j = 0; j < nFrames; j++) {
if (frames[j] != -1)
cout << frames[j] << " ";
else
cout << "- ";
}
}

cout << "\n\nTotal Page Faults: " << pageFault << endl;

return 0;
}

OUTPUT

Shree bhagwan 13
03513302723
PROGRAM : 6
AIM : Write a program to illustrate First In First Out Page replacement.
Shree bhagwan 14
03513302723
CODE :

#include <iostream>
using namespace std;

int getReplacePosition(int counter[], int nFrames) {


int max = counter[0];
int pos = 0;
for (int i = 1; i < nFrames; i++) {
if (counter[i] > max) {
max = counter[i];
pos = i;
}
}
return pos;
}

int main() {
int pages[20], frames[10], counter[10];
int nPages, nFrames, pageFault = 0;

cout << "Enter the number of pages (MAX 20): ";


cin >> nPages;

cout << "Enter the number of frames (MAX 10): ";


cin >> nFrames;

cout << "Enter the Page reference string: ";


for (int i = 0; i < nPages; i++) {
cin >> pages[i];
}

// Initialize frames and counters


for (int i = 0; i < nFrames; i++) {
frames[i] = -1; // -1 means empty
counter[i] = 0; // counter to track age
}

for (int i = 0; i < nPages; i++) {


int flag = 0;

// Check if page already in frames


for (int j = 0; j < nFrames; j++) {
if (frames[j] == pages[i]) {
flag = 1;
break;
}
}

// If page is not in frames


if (flag == 0) {
pageFault++;
int emptyIndex = -1;
Shree bhagwan 15
03513302723
// Find an empty frame
for (int j = 0; j < nFrames; j++) {
if (frames[j] == -1) {
emptyIndex = j;
break;
}
}

if (emptyIndex != -1) {
frames[emptyIndex] = pages[i];
counter[emptyIndex] = 1;
} else {
int pos = getReplacePosition(counter, nFrames);
frames[pos] = pages[i];
counter[pos] = 1;
}
}

// Increase counter for all frames except the newly inserted one
for (int k = 0; k < nFrames; k++) {
if (frames[k] != -1) counter[k]++;
}

// Print current frame status


cout << "\nFrames: ";
for (int j = 0; j < nFrames; j++) {
if (frames[j] == -1) cout << "- ";
else cout << frames[j] << " ";
}
}

cout << "\nTotal Page Faults: " << pageFault << endl;
return 0;
}

OUTPUT

Shree bhagwan 16
03513302723
PRAGRAM : 7
AIM : Write a program to illustrate Optimal Page replacement algorithm.
Shree bhagwan 17
03513302723
CODE :

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

void printFrames(const vector<int> &frames) {


for (int f : frames) {
if (f == -1)
cout << setw(4) << "-";
else
cout << setw(4) << f;
}
}

int main() {
int nop, nof;

cout << "Enter the number of pages: ";


cin >> nop;

vector<int> pages(nop);
cout << "Enter the reference string: ";
for (int i = 0; i < nop; i++) {
cin >> pages[i];
}

cout << "Enter the number of frames: ";


cin >> nof;

if (nop <= 0 || nof <= 0) {


cout << "Invalid input! Pages and frames must be positive." << endl;
return 1;
}

vector<int> frames(nof, -1);


int hits = 0, faults = 0;

cout << "\nRef";


for (int i = 0; i < nof; i++) cout << setw(4) << "F" << i+1;
cout << setw(10) << "Result\n";
cout << "------------------------------------\n";

for (int i = 0; i < nop; i++) {


int currentPage = pages[i];
bool hit = false;

// Check if page is already in frame (Hit)


for (int f = 0; f < nof; f++) {
if (frames[f] == currentPage) {
hit = true;
hits++;
Shree bhagwan 18
03513302723
break;
}
}

if (!hit) {
faults++;

// Find empty frame first


bool placed = false;
for (int f = 0; f < nof; f++) {
if (frames[f] == -1) {
frames[f] = currentPage;
placed = true;
break;
}
}

// If no empty frame, use Optimal Replacement


if (!placed) {
int farthest = -1, victimIndex = -1;

for (int f = 0; f < nof; f++) {


int nextUse = -1;
for (int k = i + 1; k < nop; k++) {
if (frames[f] == pages[k]) {
nextUse = k;
break;
}
}

if (nextUse == -1) { // Not used again


victimIndex = f;
break;
}

if (nextUse > farthest) {


farthest = nextUse;
victimIndex = f;
}
}

frames[victimIndex] = currentPage;
}
}

// Print step
cout << setw(3) << currentPage;
printFrames(frames);
cout << setw(10) << (hit ? "Hit" : "Fault") << "\n";
}

cout << "\nTotal Hits: " << hits;


cout << "\nTotal Faults: " << faults;
cout << "\nHit Ratio: " << fixed << setprecision(2) << (float)hits / nop;
Shree bhagwan 19
03513302723
cout << "\nFault Ratio: " << fixed << setprecision(2) << (float)faults / nop << "\n";

return 0;
}

OUTPUT

Shree bhagwan 20
03513302723
PROGRAM : 8
AIM : Write a program to illustrate First Fit Memory Management.
Shree bhagwan 21
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int num_blocks, num_files;
int blocks[10], files[10];
int remain[10], fragments[10], fileAlloc[10];
int i, j;

// Initialize
for (i = 0; i < 10; i++) {
fragments[i] = 0;
fileAlloc[i] = -1;
remain[i] = 0;
}

cout << "\nEnter the No. of Blocks: ";


cin >> num_blocks;

cout << "\nEnter the sizes of blocks\n";


for (i = 0; i < num_blocks; i++) {
cout << "Block[" << i + 1 << "]: ";
cin >> blocks[i];
remain[i] = blocks[i];
}

cout << "\nEnter the No. of Files: ";


cin >> num_files;

cout << "\nEnter the sizes of files\n";


for (i = 0; i < num_files; i++) {
cout << "File[" << i + 1 << "]: ";
cin >> files[i];
}

// First Fit allocation


for (i = 0; i < num_files; i++) {
for (j = 0; j < num_blocks; j++) {
if (files[i] <= remain[j]) {
remain[j] -= files[i]; // reduce space in block
fragments[j]++; // count files in block
fileAlloc[i] = j; // record allocation
break;
}
}
}

// Output format
cout << "\nBLOCKS\tREMAIN\tFRAGMENTS\n";
for (i = 0; i < num_blocks; i++) {
cout << i + 1 << "\t" << remain[i] << "\t" << fragments[i] << "\n";
Shree bhagwan 22
03513302723
}

cout << "\nFiles which are not allocated memory\n";


for (i = 0; i < num_files; i++) {
if (fileAlloc[i] == -1) {
cout << "File " << i + 1 << "\n";
}
}

return 0;
}

OUTPUT

Shree bhagwan 23
03513302723
PROGRAM : 9
AIM : Write a program to illustrate Best Fit Memory Management.
Shree bhagwan 24
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int nBlocks, nProcess, blockSize[20], processSize[20];

cout << "Enter the number of blocks: ";


cin >> nBlocks;

cout << "Enter the number of processes: ";


cin >> nProcess;

cout << "Enter the size of " << nBlocks << " blocks:\n";
for (int i = 0; i < nBlocks; i++)
cin >> blockSize[i];

cout << "Enter the size of " << nProcess << " processes:\n";
for (int i = 0; i < nProcess; i++)
cin >> processSize[i];

for (int i = 0; i < nProcess; i++) {


int bestIdx = -1;
for (int j = 0; j < nBlocks; j++) {
if (processSize[i] <= blockSize[j]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}

if (bestIdx != -1) {
cout << "\nProcess " << i + 1 << " is allocated to Block " << bestIdx + 1;
blockSize[bestIdx] -= processSize[i];
} else {
cout << "\nProcess " << i + 1 << " cannot be allocated";
}

cout << "\nRemaining Block size\n";


for (int k = 0; k < nBlocks; k++) {
cout << blockSize[k] << " ";
}
cout << "\n";
}

return 0;
}

OUTPUT

Shree bhagwan 25
03513302723
PROGRAM : 10
AIM : Write a program to illustrate Worst Fit Memory Management.

Shree bhagwan 26
03513302723
CODE :

#include <iostream>
using namespace std;

int main() {
int nBlocks, nProcess, blockSize[20], processSize[20];

cout << "Enter the number of blocks: ";


cin >> nBlocks;

cout << "Enter the number of processes: ";


cin >> nProcess;

cout << "Enter the size of " << nBlocks << " blocks:\n";
for (int i = 0; i < nBlocks; i++)
cin >> blockSize[i];

cout << "Enter the size of " << nProcess << " processes:\n";
for (int i = 0; i < nProcess; i++)
cin >> processSize[i];

for (int i = 0; i < nProcess; i++) {


int worstIdx = -1;

// Find the largest block that fits the process


for (int j = 0; j < nBlocks; j++) {
if (processSize[i] <= blockSize[j]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}
if (worstIdx != -1) {
cout << "\nProcess " << i + 1 << " is allocated to Block " << worstIdx + 1;
blockSize[worstIdx] -= processSize[i];
} else {
cout << "\nProcess " << i + 1 << " can't be allocated";
}

// Print remaining block sizes


cout << "\nRemaining Block size\n";
for (int k = 0; k < nBlocks; k++) {
cout << blockSize[k] << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT

Shree bhagwan 27
03513302723
Shree bhagwan 28
03513302723

You might also like