Paging
Fri Jun 07 2024 03:25:10 GMT+0000 (Coordinated Universal Time)
Saved by
@dbms
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 4 // Define page size (4KB for example)
#define MEMORY_SIZE 32 // Define memory size (32KB for example)
void simulatePaging(int processSize) {
int numPages = (processSize + PAGE_SIZE - 1) / PAGE_SIZE; // Calculate the number of pages required
printf("Process of size %dKB requires %d pages.\n", processSize, numPages);
int memory[MEMORY_SIZE / PAGE_SIZE]; // Simulate memory with an array
// Initialize memory (0 means free, 1 means occupied)
for (int i = 0; i < MEMORY_SIZE / PAGE_SIZE; i++) {
memory[i] = 0;
}
// Allocate pages to the process
for (int i = 0; i < numPages; i++) {
for (int j = 0; j < MEMORY_SIZE / PAGE_SIZE; j++) {
if (memory[j] == 0) {
memory[j] = 1;
printf("Allocating page %d to frame %d\n", i, j);
break;
}
}
}
}
int main() {
int processSize;
printf("Enter process size (in KB): ");
scanf("%d", &processSize);
simulatePaging(processSize);
return 0;
}
content_copyCOPY
Comments