mirror of
https://github.com/Manoj-HV30/sp-lab.git
synced 2026-05-16 19:35:27 +00:00
cooked 3
This commit is contained in:
@@ -0,0 +1,84 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define MAX 50
|
||||||
|
|
||||||
|
|
||||||
|
void fcfs(int requests[], int n, int head) {
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
printf("\n--- FCFS ---\n");
|
||||||
|
printf("Order: %d", head);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
total += abs(requests[i] - head);
|
||||||
|
head = requests[i];
|
||||||
|
printf(" -> %d", head);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nTotal Head Movement: %d\n", total);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── SCAN ──────────────────────────────────────────────────────────────────
|
||||||
|
void scan(int requests[], int n, int head, int disk_size) {
|
||||||
|
int total = 0;
|
||||||
|
int req[MAX];
|
||||||
|
|
||||||
|
// copy and sort requests
|
||||||
|
for (int i = 0; i < n; i++) req[i] = requests[i];
|
||||||
|
for (int i = 0; i < n - 1; i++)
|
||||||
|
for (int j = 0; j < n - i - 1; j++)
|
||||||
|
if (req[j] > req[j+1]) { int t = req[j]; req[j] = req[j+1]; req[j+1] = t; }
|
||||||
|
|
||||||
|
// find where head sits in sorted array
|
||||||
|
int pos = 0;
|
||||||
|
while (pos < n && req[pos] < head) pos++;
|
||||||
|
|
||||||
|
printf("\n--- SCAN (moving towards higher tracks first) ---\n");
|
||||||
|
printf("Order: %d", head);
|
||||||
|
|
||||||
|
// go right (higher) first
|
||||||
|
for (int i = pos; i < n; i++) {
|
||||||
|
total += abs(req[i] - head);
|
||||||
|
head = req[i];
|
||||||
|
printf(" -> %d", head);
|
||||||
|
}
|
||||||
|
if (head != disk_size - 1) {
|
||||||
|
|
||||||
|
total += abs((disk_size - 1) - head);
|
||||||
|
head = disk_size - 1;
|
||||||
|
|
||||||
|
printf(" -> %d", head);
|
||||||
|
}
|
||||||
|
|
||||||
|
// go left (lower)
|
||||||
|
for (int i = pos - 1; i >= 0; i--) {
|
||||||
|
total += abs(req[i] - head);
|
||||||
|
head = req[i];
|
||||||
|
printf(" -> %d", head);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nTotal Head Movement: %d\n", total);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MAIN ──────────────────────────────────────────────────────────────────
|
||||||
|
int main() {
|
||||||
|
int requests[MAX], n, head, disk_size;
|
||||||
|
|
||||||
|
printf("Enter number of requests: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter requests: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
scanf("%d", &requests[i]);
|
||||||
|
|
||||||
|
printf("Enter initial head position: ");
|
||||||
|
scanf("%d", &head);
|
||||||
|
|
||||||
|
printf("Enter disk size: ");
|
||||||
|
scanf("%d", &disk_size);
|
||||||
|
|
||||||
|
fcfs(requests, n, head);
|
||||||
|
scan(requests, n, head, disk_size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// ── PAGING ────────────────────────────────────────────────────────────────
|
||||||
|
void paging() {
|
||||||
|
int page_size, num_pages;
|
||||||
|
printf("\n--- PAGING ---\n");
|
||||||
|
printf("Enter page size: ");
|
||||||
|
scanf("%d", &page_size);
|
||||||
|
printf("Enter number of pages: ");
|
||||||
|
scanf("%d", &num_pages);
|
||||||
|
|
||||||
|
int page_table[num_pages];
|
||||||
|
printf("Enter frame number for each page:\n");
|
||||||
|
for (int i = 0; i < num_pages; i++) {
|
||||||
|
printf(" Page %d -> Frame: ", i);
|
||||||
|
scanf("%d", &page_table[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int logical;
|
||||||
|
printf("Enter logical address: ");
|
||||||
|
scanf("%d", &logical);
|
||||||
|
|
||||||
|
int page = logical / page_size; // which page
|
||||||
|
int offset = logical % page_size; // offset within page
|
||||||
|
|
||||||
|
if (page >= num_pages) {
|
||||||
|
printf("Invalid! Page %d does not exist.\n", page);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int frame = page_table[page];
|
||||||
|
int physical = frame * page_size + offset;
|
||||||
|
|
||||||
|
printf("\nPage Number : %d\n", page);
|
||||||
|
printf("Offset : %d\n", offset);
|
||||||
|
printf("Frame Number: %d\n", frame);
|
||||||
|
printf("Physical Address: %d\n", physical);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── SEGMENTATION ──────────────────────────────────────────────────────────
|
||||||
|
void segmentation() {
|
||||||
|
int num_seg;
|
||||||
|
printf("\n--- SEGMENTATION ---\n");
|
||||||
|
printf("Enter number of segments: ");
|
||||||
|
scanf("%d", &num_seg);
|
||||||
|
|
||||||
|
int base[num_seg], limit[num_seg];
|
||||||
|
printf("Enter base and limit for each segment:\n");
|
||||||
|
for (int i = 0; i < num_seg; i++) {
|
||||||
|
printf(" Segment %d -> Base: ", i);
|
||||||
|
scanf("%d", &base[i]);
|
||||||
|
printf(" Segment %d -> Limit: ", i);
|
||||||
|
scanf("%d", &limit[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int seg, offset;
|
||||||
|
printf("Enter segment number: ");
|
||||||
|
scanf("%d", &seg);
|
||||||
|
printf("Enter offset: ");
|
||||||
|
scanf("%d", &offset);
|
||||||
|
|
||||||
|
if (seg >= num_seg) {
|
||||||
|
printf("Invalid! Segment %d does not exist.\n", seg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset >= limit[seg]) {
|
||||||
|
printf("Segmentation Fault! Offset %d exceeds limit %d.\n", offset, limit[seg]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int physical = base[seg] + offset;
|
||||||
|
printf("\nSegment Base : %d\n", base[seg]);
|
||||||
|
printf("Segment Limit : %d\n", limit[seg]);
|
||||||
|
printf("Offset : %d\n", offset);
|
||||||
|
printf("Physical Address: %d\n", physical);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MAIN ──────────────────────────────────────────────────────────────────
|
||||||
|
int main() {
|
||||||
|
int ch;
|
||||||
|
while (1) {
|
||||||
|
printf("\n1. Paging");
|
||||||
|
printf("\n2. Segmentation");
|
||||||
|
printf("\n3. Exit");
|
||||||
|
printf("\nEnter choice: ");
|
||||||
|
scanf("%d", &ch);
|
||||||
|
|
||||||
|
if (ch == 1) paging();
|
||||||
|
else if (ch == 2) segmentation();
|
||||||
|
else if (ch == 3) break;
|
||||||
|
else printf("Invalid choice\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX 10
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n, r;
|
||||||
|
printf("Enter number of processes: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter number of resources: ");
|
||||||
|
scanf("%d", &r);
|
||||||
|
|
||||||
|
int alloc[MAX][MAX], max[MAX][MAX], need[MAX][MAX];
|
||||||
|
int avail[MAX], finish[MAX] = {0};
|
||||||
|
int safeSeq[MAX];
|
||||||
|
|
||||||
|
printf("Enter Allocation matrix:\n");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
for (int j = 0; j < r; j++)
|
||||||
|
scanf("%d", &alloc[i][j]);
|
||||||
|
|
||||||
|
printf("Enter Max matrix:\n");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
for (int j = 0; j < r; j++)
|
||||||
|
scanf("%d", &max[i][j]);
|
||||||
|
|
||||||
|
printf("Enter Available resources:\n");
|
||||||
|
for (int j = 0; j < r; j++)
|
||||||
|
scanf("%d", &avail[j]);
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
for (int j = 0; j < r; j++)
|
||||||
|
need[i][j] = max[i][j] - alloc[i][j];
|
||||||
|
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
while (count < n) {
|
||||||
|
int found = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (finish[i]) continue;
|
||||||
|
|
||||||
|
|
||||||
|
int ok = 1;
|
||||||
|
for (int j = 0; j < r; j++) {
|
||||||
|
if (need[i][j] > avail[j]) {
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ok) {
|
||||||
|
|
||||||
|
for (int j = 0; j < r; j++)
|
||||||
|
avail[j] += alloc[i][j];
|
||||||
|
finish[i] = 1;
|
||||||
|
safeSeq[count++] = i;
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count == n) {
|
||||||
|
printf("\nSystem is in SAFE state\n");
|
||||||
|
printf("Safe sequence: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
printf("P%d ", safeSeq[i]);
|
||||||
|
printf("\n");
|
||||||
|
} else {
|
||||||
|
printf("\nSystem is in UNSAFE state (Deadlock possible)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX 25
|
||||||
|
|
||||||
|
void firstFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
alloc[i] = j;
|
||||||
|
blocks[j] -= process[i];
|
||||||
|
break; // first block that fits, stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- First Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bestFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int bestIdx = -1;
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
// pick the smallest block that still fits
|
||||||
|
if (bestIdx == -1 || blocks[j] < blocks[bestIdx])
|
||||||
|
bestIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestIdx != -1) {
|
||||||
|
alloc[i] = bestIdx;
|
||||||
|
blocks[bestIdx] -= process[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- Best Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void worstFit(int blocks[], int m, int process[], int n) {
|
||||||
|
int alloc[MAX];
|
||||||
|
memset(alloc, -1, sizeof(alloc));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int worstIdx = -1;
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (blocks[j] >= process[i]) {
|
||||||
|
// pick the largest block
|
||||||
|
if (worstIdx == -1 || blocks[j] > blocks[worstIdx])
|
||||||
|
worstIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (worstIdx != -1) {
|
||||||
|
alloc[i] = worstIdx;
|
||||||
|
blocks[worstIdx] -= process[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n--- Worst Fit ---\n");
|
||||||
|
printf("Process\tSize\tBlock\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (alloc[i] == -1)
|
||||||
|
printf("P%d\t%d\tNot Allocated\n", i+1, process[i]);
|
||||||
|
else
|
||||||
|
printf("P%d\t%d\tBlock %d\n", i+1, process[i], alloc[i]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int m, n;
|
||||||
|
int blocks[MAX], process[MAX];
|
||||||
|
int b1[MAX], b2[MAX], b3[MAX];
|
||||||
|
|
||||||
|
printf("Enter number of memory blocks: ");
|
||||||
|
scanf("%d", &m);
|
||||||
|
printf("Enter block sizes: ");
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
scanf("%d", &blocks[i]);
|
||||||
|
b1[i] = b2[i] = b3[i] = blocks[i]; // each strategy gets a fresh copy
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Enter number of processes: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter process sizes: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
scanf("%d", &process[i]);
|
||||||
|
|
||||||
|
firstFit(b1, m, process, n);
|
||||||
|
bestFit (b2, m, process, n);
|
||||||
|
worstFit(b3, m, process, n);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX 50
|
||||||
|
|
||||||
|
// ── FIFO ──────────────────────────────────────────────────────────────────
|
||||||
|
void fifo(int pages[], int n, int frames) {
|
||||||
|
int mem[MAX], front = 0, faults = 0;
|
||||||
|
memset(mem, -1, sizeof(mem));
|
||||||
|
|
||||||
|
printf("\n--- FIFO ---\n");
|
||||||
|
printf("Page\tFrames\t\t\tFault\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
// check if page already in memory
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == pages[i]) { found = 1; break; }
|
||||||
|
|
||||||
|
printf("%d\t", pages[i]);
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
mem[front] = pages[i]; // replace oldest page
|
||||||
|
front = (front + 1) % frames;
|
||||||
|
faults++;
|
||||||
|
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == -1) printf("- "); else printf("%d ", mem[j]);
|
||||||
|
printf("\t\tFault");
|
||||||
|
} else {
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == -1) printf("- "); else printf("%d ", mem[j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Total Page Faults: %d\n", faults);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── OPTIMAL ───────────────────────────────────────────────────────────────
|
||||||
|
void optimal(int pages[], int n, int frames) {
|
||||||
|
int mem[MAX], faults = 0, count = 0;
|
||||||
|
memset(mem, -1, sizeof(mem));
|
||||||
|
|
||||||
|
printf("\n--- Optimal ---\n");
|
||||||
|
printf("Page\tFrames\t\t\tFault\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
// check if page already in memory
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == pages[i]) { found = 1; break; }
|
||||||
|
|
||||||
|
printf("%d\t", pages[i]);
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
if (count < frames) {
|
||||||
|
// frames not full yet, just fill
|
||||||
|
mem[count++] = pages[i];
|
||||||
|
} else {
|
||||||
|
// find which page in memory is used farthest in future
|
||||||
|
int replaceIdx = -1, farthest = -1;
|
||||||
|
for (int j = 0; j < frames; j++) {
|
||||||
|
int nextUse = n; // assume never used again
|
||||||
|
for (int k = i + 1; k < n; k++) {
|
||||||
|
if (pages[k] == mem[j]) { nextUse = k; break; }
|
||||||
|
}
|
||||||
|
if (nextUse > farthest) {
|
||||||
|
farthest = nextUse;
|
||||||
|
replaceIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mem[replaceIdx] = pages[i];
|
||||||
|
}
|
||||||
|
faults++;
|
||||||
|
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == -1) printf("- "); else printf("%d ", mem[j]);
|
||||||
|
printf("\t\tFault");
|
||||||
|
} else {
|
||||||
|
for (int j = 0; j < frames; j++)
|
||||||
|
if (mem[j] == -1) printf("- "); else printf("%d ", mem[j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Total Page Faults: %d\n", faults);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MAIN ──────────────────────────────────────────────────────────────────
|
||||||
|
int main() {
|
||||||
|
int pages[MAX], n, frames;
|
||||||
|
|
||||||
|
printf("Enter number of pages in reference string: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter reference string: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
scanf("%d", &pages[i]);
|
||||||
|
|
||||||
|
printf("Enter number of frames: ");
|
||||||
|
scanf("%d", &frames);
|
||||||
|
|
||||||
|
fifo(pages, n, frames);
|
||||||
|
optimal(pages, n, frames);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX 50
|
||||||
|
|
||||||
|
int stack[MAX], top = -1;
|
||||||
|
|
||||||
|
// check if page is in stack
|
||||||
|
int inStack(int page, int frames) {
|
||||||
|
for (int i = top; i > top - frames && i >= 0; i--)
|
||||||
|
if (stack[i] == page) return i;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int pages[MAX], n, frames;
|
||||||
|
|
||||||
|
printf("Enter number of pages: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
printf("Enter reference string: ");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
scanf("%d", &pages[i]);
|
||||||
|
printf("Enter number of frames: ");
|
||||||
|
scanf("%d", &frames);
|
||||||
|
|
||||||
|
int faults = 0;
|
||||||
|
|
||||||
|
printf("\nPage\tFrames\t\t\tFault\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int pos = inStack(pages[i], frames);
|
||||||
|
|
||||||
|
printf("%d\t", pages[i]);
|
||||||
|
|
||||||
|
if (pos == -1) {
|
||||||
|
// page not found — fault
|
||||||
|
stack[++top] = pages[i];
|
||||||
|
faults++;
|
||||||
|
|
||||||
|
// print current frames (top 'frames' elements)
|
||||||
|
int start = top - frames + 1;
|
||||||
|
if (start < 0) start = 0;
|
||||||
|
for (int j = start; j <= top; j++)
|
||||||
|
printf("%d ", stack[j]);
|
||||||
|
printf("\t\tFault");
|
||||||
|
} else {
|
||||||
|
// page found — move it to top (most recently used)
|
||||||
|
for (int j = pos; j < top; j++)
|
||||||
|
stack[j] = stack[j + 1];
|
||||||
|
stack[top] = pages[i];
|
||||||
|
|
||||||
|
// print current frames
|
||||||
|
int start = top - frames + 1;
|
||||||
|
if (start < 0) start = 0;
|
||||||
|
for (int j = start; j <= top; j++)
|
||||||
|
printf("%d ", stack[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nTotal Page Faults: %d\n", faults);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user