Vous êtes sur la page 1sur 20

Memory Management

#include<stdio.h>
#define ASC 0
#define DESC 1
#define FIRSTFIT 1
#define BESTFIT 2
#define WORSTFIT 3
struct actionLog
{
int proc;
int mem;
};
void notify_success(int proc,int mem)
{
printf("The process of size %d is allocated in memory block %d\n",proc,mem);
}
void notify_failure(int proc)
{
printf("The process of size %d is not allocated [ It has to wait ]\n",proc);
}
void display(struct actionLog *log,int *mem,int nob,int nol)
{
//Display will be perfect for three digit values.
//For other values add/delete one "-" for the printf statement
int i,j,temp=0;
printf("\n\nMEMORY BLOCK\n\n");
printf("\t|---------------------------|\n");
for(i=0;i<nob;i++)
{
temp=0;
printf(" %d",mem[i]);
for(j=0;j<nol;j++)
{
if(mem[i] == log[j].mem)
{
printf("\t| %d |\n",log[j].proc);
printf("\t|---------------------------|\n");
temp = temp +log[j].proc;
}
}
if(mem[i] > temp)
{
printf("\t| | -1\n",log[j].proc);
printf("\t|---------------------------|\n");
}
}
}
void sort(int *arr,int *arr2,int size,int sortBy)
{
//the two arrays are are sorted based on the array "arr" contents
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(sortBy==ASC)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] =arr[j];
arr[j]=temp;
temp = arr2[i];
arr2[i] =arr2[j];
arr2[j]=temp;
}
}
else if(sortBy == DESC)
{
if(arr[i]<arr[j])
{
temp = arr[i];
arr[i] =arr[j];
arr[j]=temp;
temp = arr2[i];
arr2[i] =arr2[j];
arr2[j]=temp;
}
}
}
}
}
void memoryManagement(int *proc,int *mem,int nop,int nob,int method)
{
int i,j,count=0,flag=0,*cmem,*cmem2;
cmem = (int *)malloc(sizeof(int)*nob);
cmem2 = (int *)malloc(sizeof(int)*nob);
struct actionLog * log;
log = (struct actionLog *)malloc(sizeof(struct actionLog)*nop);
for(i=0;i<nob;i++)
{
cmem[i] = cmem2[i] = mem[i];
}
if(method == BESTFIT)
{
sort(cmem,cmem2,nob,ASC);
}
else if(method == WORSTFIT)
{
sort(cmem,cmem2,nob,DESC);
}
for(i=0;i<nop;i++)
{
flag=0;
for(j=0;j<nob;j++)
{
if(cmem[j] >= proc[i])
{
flag = 1;
cmem[j] -=proc[i];
log[count].proc=proc[i];
log[count++].mem = cmem2[j];
notify_success(proc[i],cmem2[j]);
if(method == BESTFIT)
{
sort(cmem,cmem2,nob,ASC);
}
else if(method == WORSTFIT)
{
sort(cmem,cmem2,nob,DESC);
}
break;
}
}
if(flag == 0)
{
notify_failure(proc[i]);
}
}
display(log,mem,nob,count);
}
int main(void)
{
int nob,nop,method,*mem,*proc,i;
printf("\n\nMemory Management\n\n");
printf("\nEnter the number of memory Block: ");
scanf("%d",&nob);
mem = (int *)malloc(sizeof(int)*nob);
printf("\nEnter the size of each memmory block :\n");
for(i=0;i<nob;i++)
{
scanf("%d",&mem[i]);
}
printf("Enter the number of process:");
scanf("%d",&nop);
proc = (int *)malloc(sizeof(int)*nop);
printf("\nEnter the memory size of each process:\n");
for(i=0;i<nop;i++)
{
scanf("%d",&proc[i]);
}
printf("\n\nSelect a Memory Management Method:\n");
printf("\n1.FirstFit.\n2.BestFit.\n3.WorstFit.\n\nChoice:");
scanf("%d",&method);
memoryManagement(proc,mem,nop,nob,method);
return 0;
}
OUTPUT:
Memory Management
Enter the number of memory Block: 4
Enter the size of each memmory block :
920
630
450
870
Enter the number of process:6
Enter the memory size of each process:
320
510
120
400
990
520
Select a Memory Management Method:
1.FirstFit.
2.BestFit.
3.WorstFit.

FIRSTFIT
The process of size 320 is allocated in memory block 920
The process of size 510 is allocated in memory block 920
The process of size 120 is allocated in memory block 630
The process of size 400 is allocated in memory block 630
The process of size 990 is not allocated [ It has to wait ]
The process of size 520 is allocated in memory block 870

MEMORY BLOCK
|----------------------------------- |
920 | 320 |
|----------------------------------- |
| 510 |
|----------------------------------- |
| | -1
|----------------------------------- |
630 | 120 |
|----------------------------------- |
| 400 |
|----------------------------------- |
| | -1
|----------------------------------- |
450 | | -1
|----------------------------------- |
870 | 520 |
|----------------------------------- |
| | -1
|----------------------------------- |
WORSTFIT
The process of size 320 is allocated in memory block 920
The process of size 510 is allocated in memory block 870
The process of size 120 is allocated in memory block 630
The process of size 400 is allocated in memory block 920
The process of size 990 is not allocated [ It has to wait ]
The process of size 520 is not allocated [ It has to wait ]

MEMORY BLOCK

|----------------------------------- |
920 | 320 |
|----------------------------------- |
| 400 |
|----------------------------------- |
| | -1
|----------------------------------- |
630 | 120 |
|----------------------------------- |
| | -1
|----------------------------------- |
450 | | -1
|----------------------------------- |
870 | 510 |
|----------------------------------- |
| | -1
|----------------------------------- |

BESTFIT
The process of size 320 is allocated in memory block 450
The process of size 510 is allocated in memory block 630
The process of size 120 is allocated in memory block 630
The process of size 400 is allocated in memory block 870
The process of size 990 is not allocated [ It has to wait ]
The process of size 520 is allocated in memory block 920
MEMORY BLOCK
|----------------------------------- |
920 | 520 |
|----------------------------------- |
| | -1
|----------------------------------- |
630 | 510 |
|----------------------------------- |
| 120 |
|----------------------------------- |
450 | 320 |
|----------------------------------- |
| | -1
|----------------------------------- |
870 | 400 |
|----------------------------------- |
| | -1
|----------------------------------- |
Contiguous Memory Allocation
#include<stdio.h>
#include<conio.h>
#define alloc 1
#define notalloc 0
int disk,cp_disk,i=0;
void displ(void);
struct file
{
char name[20];
int sz;
int stat;
}f[20];
void allc()
{
int ch;
disk--;
cp_disk=disk;
while(1)
{
printf("\nEnter the file name: ");
scanf("%s",&f[i].name);
printf("\nEnter the file size: ");
scanf("%d",&f[i].sz);
if(f[i].sz<disk)
{
disk-=f[i].sz;
f[i].stat=alloc;
}
else if(f[i].sz==disk)
{
f[i].stat=alloc;
disk=0;
displ();
getch();
exit(0);
}
else
{
f[i].stat=notalloc;
printf("\nsize of file %s exceeded available space %d",f[i].name,disk);

}
p: printf("\nContinue or not (y/n): ");
scanf("%s",&ch);
if(ch=='n'||ch=='N')
{
displ();
break;
}
else if(ch=='y'||ch=='Y')
{
i++;
continue;
}
else
{
printf("\nInvalid input");
goto p;
}
}
}
void displ()
{
int j=0,temp=0;
printf("\n\t\tFile Allocation Table");
printf("\n\n\t 0|------------|");
for(j=0;j<i+1;j++)
{
if(f[j].stat==alloc)
{
printf("\n\t |\t%s\t|",f[j].name);
temp+=f[j].sz;
printf("\n\t%d|------------|",temp);
}
}
if(disk!=0)
{ printf("\n\t | |-1");
printf("\n\t%d|------------|",cp_disk);
}
}
void main()
{
printf("\nEnter the space of the disk: ");
scanf("%d",&disk);
allc();
getch();
}
OUTPUT:

Enter the file name: vignesh


Enter the file size: 120
Continue or not (y/n): y
Enter the file name: sathish
Enter the file size: 70
Continue or not (y/n): y
Enter the file name: sushin
Enter the file size: 50
Continue or not (y/n): y
Enter the file name: venkat
Enter the file size: 80
size of file venkat exceeded available space 59
Continue or not (y/n): n

File Allocation Table

0 |--------------------------|
| vignesh |
120 |--------------------------|
| sathish |
190 |--------------------------|
| sushin |
240 |--------------------------|
| | -1
299 |--------------------------|
Page replacement: FIFO
#include<stdio.h>
#include<conio.h>
#define not_found 1
#define found 2
int n,frs,i,tab[25][25],j,k,temp,page_fault=0,clear_count=0;
void display(void);
struct fifo
{
int string,stat,pf,x,y;
}p[25];
void display()
{
for(i=0;i<n;i++)
printf("%2d ",p[i].string);
printf("\n");
for(i=0;i<n;i++)
printf("----");
for(i=0;i<frs;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if(j==p[j].x&&i==p[j].y&&p[j].pf===1)
printf("*%d ",tab[i][j]);
else
printf("%2d ",tab[i][j]);
}
}
printf("\n\nThe no. of page faults = %d",page_fault);
}
void main()
{
printf("\nEnter the no. of strings: ");
scanf("%d",&n);
printf("\nEnter the strings: ");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].string);
p[i].stat=0;
p[i].pf=0;
}
printf("\nEnter the frame size: ");
scanf("%d",&frs);
for(i=0;i<n;i++)
for(j=0;j<frs;j++)
tab[j][i]=-1;
for(i=0;i<n;i++)
{
for(j=0;j<frs;j++)
{
if(p[i].string==tab[j][i])
{
clear_count++;
p[i].stat=found;
goto q;
}
else
p[i].stat=not_found;

}
if(p[i].stat==not_found)
{
p[i].pf=1;
temp=(i-clear_count)%frs;
p[i].y=temp;
for(k=i;k<n;k++)
tab[temp][k]=p[i].string;
page_fault++;
}
q: p[i].x=i;
}
display();
getch();
}
OUTPUT:
Enter the no. of strings: 10
Enter the strings: 1
2
3
4
1
2
6
7
3
4
Enter the frame size: 4
1 2 3 4 1 2 6 7 3 4
------------------------------------------------------------------------------------
*1 1 1 1 1 1 *6 6 6 6
-1 *2 2 2 2 2 2 *7 7 7
-1 -1 *3 3 3 3 3 3 3 3
-1 -1 -1 *4 4 4 4 4 4 4

The no. of page faults = 6


Page Replacement: LRU
#include<stdio.h>
#include<conio.h>
#define not_found 1
#define found 2
int n,frs,i,tab[25][25],j,k,temp=0,temp2=0,page_fault=0;
struct lru
{
int string,stat,pf,x,y;
}p[25];
int fr_age[6];
void display()
{
for(i=0;i<n;i++)
printf("%2d ",p[i].string);
printf("\n");
for(i=0;i<n;i++)
printf("----");
for(i=0;i<frs;i++)
{
printf("\n");
for(j=0;j<n;j++)
{ if(j==p[j].x&&i==p[j].y&&p[j].pf==1)
printf("*%d ",tab[i][j]);
else
printf("%2d ",tab[i][j]);
}
}
printf("\n\nThe no. of page faults = %d",page_fault);
}
void main()
{
printf("\nEnter the no. of strings: ");
scanf("%d",&n);
for(i=0;i<6;i++)
fr_age[i]=0;
printf("\nEnter the strings: ");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].string);
p[i].stat=0;
p[i].pf=0;
}
printf("\nEnter the frame size: ");
scanf("%d",&frs);
for(i=0;i<n;i++)
for(j=0;j<frs;j++)
tab[j][i]=-1;
for(i=0;i<n;i++)
{
for(j=0;j<frs;j++)
{
if(p[i].string==tab[j][i])
{
p[i].pf=0;
temp2=j;
p[i].stat=found;
goto q;
}
else
p[i].stat=not_found;
}
if(p[i].stat==not_found)
{ p[i].pf=1;
temp=fr_age[0];
temp2=0;
for(k=0;k<frs;k++)
{
if(temp<fr_age[k])
{
temp2=k;
temp=fr_age[k];
}
}
p[i].y=temp2;
for(k=i;k<n;k++)
tab[temp2][k]=p[i].string;
page_fault++;
}
q:
for(k=0;k<frs;k++)
if(k!=temp2)
fr_age[k]+=i*i+1;
p[i].x=i;
}
display();
getch();
}
OUTPUT:
Enter the no. of strings: 10
Enter the strings: 1
2
3
4
2
1
5
6
7
8
Enter the frame size: 4
1 2 3 4 2 1 5 6 7 8
------------------------------------------------------------------------------------
*1 1 1 1 1 1 1 1 1 *8
-1 *2 2 2 2 2 2 2 *7 7
-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 = 8


Producer-Consumer Problem
#include<stdio.h>
#include<conio.h>
#include<assert.h>
#define empty -1
#define produce 1
#define consume 2
int buffsize;
int queue_ptr;
void air_shift(int *buffer)
{
int i;
for(i=0; i<=queue_ptr; i++)
buffer[i]=buffer[i+1];
queue_ptr--;
}
void display(int *buffer)
{
int i;
if(queue_ptr==empty)
printf("\nbuffer empty");
else
{
printf("\n\nThe current processes in buffer is \n\n");
for(i=0; i<=queue_ptr; i++)
printf("%d\t",buffer[i]);
}
}
void main()
{
int buffer[40];
char ch;
int option;
queue_ptr=empty;
printf("\nEnter buffer size:");
scanf("%d", &buffsize);
do
{
printf("\nEnter the option:\t1.Produce 2.Consume\noption:");
scanf("%d", &option);
switch(option)
{
case produce:
if(queue_ptr<buffsize-1)
{
printf("\nEnter the data(pid) that you want to feed:");
scanf("%d",&buffer[++queue_ptr]);
}
else
printf("\nOverflow.. Sorry buffer is full.. Please
consume one process..");
break;
case consume:
if(queue_ptr==empty)
printf("\nUnderflow.. Sorry, no space in buffer..Please
produce at least one process");
else
{
printf("\nThe pid of process you consumed is ->%d",
buffer[0]);
air_shift(buffer);
}
break;
default:
printf("\nInvalid option");
}
display(buffer);
printf("\nWant to continue..(y/n):");
fflush(stdin);
scanf("%c", &ch);
}while(ch=='y'||ch=='n');
printf("\nPress any key to exit!!");
getch();
}
OUTPUT:
Enter the buffer size: 3
Enter the option: 1.Produce 2.Consume
option:1

Enter the data(pid) that you want to feed:11

The current processes in buffer is


11
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option:1

Enter the data(pid) that you want to feed:22

The current processes in buffer is


11 22
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option:1

Enter the data(pid) that you want to feed:33

The current processes in buffer is


11 22 33
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option:1

Overflow.. Sorry buffer is full.. Please consume one process..

The current processes in buffer is


11 22 33
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option :2

The pid of process you consumed is 11


The current processes in buffer is
22 33
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option :2

The pid of process you consumed is 22

The current processes in buffer is


33
Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option :2

The pid of process you consumed is 33

buffer empty

Want to continue..(y/n):y

Enter the option: 1.Produce 2.Consume


option:2

Underflow.. Sorry, no space in buffer.. Please produce at least one process

buffer empty

Want to continue..(y/n):n
Press any key to exit!!

Vous aimerez peut-être aussi