Fragmentation
"Fragmentation is a process of data storage in which memory space is used inadequately,
decreasing ability or efficiency and sometimes both."
Internal fragmentation is the wasted space within each allocated block because of rounding up
from the actual requested allocation to the allocation granularity.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, p,a[10],c[15],temp=0,total=0;
float b,t;
printf("Enter the total memory: ");
scanf("%f",&t);
printf("Enter the processes: ");
scanf("%d", &p);
b=t/p;
for(i=0;i<p;i++)
a[i]=b;
for(i=0;i<p;i++)
{
lable: printf("Enter memory for %d process: ",i);
scanf("%d",&temp);
if(temp<=b)
{
c[i]=a[i]-temp;
printf("Internal fragmentation for this block: ");
printf("%dn",c[i]);
}
else
{
printf("Required memory is not available");
goto lable;
}
total=total+c[i];
}
printf("Total internal fragmentation %dn",total);
getch();
}
Sample Output
Case 1:
Enter the total memory: 800
Enter the processes: 4
Enter memory for 0 process: 150
Internal fragmentation for this block: 50
Enter memory for 1 process: 100
Internal fragmentation for this block: 100
Enter memory for 2 process: 200
Internal fragmentation for this block: 0
Enter memory for 3 process: 200
Internal fragmentation for this block: 0
Total internal fragmentation 150
Case 2:
Enter the total memory: 1200
Enter the processes: 4
Enter memory for 0 process: 300
Internal fragmentation for this block: 0
Enter memory for 1 process: 300
Internal fragmentation for this block: 0
Enter memory for 2 process: 300
Internal fragmentation for this block: 0
Enter memory for 3 process: 300
Internal fragmentation for this block: 0
Total internal fragmentation 0
External fragmentation: External fragmentation is the various free spaced holes that are
generated in either your memory or disk space.
Sample Code
#include<stdio.h>
#include<conio.h>
void main()
{
int m,i,p[15];
char ch;
printf("Enter memory to be allocated: ");
scanf("%d",&m);
printf("Enter process size : ");
scanf("%d", &p[0]);
i=0;
do
{
m=m-p[i];
printf("nRemaining memory is %dn",m);
abc:printf("Do you want to continue: ");
fflush(stdin);
scanf("%c",&ch);
i++;
if(ch=='y')
{
printf("Enter the process size: ");
scanf("%d",&p[i]);
}
else
{
printf("nExternal fragmentation is %d",m);
break;
}
if(m<p[i])
{
printf("nRequired memory is not availablen");
goto abc;
}
}while((ch=='y')&&(m>=p[i]));
getch();
}
Sample Output
Case 1:
Enter memory to be allocated: 1000
Enter process size : 500
Remaining memory is 500
Do you want to continue: y
Enter the process size: 300
Remaining memory is 200
Do you want to continue: y
Enter the process size: 100
Remaining memory is 100
Do you want to continue: n
External fragmentation is 100
Case 2:
Enter memory to be allocated: 800
Enter process size : 300
Remaining memory is 500
Do you want to continue: y
Enter the process size: 200
Remaining memory is 300
Do you want to continue: y
Enter the process size: 200
Remaining memory is 100
Do you want to continue: y
Enter the process size: 100
Remaining memory is 0
Do you want to continue: n
External fragmentation is 0

Fragmentation

  • 1.
    Fragmentation "Fragmentation is aprocess of data storage in which memory space is used inadequately, decreasing ability or efficiency and sometimes both." Internal fragmentation is the wasted space within each allocated block because of rounding up from the actual requested allocation to the allocation granularity. #include <stdio.h> #include <conio.h> void main() { int i, p,a[10],c[15],temp=0,total=0; float b,t; printf("Enter the total memory: "); scanf("%f",&t); printf("Enter the processes: "); scanf("%d", &p); b=t/p; for(i=0;i<p;i++)
  • 2.
    a[i]=b; for(i=0;i<p;i++) { lable: printf("Enter memoryfor %d process: ",i); scanf("%d",&temp); if(temp<=b) { c[i]=a[i]-temp; printf("Internal fragmentation for this block: "); printf("%dn",c[i]); } else { printf("Required memory is not available"); goto lable; } total=total+c[i]; } printf("Total internal fragmentation %dn",total); getch(); } Sample Output Case 1: Enter the total memory: 800 Enter the processes: 4 Enter memory for 0 process: 150 Internal fragmentation for this block: 50 Enter memory for 1 process: 100
  • 3.
    Internal fragmentation forthis block: 100 Enter memory for 2 process: 200 Internal fragmentation for this block: 0 Enter memory for 3 process: 200 Internal fragmentation for this block: 0 Total internal fragmentation 150 Case 2: Enter the total memory: 1200 Enter the processes: 4 Enter memory for 0 process: 300 Internal fragmentation for this block: 0 Enter memory for 1 process: 300 Internal fragmentation for this block: 0 Enter memory for 2 process: 300 Internal fragmentation for this block: 0 Enter memory for 3 process: 300 Internal fragmentation for this block: 0 Total internal fragmentation 0
  • 4.
    External fragmentation: Externalfragmentation is the various free spaced holes that are generated in either your memory or disk space. Sample Code #include<stdio.h> #include<conio.h> void main() { int m,i,p[15]; char ch; printf("Enter memory to be allocated: "); scanf("%d",&m); printf("Enter process size : "); scanf("%d", &p[0]); i=0; do { m=m-p[i]; printf("nRemaining memory is %dn",m);
  • 5.
    abc:printf("Do you wantto continue: "); fflush(stdin); scanf("%c",&ch); i++; if(ch=='y') { printf("Enter the process size: "); scanf("%d",&p[i]); } else { printf("nExternal fragmentation is %d",m); break; } if(m<p[i]) { printf("nRequired memory is not availablen"); goto abc; } }while((ch=='y')&&(m>=p[i])); getch(); } Sample Output Case 1: Enter memory to be allocated: 1000 Enter process size : 500 Remaining memory is 500
  • 6.
    Do you wantto continue: y Enter the process size: 300 Remaining memory is 200 Do you want to continue: y Enter the process size: 100 Remaining memory is 100 Do you want to continue: n External fragmentation is 100 Case 2: Enter memory to be allocated: 800 Enter process size : 300 Remaining memory is 500 Do you want to continue: y Enter the process size: 200 Remaining memory is 300 Do you want to continue: y Enter the process size: 200 Remaining memory is 100 Do you want to continue: y Enter the process size: 100
  • 7.
    Remaining memory is0 Do you want to continue: n External fragmentation is 0