Programar em C/Algoritmo de alocação: diferenças entre revisões

[edição verificada][revisão pendente]
Conteúdo apagado Conteúdo adicionado
Torneira (discussão | contribs)
Sem resumo de edição
Linha 1:
==first fist==
 
==best fit==
 
<syntaxhighlight lang="c">
#include <stdio.h>
#include <windows.h>
 
int main(){
int p,m;
printf("Enter number of processes:");
scanf("%d",&p);
printf("Enter number of Memory blocks:");
scanf("%d",&m);
int parr[p];
struct mem{
int id;
int size;
}marr[m];
 
int i;
 
for(i=0;i<p;i++)
{
printf("Enter size of process %d:",i+1);
scanf("%d",&parr[i]);
}
for(i=0;i<m;i++)
{
printf("Enter size of memory %d:",i+1);
scanf("%d",&marr[i].size);
marr[i].id=i+1;
}
int j;
int size = 0;
for(i; size <= marr[i].size; i++ )
size = marr[i].size;
int old_size = size ;
int im ;
int ref = 0;
for(i=0;i<p;i++){
for(j=0;j<m;j++){
if(marr[j].size>=parr[i]){
if(marr[j].size < size){
im = j;
size = marr[j].size;
ref = 1 ;
}
}
}
if(ref){
ref = 0;
marr[im].size-=parr[i];
size = old_size ;
printf("Allocating process %d to memory %d\n Size remaining in it after allocation %d\n\n",i+1,marr[im].id,marr[im].size);
}else {printf("Not enough memory for process %d",i);break;}
}
system ("pause");
return 0;
}
 
</syntaxhighlight>
 
==worst fit==