Académique Documents
Professionnel Documents
Culture Documents
#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:
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
buffer empty
Want to continue..(y/n):y
buffer empty
Want to continue..(y/n):n
Press any key to exit!!