☛ Write C programs to simulate the following memory management techniques a) Paging b) Segmentation.
Aim:
☛ Write C programs to simulate the following memory management techniques
a) Paging
b) Segmentation.
Solution :
a) Paging
PROGRAM: ( paging.c )
#include
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- ");
scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
printf("\nEnter pagetable for p[%d] --- ",i);
for(j=0;jnp || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
return 0;
}
OUTPUT:
$ gcc paging.c
$ ./a.out
Enter the memory size -- 1200
Enter the page size -- 100
The no. of pages available in memory are -- 12
Enter number of processes -- 3
Enter no. of pages required for p[1]-- 4
Enter pagetable for p[1] --- 2 3 4 5
Enter no. of pages required for p[2]-- 3
Enter pagetable for p[2] --- 4 5 6
Enter no. of pages required for p[3]-- 4
Enter pagetable for p[3] --- 7 1 8 9
Enter Logical Address to find Physical Address
Enter process no. and pagenumber and offset -- 2 4 60
The Physical Address is -- 60
b) Segmentation
PROGRAM: ( segmentation.c )
#include
#include
#include
int main()
{
int b[20],l[20],n,i,pa,s,a,d;
printf("\nProgram for segmentation");
printf("\nEnter the number of segments:");
scanf("%d",&n);
printf("\nEnter the base address and limit register:");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
scanf("%d",&l[i]);
}
printf("\nEnter the logical address:");
scanf("%d",&d);
printf("\nEnter segment number:");
scanf("%d",&s);
for(i=1;i<=n;i++)
{
if(i==s)
{
if(d OUTPUT:
$ gcc segmentation.c
$ ./a.out
Program for segmentation
Enter the number of segments:3
Enter the base address and limit register:
100 50
150 20
130 34
Enter the logical address:25
Enter segment number:1
PageNo. BaseAdd. PhysicalAdd.
1 100 125
💻 Run This C Program
☛ Write C programs to simulate the following memory management techniques
a) Paging
b) Segmentation.
The selected C program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.
The compiler is embedded using OneCompiler.
Your C program is compiled and run in the embedded
Linux environment; Study Glance does not execute C code
directly on your web server.
Some Operating Systems programs (fork, pipes, FIFOs,
message queues, shared memory and semaphores) may be
restricted in an online sandbox. If a program does not
run here, compile it on a Linux system with
gcc.