Assignment 10
Output (Assignment 10)
Code (Assignment 10)
C Code
#include <stdio.h>
void reset_blocks(int source[], int dest[], int n) {
for (int i = 0; i < n; i++) {
dest[i] = source[i];
}
}
void first_fit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\n--- First Fit Allocation ---\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
void best_fit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1) {
bestIdx = j;
} else if (blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\n--- Best Fit Allocation ---\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
int originalBlockSize[m], blockSize[m];
printf("Enter the size of each block:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &originalBlockSize[i]);
}
printf("\nEnter the number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the size of each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
reset_blocks(originalBlockSize, blockSize, m);
first_fit(blockSize, m, processSize, n);
reset_blocks(originalBlockSize, blockSize, m);
best_fit(blockSize, m, processSize, n);
return 0;
}
Assignment 11
Output (Assignment 11)
Code (Assignment 11)
C Code
#include <stdio.h>
void reset_blocks(int source[], int dest[], int n) {
for (int i = 0; i < n; i++) {
dest[i] = source[i];
}
}
void next_fit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
int id = 0;
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int count = 0;
while (count < m) {
if (blockSize[id] >= processSize[i]) {
allocation[i] = id;
blockSize[id] -= processSize[i];
break;
}
id = (id + 1) % m;
count++;
}
}
printf("\n--- Next Fit Allocation ---\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
void worst_fit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1) {
worstIdx = j;
} else if (blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("\n--- Worst Fit Allocation ---\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
int originalBlockSize[m], blockSize[m];
printf("Enter the size of each block:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &originalBlockSize[i]);
}
printf("\nEnter the number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the size of each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
reset_blocks(originalBlockSize, blockSize, m);
next_fit(blockSize, m, processSize, n);
reset_blocks(originalBlockSize, blockSize, m);
worst_fit(blockSize, m, processSize, n);
return 0;
}
Assignment 12
Output (Assignment 12)
Code (Assignment 12)
Bash Code
#!/bin/bash
declare -A alloc
declare -A max
declare -A need
declare -a avail
declare -a work
declare -a finish
declare -a safeSeq
echo "--- Banker's Algorithm Setup ---"
read -p "Enter number of processes: " n
read -p "Enter number of resources: " m
echo ""
echo "Enter Allocation Matrix:"
for ((i=0; i<n; i++)); do
for ((j=0; j<m; j++)); do
read -p "Process P$i, Resource R$j allocated: " val
alloc[$i,$j]=$val
done
done
echo ""
echo "Enter Max Matrix:"
for ((i=0; i<n; i++)); do
for ((j=0; j<m; j++)); do
read -p "Process P$i, Resource R$j max need: " val
max[$i,$j]=$val
done
done
echo ""
echo "Enter Available Resources:"
for ((j=0; j<m; j++)); do
read -p "Resource R$j initially available: " val
avail[$j]=$val
work[$j]=$val
done
for ((i=0; i<n; i++)); do
finish[$i]=0
done
echo " "
printf "Process\t Allocation\t Max\t\t Need\n"
for ((i=0; i<n; i++)); do
printf "P$i\t "
for ((j=0; j<m; j++)); do
printf "%s " "${alloc[$i,$j]}"
done
printf "\t "
for ((j=0; j<m; j++)); do
printf "%s " "${max[$i,$j]}"
done
printf "\t "
for ((j=0; j<m; j++)); do
need_val=$(( ${max[$i,$j]} - ${alloc[$i,$j]} ))
need[$i,$j]=$need_val
printf "%s " "$need_val"
done
printf "\n"
done
echo " "
count=0
while [ $count -lt $n ]; do
found=false
for ((i=0; i<n; i++)); do
if [ ${finish[$i]} -eq 0 ]; then
can_allocate=true
for ((j=0; j<m; j++)); do
if [ ${need[$i,$j]} -gt ${work[$j]} ]; then
can_allocate=false
break
fi
done
if [ "$can_allocate" = true ]; then
for ((j=0; j<m; j++)); do
work[$j]=$(( ${work[$j]} + ${alloc[$i,$j]} ))
done
safeSeq[$count]=$i
finish[$i]=1
found=true
((count++))
break
fi
fi
done
if [ "$found" = false ]; then
echo "System is in an UNSAFE state. Deadlock possible."
exit 1
fi
done
echo "System is in a SAFE state."
echo -n "Safe Sequence: "
for ((i=0; i<n; i++)); do
echo -n "P${safeSeq[$i]} "
if [ $i -lt $((n-1)) ]; then echo -n "-> "; fi
done
echo ""
Assignment 13
Output (Assignment 13)
Code (Assignment 13)
#include <bits/stdc++.h>
using namespace std;
int fifo(vector<int> pages, int frames) {
vector<int> f(frames, -1);
int pointer = 0, faults = 0;
for (int p : pages) {
bool found = false;
for (int x : f)
if (x == p) { found = true; break; }
if (!found) {
f[pointer] = p;
pointer = (pointer + 1) % frames;
faults++;
}
}
return faults;
}
int lru(vector<int> pages, int frames) {
vector<int> f(frames, -1);
unordered_map<int, int> lastUsed;
int faults = 0, time = 0;
for (int p : pages) {
time++;
bool found = false;
for (int x : f)
if (x == p) { found = true; break; }
if (!found) {
int replaceIndex = -1;
for (int i = 0; i < frames; i++) {
if (f[i] == -1) { replaceIndex = i; break; }
}
if (replaceIndex == -1) {
int lruPage = INT_MAX;
for (int i = 0; i < frames; i++) {
if (lastUsed[f[i]] < lruPage) {
lruPage = lastUsed[f[i]];
replaceIndex = i;
}
}
}
f[replaceIndex] = p;
faults++;
}
lastUsed[p] = time;
}
return faults;
}
int optimal(vector<int> pages, int frames) {
vector<int> f(frames, -1);
int faults = 0;
for (int i = 0; i < [Link](); i++) {
int p = pages[i];
bool found = false;
for (int x : f)
if (x == p) { found = true; break; }
if (!found) {
int replaceIndex = -1;
for (int j = 0; j < frames; j++) {
if (f[j] == -1) { replaceIndex = j; break; }
}
if (replaceIndex == -1) {
int farthest = -1;
for (int j = 0; j < frames; j++) {
int nextUse = INT_MAX;
for (int k = i + 1; k < [Link](); k++) {
if (pages[k] == f[j]) {
nextUse = k; break;
}
}
if (nextUse > farthest) {
farthest = nextUse;
replaceIndex = j;
}
}
}
f[replaceIndex] = p;
faults++;
}
}
return faults;
}
int mru(vector<int> pages, int frames) {
vector<int> f(frames, -1);
unordered_map<int, int> lastUsed;
int faults = 0, time = 0;
for (int p : pages) {
time++;
bool found = false;
for (int x : f)
if (x == p) { found = true; break; }
if (!found) {
int replaceIndex = -1;
for (int i = 0; i < frames; i++) {
if (f[i] == -1) { replaceIndex = i; break; }
}
if (replaceIndex == -1) {
int mostRecent = -1;
for (int i = 0; i < frames; i++) {
if (lastUsed[f[i]] > mostRecent) {
mostRecent = lastUsed[f[i]];
replaceIndex = i;
}
}
}
f[replaceIndex] = p;
faults++;
}
lastUsed[p] = time;
}
return faults;
}
int main() {
int frames, n;
cout << "Enter number of frames: ";
cin >> frames;
cout << "Enter number of pages: ";
cin >> n;
vector<int> pages(n);
cout << "Enter page numbers: ";
for (int i = 0; i < n; i++) cin >> pages[i];
cout << "\n--- Page Fault Count ---\n";
cout << "FIFO: " << fifo(pages, frames) << endl;
cout << "LRU : " << lru(pages, frames) << endl;
cout << "Optimal: " << optimal(pages, frames) << endl;
cout << "MRU: " << mru(pages, frames) << endl;
return 0;
}
Output (Assignment 14)
Code (Assignment 14)
#include <bits/stdc++.h>
using namespace std;
int FCFS(vector<int> req, int head) {
int total = 0;
for (int track : req) {
total += abs(track - head);
head = track;
}
return total;
}
int SCAN(vector<int> req, int head, int cylinders, string direction) {
int total = 0;
vector<int> left, right;
left.push_back(0);
right.push_back(cylinders - 1);
for (int track : req) {
if (track < head)
left.push_back(track);
else
right.push_back(track);
}
sort([Link](), [Link]());
sort([Link](), [Link]());
if (direction == "right") {
for (int track : right) {
total += abs(track - head);
head = track;
}
for (int track : left) {
total += abs(track - head);
head = track;
}
}
else {
reverse([Link](), [Link]());
for (int track : left) {
total += abs(track - head);
head = track;
}
for (int track : right) {
total += abs(track - head);
head = track;
}
}
return total;
}
int main() {
int cylinders, n, head;
cout << "Enter total number of cylinders: ";
cin >> cylinders;
cout << "Enter number of requests: ";
cin >> n;
vector<int> req(n);
cout << "Enter request queue: ";
for (int i = 0; i < n; i++)
cin >> req[i];
cout << "Enter initial head position: ";
cin >> head;
int totalFCFS = FCFS(req, head);
cout << "\nTotal head movement (FCFS): " << totalFCFS << " cylinders\n";
string direction;
cout << "\nEnter initial direction (left/right): ";
cin >> direction;
int totalSCAN = SCAN(req, head, cylinders, direction);
cout << "Total head movement (SCAN): " << totalSCAN << " cylinders\n";
return 0;
}