Segmentation
Fri Jun 07 2024 03:25:40 GMT+0000 (Coordinated Universal Time)
Saved by
@dbms
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int base;
int limit;
} Segment;
void simulateSegmentation(int segments[], int numSegments) {
int memory[100]; // Simulate memory with an array (100 units for example)
int currentBase = 0;
// Allocate segments in memory
for (int i = 0; i < numSegments; i++) {
if (currentBase + segments[i] <= 100) {
printf("Allocating segment %d with size %d at base %d\n", i, segments[i], currentBase);
for (int j = currentBase; j < currentBase + segments[i]; j++) {
memory[j] = 1; // Mark memory as occupied
}
currentBase += segments[i];
} else {
printf("Not enough memory for segment %d with size %d\n", i, segments[i]);
}
}
}
int main() {
int numSegments;
printf("Enter number of segments: ");
scanf("%d", &numSegments);
int segments[numSegments];
for (int i = 0; i < numSegments; i++) {
printf("Enter size of segment %d: ", i);
scanf("%d", &segments[i]);
}
simulateSegmentation(segments, numSegments);
return 0;
}
content_copyCOPY
Comments