Vous êtes sur la page 1sur 17

PROGRAM:

MEMORY MANAGEMENT
#include<stdio.h>
#include<conio.h>
int m1,p1,m[10],p[10],datam[10];
int adref[10],rin[10];
int option;
int count;
void firstfit(int i)
{
int j;
for(j=0;j<m1;j++)
{
if(m[j]>=p[i])
{
printf("\nProcess %3.0d resides in memory %3.0d",p[i],datam[adref[j]]);
m[j]-=p[i];
rin[i] = adref[j];
break;
}
else if(j+1==m1)
{
printf("\nProcess %3.0d must wait...",p[i]);
rin[i] = -1;
}
}

}
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void sort(char order)
{
int i,j;
for(i=0;i<m1;i++)
for(j=i+1;j<m1;j++)
if(m[i]==m[j])
{
if(adref[i]>adref[j])
swap(&adref[i],&adref[j]);
}
else
{
if(order=='a' && m[i]>m[j])
{
swap(&m[i],&m[j]);
swap(&adref[i],&adref[j]);
}
if(order=='d' && m[i]<m[j])
{
swap(&m[i],&m[j]);
swap(&adref[i],&adref[j]);
}
}
}
void print()
{
int i,j;
printf("\n\nMEMORY CHART:....\n\n");
for(i=0;i<m1;i++)
{
printf("\n%0.3d |----------|",datam[i]);
for(j=0;j<p1;j++)
{
if(rin[j]==i)
printf("\n | %0.3d |",p[j]);
}
}
printf("\n |----------|");
}
void main()
{
int i;
clrscr();
printf("\n\tEnter the number of memory slots...");
scanf("%d",&m1);
for(i=0;i<m1;i++)
{
printf("\n\tEnter the memory of slot %d:...",i+1);
scanf("%d",&datam[i]);
}
printf("\n\tEnter the number of processes...");
scanf("%d",&p1);
for(i=0;i<p1;i++)
{
printf("\n\tEnter the process %d:",i+1);
scanf("%d",&p[i]);
}
while(count<3)
{
for(i=0;i<m1;i++)
{
m[i]=datam[i];
adref[i] = i;
}
printf("\n1.FIRST FIT \n2.BEST FIT \n3.WORST FIT \nEnter your option..");
scanf("%d",&option);
for(i=0;i<p1;i++)
{
if(option == 2)
sort('a');
else if(option == 3)
sort('d');
firstfit(i);
}
print();
getch();
count++;
}
getch();
}

OUTPUT:
Enter the number of memory slots...4
Enter the memory of slot 1:...400
Enter the memory of slot 2:...350
Enter the memory of slot 3:...150
Enter the memory of slot 4:...200
Enter the number of processes...3
Enter the process 1:75
Enter the process 2:270
Enter the process 3:130

1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your option..1

Process 75 resides in memory 400


Process 270 resides in memory 400
Process 130 resides in memory 350

MEMORY CHART:....

400 |----------|
| 075 |
| 270 |
350 |----------|
| 130 |
150 |----------|
200 |----------|
|----------|

1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your option..2

Process 75 resides in memory 150


Process 270 resides in memory 350
Process 130 resides in memory 200
MEMORY CHART:....

400 |----------|
350 |----------|
| 270 |
150 |----------|
| 075 |
200 |----------|
| 130 |
|----------|

1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your option..3

Process 75 resides in memory 400


Process 270 resides in memory 350
Process 130 resides in memory 400

MEMORY CHART:....

400 |----------|
| 075 |
| 130 |
350 |----------|
| 270 |
150 |----------|
200 |----------|
|----------|

PROGRAM:
CONTIGUOS ALLOCATION OF MEMORY

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#define TOTALSP 9999
char filenameof[100][10];
int filesizeof[100];
int fp;
char dirname[10];
int rem,i;
void table()
{
int pc;
pc = i = 0;
printf("\n\nSTATISTICS....\n");
printf("\n\nFILENAME\t FILESIZE\t STARTADR\t ENDADR\n");
for(i = 0;i < fp;rem=i++)
{
if(pc + filesizeof[i] <= TOTALSP)
{
printf("\n\n%5s\t\t %5d\t\t %5d\t\t %5d",filenameof[i],filesizeof[i],pc,pc +
filesizeof[i]);
pc += filesizeof[i];
}
else
break;
}
printf("\n\nFreespace :: %5d",TOTALSP - pc);
}
int allocate()
{
int pc;
pc = rem = i = 0;
printf("\n\nDIRECTORY....%s\n",dirname);
for(i = 0;i < fp;rem=i++)
{
if(pc + filesizeof[i] <= TOTALSP)
{
printf("\n\t %5d|------------------|",pc);
printf("\n\t | %5s | %5d",filenameof[i],filesizeof[i]);
pc += filesizeof[i];
}
else
break;
}
printf("\n\t %5d|------------------|",pc);
if(pc<TOTALSP)
{
printf("\n\t |##################|");
printf("\n\t |##################| Freespace :: %5d",TOTALSP - pc);
printf("\n\t %5d|------------------|",TOTALSP);

}
while(i<fp)
{
printf("\n\nNO SPACE FOR THE FILE %s OF %d
MEMORY!",filenameof[i],filesizeof[i]);
i++;
}
if(pc == TOTALSP)
return 0;
return 1}
void main()
{
int status;
char ch;
fp = 0;
clrscr();
printf("ENTER THE DIRECTORY NAME...");
gets(dirname);
printf("\nMEMORY AVAILABLE!(0000 TO 9999)");
do
{
do
{
printf("\n\nENTER THE FILE NAME....");
gets(filenameof[fp]);
printf("\nENTER THE SIZE OF %s..",filenameof[fp]);
scanf("%d",&filesizeof[fp]);
printf("\n\nWANT TO CREATE ANOTHER FILE:?(Y/N)..");
ch = getche();
fp++;
}
while(ch=='y');
status = allocate();
table();
switch(status)
{
case 0:
printf("\n\nMEMORY FULL CANNOT PROCEED FURTHER! press any
key to exit");
getch();
exit(1);
case 1:
fp = rem+1;
printf("\n\nMEMORY AVAILABLE! WONNA CREATE ANOTHER
FILE...(y/n)");
ch = getche();
break;
}
}
while(ch == 'y');
printf("\n\nPress any key to exit..");
getch();
}

OUTPUT:

ENTER THE DIRECTORY NAME...OS LAB


MEMORY AVAILABLE!(0000 TO 9999)
ENTER THE FILE NAME....FILE 1
ENTER THE SIZE OF FILE 1..4500
WANT TO CREATE ANOTHER FILE:?(Y/N)..y
ENTER THE FILE NAME....FILE 2
ENTER THE SIZE OF FILE 2..4200
WANT TO CREATE ANOTHER FILE:?(Y/N)..y
ENTER THE FILE NAME....FILE 3
ENTER THE SIZE OF FILE 3..1000
WANT TO CREATE ANOTHER FILE:?(Y/N)..y
ENTER THE FILE NAME....FILE 4
ENTER THE SIZE OF FILE 4..200
WANT TO CREATE ANOTHER FILE:?(Y/N)..
WANT TO CREATE ANOTHER FILE:?(Y/N)..n
DIRECTORY....OS LAB

0 |------------------|
| FILE 1 |4500
4500|------------------|
| FILE 2 | 4200
8700|------------------|
| FILE 3 | 1000
9700|------------------|
| FILE 4 | 200
9900|------------------|
|#############|
|#############| Freespace :: 99
9999|------------------|
STATISTICS....

FILENAME FILESIZE STARTADR ENDADR


FILE 1 4500 0 4500
FILE 2 4200 4500 8700
FILE 3 1000 8700 9700
FILE 4 200 9700 9900
Freespace :: 99
MEMORY AVAILABLE! WONNA CREATE ANOTHER FILE...(y/n) Press any key to exit..

PROGRAM:
PAGE REPLACEMENT ALGORITHM – FIFO

#include<stdio.h>
#include<conio.h>
#define YES 1
#define NO 0
#define NOPAGE -1
#define NOFRAME -1
struct numberof
{
int frames;
int refpages;
int pagefaults;
}num;
int frame
int ref_page[50];
int pageinframe[50];
int pagetable[50][50];
int selectedframes[50];
void copytopagetable(int i)
{
for(frame=0;frame<num.frames;frame++)
pagetable[frame][i] = pageinframe[frame];
}
void printtable()
{
int i;
printf("\nREFERENCE PAGES...\n");
for(i=0;i<num.refpages;i++)
printf(" %2d ",ref_page[i]);
printf("\n\nFRAMES...\n")
for(frame = 0;frame<num.frames;frame++)
{
printf("\n|");
for(i=0;i<num.refpages;i++)
{
printf("---|");
}
printf("\n|");
for(i=0;i<num.refpages;i++)
{
if(frame == selectedframes[i])
printf("%2d*|",pagetable[frame][i]);
else
printf("%2d |",pagetable[frame][i]);

}
}
printf("\n|");
for(i=0;i<num.refpages;i++)
{
printf("---|");
}
printf("\n\n\nTHE NO OF PAGE FAULTS...%d",num.pagefaults);
}
int check_page_exist(int page)
{
for(frame=0;frame<num.frames;frame++)
{
if(pageinframe[frame] == page)
return frame;
}
return NOFRAME;
}
int main()
{
int i;
int selframe = NOFRAME;
clrscr();
printf("\nFIRST IN FIRST OUT !!! PAGE REPLACEMENT ALGORITHM !!!!!\n");
printf("\nENTER THE NO OF REFERENCE PAGES...");
scanf("%d",&num.refpages);
printf("\nENTER THE REFERENCE PAGES......");
for(i=0;i<num.refpages;i++)
scanf("%d",&ref_page[i]);
printf("\nENTER THE NO OF FRAMES..");
scanf("%d",&num.frames);
printf("\nTHE FRAMES ARE....\n");
for(i=0;i<num.frames;i++)
{
printf("%d\t",i);
pageinframe[i] = NOPAGE;
}
for(i=0;i<num.refpages;i++)
{
frame = check_page_exist(ref_page[i]);
if(frame != NOFRAME )
selectedframes[i] = NOFRAME;
else
{
num.pagefaults++;
selframe = ( selframe + 1 ) % num.frames;
pageinframe[selframe] = ref_page[i];
selectedframes[i] = selframe;
}
copytopagetable(i);
}
printtable();
getch();
}

OUTPUT:

FIRST IN FIRST OUT !!! PAGE REPLACEMENT ALGORITHM !!!!!


ENTER THE NO OF REFERENCE PAGES...10

ENTER THE REFERENCE PAGES......1 2 3 4 2 1 5 6 2 3

ENTER THE NO OF FRAMES..4

THE FRAMES ARE....


0 1 2 3
REFERENCE PAGES...
1 2 3 4 2 1 5 6 2 3

FRAMES...
|---|---|---|---|---|---|---|---|---|---|
| 1*| 1 | 1 | 1 | 1 | 1 | 5*| 5 | 5 | 5 |
|---|---|---|---|---|---|---|---|---|---|
|-1 | 2*| 2 | 2 | 2 | 2 | 2 | 6*| 6 | 6 |
|---|---|---|---|---|---|---|---|---|---|
|-1 |-1 | 3*| 3 | 3 | 3 | 3 | 3 | 2*| 2 |
|---|---|---|---|---|---|---|---|---|---|
|-1 |-1 |-1 | 4*| 4 | 4 | 4 | 4 | 4 | 3*|
|---|---|---|---|---|---|---|---|---|---|

THE NO OF PAGE FAULTS...8

PROGRAM:

PAGE REPLACEMENT ALGORITHM – LRU


#include<stdio.h>
#include<conio.h>
#define YES 1
#define NO 0
#define NOPAGE -1
#define NOFRAME -1
struct numberof
{
int frames;
int refpages;
int pagefaults;
}num;
int frame;
int idletime[50];
int ref_page[50];
int pageinframe[50];
int pagetable[50][50];
int selectedframes[50];
int lru()
{
int selframe = 0;
for(frame = 0;frame<num.frames;frame++)
{
if(pageinframe[frame]==NOFRAME)
return frame;
else
if(idletime[frame] > idletime[selframe])
selframe = frame;
}return selframe;
}
void idletimeup_except(int selframe)
{
for(frame=0;frame<num.frames;frame++)
{
if(frame == selframe)
idletime[frame] = 1;
else
if(pageinframe[frame] != NOFRAME)
idletime[frame]++;
}
}
void copytopagetable(int i)
{
for(frame=0;frame<num.frames;frame++)
pagetable[frame][i] = pageinframe[frame];
}
void printtable()
{
int i;
printf("\nREFERENCE PAGES...\n");
for(i=0;i<num.refpages;i++)
printf(" %2d ",ref_page[i]);
printf("\n\nFRAMES...\n") ;
for(frame = 0;frame<num.frames;frame++)
{
printf("\n--------------------------");
printf("\n|");
for(i=0;i<num.refpages;i++)
{
if(frame == selectedframes[i])
printf("%2d*|",pagetable[frame][i]);
else
printf("%2d |",pagetable[frame][i]);

}
}printf("\n--------------------------");
printf("\n\n\nTHE NO OF PAGE FAULTS...%d",num.pagefaults);
}
int check_page_exist(int page)
{
for(frame=0;frame<num.frames;frame++)
{
if(pageinframe[frame] == page)
return frame;
}
return NOFRAME;
}

int main()
{
int i;
int selframe = NOFRAME;
printf("\nLEAST RECENTLY USED !!!
PAGE REPLACEMENT ALGORITHM !!!!!\n");
printf("\nENTER THE NO OF REFERENCE PAGES...");
scanf("%d",&num.refpages); printf("\nENTER THE REFERENCE PAGES......");
for(i=0;i<num.refpages;i++)
scanf("%d",&ref_page[i]);
printf("\nENTER THE NO OF FRAMES..");
scanf("%d",&num.frames);
printf("\nTHE FRAMES ARE....\n");
for(i=0;i<num.frames;i++)
{
printf("%d\t",i);
pageinframe[i] = NOPAGE;
}
for(i=0;i<num.refpages;i++)
{
frame = check_page_exist(ref_page[i]);
if(frame != NOFRAME )
{
selectedframes[i] = NOFRAME;
idletimeup_except(frame);
}
else
{
num.pagefaults++;
selframe = lru();
pageinframe[selframe] = ref_page[i];
selectedframes[i] = selframe;
idletimeup_except(selframe);
} copytopagetable(i);
}
printtable();
getch();
}

OUTPUT:

LEAST RECENTLY USED !!! PAGE REPLACEMENT ALGORITHM !!!!!


ENTER THE NO OF REFERENCE PAGES...10

ENTER THE REFERENCE PAGES......1 2 3 4 2 1 5 6 2 3

ENTER THE NO OF FRAMES..4

THE FRAMES ARE....


0 1 2 3
REFERENCE PAGES...
1 2 3 4 2 1 5 6 2 3

FRAMES...

-------------------------------------
| 1*| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3*|
-------------------------------------
|-1 | 2*| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
-------------------------------------
|-1 |-1 | 3*| 3 | 3 | 3 | 5*| 5 | 5 | 5 |
--------------------------------------
|-1 |-1 |-1 | 4*| 4 | 4 | 4 | 6*| 6 | 6 |
--------------------------------------

THE NO OF PAGE FAULTS...7

Vous aimerez peut-être aussi