UNIX C Program for Paging | CS1254-Operating Systems Lab


AIM:
To write a LINUX/UNIX C Program for the Implementation of Paging in CS1254 - Operating Systems Laboratory.

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct list
{
int page;
int frame;
struct list *next;
}*p;
void add(struct list *q,int page,int frame)
{
if(p==NULL)
{
p=malloc(sizeof(struct list));
p->page=page;
p->frame=frame;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=malloc(sizeof(struct list));
q->next->page=page;
q->next->frame=frame;
q->next->next=NULL;
}
}
int search(struct list *q,int page)
{
while(q->page!=page)
{
q=q->next;
}
return q->frame;
}
main()
{
p=NULL;
int pagesize,logicaladdr;
int i,page,frame;
int quotient,offset,physicaladdr;
printf("Enter page table as structure\n");
printf("Enter 100 as page number for termination\n");
printf("Enter page size");
scanf("%d",&pagesize);
do
{
printf("Enter the page number");
scanf("%d",&page);
if(page!=100)
{
printf("Enter the frame number:");
scanf("%d",&frame);
add(p,page,frame);
}
}
while(page!=100);
printf("Enter logical address");
scanf("%d",&logicaladdr);
quotient=logicaladdr/pagesize;
offset=logicaladdr%pagesize;
physicaladdr=(search(p,quotient)*pagesize)+offset;
printf("CORRESPONDING PHYSICAL ADDRESS IS %d\n",physicaladdr);
}

OUTPUT: 
< [examuser56@localhost ~]$ cc paging.c
[examuser56@localhost ~]$ ./a.out
Enter page table as structure
Enter 100 as page number for termination
Enter page size8
Enter the page number1
Enter the frame number:5
Enter the page number2
Enter the frame number:4
Enter the page number3
Enter the frame number:2
Enter the page number4
Enter the frame number:3
Enter the page number100
Enter logical address12

CORRESPONDING PHYSICAL ADDRESS IS 44

Previous
Next Post »

Still not found what you are looking for? Try again here.