added optimised OS codes

This commit is contained in:
sherlock 2025-04-04 08:57:29 +05:30
parent fbcc758f43
commit 86ff7c0f35
9 changed files with 723 additions and 0 deletions

BIN
OS/C/Week10/aq1 Executable file

Binary file not shown.

50
OS/C/Week10/aq1.c Normal file
View file

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
// Page Replacement: FIFO & Optimal (Minimal Code for Paper)
int main() {
int nf, np, i, j, k, pf, hit, idx, max_f, vic; // nf=frames, np=pages, pf=faults, idx=fifo_idx, max_f=max_future, vic=victim
int *rs, *f; // rs=ref_string, f=frames
printf("F N:"); scanf("%d%d", &nf, &np); // Frames, NumPages
rs = malloc(np * sizeof(int));
f = malloc(nf * sizeof(int));
if(!rs || !f) return 1; // Basic alloc check
printf("RS:"); for(i=0; i<np; i++) scanf("%d", &rs[i]); // Ref String
// FIFO
puts("FIFO"); pf=0; idx=0;
for(k=0; k<nf; k++) f[k]=-1; // Init frames
for(i=0; i<np; i++){ // Iterate ref string
hit=0; for(k=0; k<nf; k++) if(f[k]==rs[i]) {hit=1; break;} // Check hit
if(!hit){ // Page Fault
pf++; f[idx]=rs[i]; idx=(idx+1)%nf; // Replace using FIFO index
}
}
printf("F:%d\n", pf); // Print Faults
// Optimal
puts("OPT"); pf=0;
for(k=0; k<nf; k++) f[k]=-1; // Re-init frames
for(i=0; i<np; i++){ // Iterate ref string
hit=0; for(k=0; k<nf; k++) if(f[k]==rs[i]) {hit=1; break;} // Check hit
if(!hit){ // Page Fault
pf++; int empty=-1; for(k=0; k<nf; k++) if(f[k]==-1) {empty=k; break;} // Find empty frame
if(empty!=-1) f[empty]=rs[i]; // Use empty frame if available
else { // No empty frames, find victim
vic=0; max_f=-1; // Victim index, max future distance
for(k=0; k<nf; k++){ // Check each current frame 'f[k]'
int fut=-1; // Index of next use for f[k]
for(j=i+1; j<np; j++) if(f[k]==rs[j]) {fut=j; break;} // Look ahead
if(fut==-1) {vic=k; break;} // f[k] not used again? Best victim. Stop search.
if(fut>max_f) {max_f=fut; vic=k;} // f[k] used later than current max? Update victim.
}
f[vic]=rs[i]; // Replace victim frame
}
}
}
printf("F:%d\n", pf); // Print Faults
free(rs); free(f); // Free memory
return 0;
}

BIN
OS/C/Week10/aq2 Executable file

Binary file not shown.

78
OS/C/Week10/aq2.c Normal file
View file

@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h> // For malloc, free
// C program for LRU Page Replacement Simulation
// Optimized for minimal code size (e.g., for writing on paper)
int main() {
int nf, np, i, j, pg, idx, lru, pf = 0, time = 0, min_t;
// nf=num frames, np=num pages, pf=page faults, time=logical clock
// idx=found index, lru=least recently used index, pg=current page
// Input frame count (nf) and page reference string length (np)
printf("Frames Pages:"); scanf("%d%d", &nf, &np);
// Dynamic Allocation
int *f = malloc(nf * sizeof(int)); // f: frames array
int *c = malloc(nf * sizeof(int)); // c: counter/lru time array
int *p = malloc(np * sizeof(int)); // p: page reference string array
// Input page reference string
printf("Pages:");
for(i=0; i<np; ) scanf("%d", p+i++); // Read pages into p
// Initialize frames to -1 (empty)
for(i=0; i<nf; i++) f[i]=-1; // Can also use for(i=nf;i--;)f[i]=-1;
// --- LRU Algorithm ---
for(i=0; i<np; i++) { // Iterate through page reference string
pg = p[i]; // Current page
idx = -1; // Reset found index
// 1. Search if page 'pg' is already in frames 'f'
for(j=0; j<nf; j++) {
if(f[j] == pg) {
idx = j; // Page found at index j
break;
}
}
if(idx != -1) { // 2a. Page Hit
c[idx] = ++time; // Update last used time for the hit page
} else { // 2b. Page Fault
pf++; // Increment page fault counter
lru = 0; // Index to replace (default to 0)
min_t = 0x7FFFFFFF; // Initialize minimum time to max int
// 3. Find replacement slot: first empty (-1) or LRU
for(j=0; j<nf; j++) {
if(f[j] == -1) { // Found an empty frame
lru = j;
break; // Use the first empty frame
}
if(c[j] < min_t) { // Track frame with the smallest time (LRU)
min_t = c[j];
lru = j;
}
}
// 'lru' now holds index of empty slot or the least recently used page
// 4. Replace frame and update its time
f[lru] = pg;
c[lru] = ++time;
}
// Optional: print frame state after each step (for debugging)
// printf(" (%d):",pg); for(j=0; j<nf; j++)printf(" %d", f[j]==-1?-1:f[j]); puts("");
}
// --- End Algorithm ---
// Output total page faults
printf("Faults: %d\n", pf);
// Free dynamically allocated memory
free(f);
free(c);
free(p);
return 0; // End of program
}

198
OS/C/Week10/q1.c Normal file
View file

@ -0,0 +1,198 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // For INT_MAX in optimal
// Function to check if a page is present in frames
int isPresent(int page, int *frames, int num_frames) {
for (int i = 0; i < num_frames; i++) {
if (frames[i] == page) {
return 1; // Present
}
}
return 0; // Not present
}
// Function to print the current state of frames (for debugging/visualization)
void printFrames(int *frames, int num_frames) {
printf("Frames: ");
for (int i = 0; i < num_frames; i++) {
if (frames[i] == -1) {
printf("[ ] ");
} else {
printf("[%d] ", frames[i]);
}
}
printf("\n");
}
// FIFO Page Replacement Simulation
int simulateFIFO(int *ref_string, int num_refs, int num_frames) {
int *frames = (int *)malloc(num_frames * sizeof(int));
if (frames == NULL) {
perror("Failed to allocate memory for frames");
exit(EXIT_FAILURE);
}
for (int i = 0; i < num_frames; i++) {
frames[i] = -1; // Initialize frames as empty (-1 indicates empty)
}
int page_faults = 0;
int frame_index = 0; // Points to the next frame to be replaced (FIFO queue head)
printf("\n--- FIFO Simulation ---\n");
for (int i = 0; i < num_refs; i++) {
int current_page = ref_string[i];
printf("Ref: %d -> ", current_page);
if (!isPresent(current_page, frames, num_frames)) {
page_faults++;
frames[frame_index] = current_page;
frame_index = (frame_index + 1) % num_frames; // Move to next frame in circular fashion
printf("Fault! ");
printFrames(frames, num_frames);
} else {
printf("Hit. ");
printFrames(frames, num_frames);
}
}
free(frames);
return page_faults;
}
// Function to find the optimal page to replace
int findOptimalVictim(int *frames, int num_frames, int *ref_string, int num_refs, int current_index) {
int victim_frame = -1;
int farthest_use = -1; // Index of the farthest future use
for (int i = 0; i < num_frames; i++) {
int page_in_frame = frames[i];
int next_use = INT_MAX; // Assume page is never used again initially
// Look for the next occurrence of this page in the reference string
for (int j = current_index + 1; j < num_refs; j++) {
if (ref_string[j] == page_in_frame) {
next_use = j;
break; // Found the *next* use
}
}
// If this page is never used again, it's the best victim
if (next_use == INT_MAX) {
return i; // Return the index of the frame holding this page
}
// Otherwise, track the page whose next use is farthest away
if (next_use > farthest_use) {
farthest_use = next_use;
victim_frame = i; // This frame holds the current best candidate for victim
}
}
// Should always find a victim if frames are full, defaults to first if logic error/all used soon
return (victim_frame == -1) ? 0 : victim_frame;
}
// Optimal Page Replacement Simulation
int simulateOptimal(int *ref_string, int num_refs, int num_frames) {
int *frames = (int *)malloc(num_frames * sizeof(int));
if (frames == NULL) {
perror("Failed to allocate memory for frames");
exit(EXIT_FAILURE);
}
for (int i = 0; i < num_frames; i++) {
frames[i] = -1; // Initialize frames as empty
}
int page_faults = 0;
int current_frame_count = 0;
printf("\n--- Optimal Simulation ---\n");
for (int i = 0; i < num_refs; i++) {
int current_page = ref_string[i];
printf("Ref: %d -> ", current_page);
if (!isPresent(current_page, frames, num_frames)) {
page_faults++;
printf("Fault! ");
// Check if there are empty frames first
if (current_frame_count < num_frames) {
frames[current_frame_count] = current_page;
current_frame_count++;
} else {
// Frames are full, need to find the optimal victim
int victim_index = findOptimalVictim(frames, num_frames, ref_string, num_refs, i);
frames[victim_index] = current_page; // Replace victim
}
printFrames(frames, num_frames);
} else {
printf("Hit. ");
printFrames(frames, num_frames);
}
}
free(frames);
return page_faults;
}
int main() {
int num_frames;
int num_refs;
int *ref_string;
// Get number of frames
printf("Enter the number of page frames: ");
scanf("%d", &num_frames);
if (num_frames <= 0) {
printf("Number of frames must be positive.\n");
return 1;
}
// Get number of page references
printf("Enter the number of page references in the sequence: ");
scanf("%d", &num_refs);
if (num_refs <= 0) {
printf("Number of references must be positive.\n");
return 1;
}
// Allocate memory for reference string
ref_string = (int *)malloc(num_refs * sizeof(int));
if (ref_string == NULL) {
perror("Failed to allocate memory for reference string");
return 1;
}
// Get the reference string
printf("Enter the page reference sequence (e.g., 7 0 1 2 0 ...):\n");
for (int i = 0; i < num_refs; i++) {
if (scanf("%d", &ref_string[i]) != 1) {
printf("Invalid input for reference sequence.\n");
free(ref_string);
return 1;
}
if (ref_string[i] < 0) {
printf("Page numbers cannot be negative.\n");
free(ref_string);
return 1;
}
}
// --- Run Simulations ---
int fifo_faults = simulateFIFO(ref_string, num_refs, num_frames);
int optimal_faults = simulateOptimal(ref_string, num_refs, num_frames);
// --- Print Results ---
printf("\n--- Results ---\n");
printf("Reference String Length: %d\n", num_refs);
printf("Number of Frames: %d\n", num_frames);
printf("FIFO Page Faults: %d\n", fifo_faults);
printf("Optimal Page Faults: %d\n", optimal_faults);
// --- Cleanup ---
free(ref_string);
return 0;
}

0
OS/C/Week10/q2.c Normal file
View file