Uploaded by Zanam Khan

077bct033 osLAB9

advertisement
Submitted by:
Name: Javed Ansari
Roll: 077BCT033
Section: B
Submitted to:
Bikal Adhikari Sir
Department of Electronics and Computer Engineering
Paging and Segmentation
Ex 9: Paging
Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
int b[20], n, i, pa, p, a, d;
printf("\nProgram for paging\n");
printf("Enter the number of pages: ");
scanf("%d", &n);
printf("Enter the base address: ");
for (i = 0; i < n; i++)
{
scanf("%d", &b[i]);
}
printf("Enter the logical address: ");
scanf("%d", &p);
// Check if page number is within valid range
if (p >= 0 && p < n)
{
pa = b[p] + d;
a = b[p];
printf("\n\tPageNo.\t BaseAdd. PhysicalAdd.\n");
printf("\t%d\t %d\t %d\n", p, a, pa);
}
else
{
printf("\nInvalid page\n");
}
return 0;
}
Output:
Discussion:





User Input: The program prompts the user to input the number of pages and the base
addresses for each page.
Logical Address Input: After taking the base addresses, the program prompts the user to
input a logical address, i.e., the page number.
Address Translation: It checks if the input logical address (page number) is within the
valid range of page numbers (0 to n-1). If valid, it calculates the physical address using
the base address of the corresponding page.
Output: If the logical address is valid, the program prints the corresponding page
number, base address, and physical address in a tabular form.
Error Handling: It displays an error message if the input logical address is not within
the valid range, preventing potential issues such as accessing non-existent pages.
Ex 10: Segmentation
Code :
#include <stdio.h>
#include <unistd.h>
int main() {
int b[20], l[20], n, i, pa, s, a, d;
printf("\nProgram for segmentation\n");
printf("Enter the number of segments: ");
scanf("%d", &n);
printf("Enter the base address and limit register for each segment:\n");
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
scanf("%d", &l[i]);
}
printf("\nEnter the logical address: ");
scanf("%d", &d);
for (i = 0; i < n; i++) {
if (d < l[i]) {
pa = b[i] + d;
a = b[i];
printf("\n\tSegmentNo.\t BaseAdd.\t LogicalAdd.\t PhysicalAdd.\n");
printf("\t%d\t\t %d\t\t %d\t\t %d\n", i, a, d, pa);
return 0;
}
}
printf("\nInvalid segment\n");
return 0;
}
Output:
Discussion:
1. User Input: The program asks the user to input the number of segments and the base
addresses along with the limit registers for each segment. This information is essential
for simulating the segmentation mechanism.
2. Logical Address Input: After taking the base addresses and limit registers, the program
prompts the user to input a logical address, which consists of a segment number and an
offset within that segment. The segment number represents the segment the user wants
to access, and the offset represents the position within that segment.
3. Address Translation: The program checks if the input segment number is within the
valid range of segment numbers (0 to n-1). If the segment number is valid, it calculates
the physical address by adding the base address of the corresponding segment to the
offset.
4. Output: If the input segment number is valid, the program prints the corresponding
segment number, base address, logical address, and physical address in a tabular
format.
5. Error Handling: If the input segment number is not within the valid range, the program
displays an error message indicating an invalid segment. This prevents potential issues
such as accessing non-existent segments.
Download