You are on page 1of 5

2024

Operating System II

Lab 2
.

BY:
ABDULLAH AHMED
COMPUTER.ENG
Write a program in c language aims to create logical address memory of program sizes 32
divided in to four page each page contents array of char from a to z or A to Z characters.

#include <stdio.h>

#include <stdlib.h>

#define PAGE_SIZE 8

#define NUM_PAGES 4

void initializePages(char pages[NUM_PAGES][PAGE_SIZE]) {

char start_char = 'a';

for (int i = 0; i < NUM_PAGES; i++) {

for (int j = 0; j < PAGE_SIZE; j++) {

pages[i][j] = start_char + i;

if (start_char == 'Z')

start_char = 'a';

else if (start_char == 'z')

start_char = 'A';

else

start_char++;

void printLogicalMemory(char pages[NUM_PAGES][PAGE_SIZE]) {

printf("Logical Memory:\n");

for (int i = 0; i < NUM_PAGES; i++) {

printf("Logical[%d]: ", i);

for (int j = 0; j < PAGE_SIZE; j++) {

printf("%c", pages[i][j]);

}
printf("\n");

int main() {

char pages[NUM_PAGES][PAGE_SIZE];

initializePages(pages);

printLogicalMemory(pages);

return 0;

}
Write a program For finding the page number and the offset from the logical address. The program
will accept only a logical address. Call your program: logical_address.c As an example, your
program would run as follows: ./logical_address 7
Your program would output: The logical address=7 contains Page number = 1 offset = 3

#include <stdio.h>
#include <stdlib.h>

#define PAGE_SIZE 4

void calculatePageAndOffset(int logicalAddress) {


int pageNumber = logicalAddress / PAGE_SIZE;
int offset = logicalAddress % PAGE_SIZE;
printf("The logical address=%d contains Page number = %d offset = %d\n",
logicalAddress, pageNumber, offset);
}

int main(int argc, char *argv[]) {


if (argc != 2) {
printf("Usage: %s <logical_address>\n", argv[0]);
return 1;
}

int logicalAddress = atoi(argv[1]);


calculatePageAndOffset(logicalAddress);
return 0;
}
The
End

You might also like