Menu

Operating Systems [ Lab Programs ]


Aim:

  Write C programs to simulate the following memory management techniques
a) Paging
b) Segmentation.

Solution :

a) Paging

PROGRAM: ( paging.c )

 
#include<stdio.h>
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;j<s[i];j++)
scanf("%d",&fno[i][j]); }
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np || 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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
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<l[i])
{
pa=b[i]+d;
a=b[i];
printf("\nPageNo.\t BaseAdd. PhysicalAdd. \n %d \t %d \t %d\n",s,a,pa);
exit(0);
}
else
{
printf("\nPage size exceeds");
exit(0);
}
}
}
printf("\nInvalid segment");
return 0;
}

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




Related Content :

1. Write C programs to simulate the following CPU Scheduling algorithms
a) FCFS
b) SJF
c) RoundRobin
d) priority    View Solution


2. Write programs using the I/O system calls of UNIX/LINUX operating system (open, read, write, close,fcntl, seek, stat, opendir, readdir)    View Solution


3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention.    View Solution


4. Write a C program to implement the Producer – Consumer problem using semaphores using UNIX/LINUX system calls.   View Solution


5. Write C programs to illustrate the following IPC mechanisms
a) Pipes
b) FIFOs
c) Message Queues
d) Shared Memory.    View Solution


6. Write C programs to simulate the following memory management techniques
a) Paging
b) Segmentation    View Solution


7. Write C programs to simulate Page replacement policies
a) FCFS
b) LRU
c) Optimal    View Solution