请使用First-fit(FF),Best-fit(BF)和Worst-fit(WF)算法进行VM Placement。 FF和WF似乎工作得很好,但BF有点问题。为该放置随机生成VM序列。
问题在于,有时当它对PM进行分类并选择资源容量最小的PM时,以及当它检查是否有足够的资源来托管该特定VM时,它会说"等待&#34 ;而不是将其放在具有足够资源的下一个可用PM上。它从一开始就运作良好,但随着流程的继续,它往往会在展示位置中出现这样的错误。
例如,X, S, M, L, S, L, X, L, M, M
展示位置的VM序列应为:
X 2
S 2
M 2
L 1
S 1
L 1
X 1
L 1
M WAIT
M WAIT
而是显示:
X 2
S 2
M 2
L 1
S 1
L 1
X WAIT
L WAIT
M WAIT
M WAIT
我真的不知道代码的哪一部分正在发生这样的错误。如果可以提供帮助,我会很高兴。下面是代码 - 谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#define max 250
#define min 10
int main()
{
int i, k,g, c[max] = {0}, r[max] = {0}, d[max] = {0}, cv[max] = {0},
rv[max] = {0}, dv[max] = {0}, rw[max] = {0}; //declaration of variables
int nf=10; char ch;
float avgpwr=0; //, nb = 3
int m=2;
int b = 1; int ff[10] = {0};
int cm[max] = {0}, rm[max] = {0}, dm[max] = {0};
int a;
for (a=1; a<=b; a++)
{
printf("\n\tVirtual Machine Placement");
printf("\n\t**************************************"); printf("\n");
int flag, j, count=0;
char vm_type[] = {'S', 'M', 'L', 'X'};
printf("\n");
//loop throug array and print values
for (i=1; i<=nf; i++)
{
//generate random index number
int index = my_rand ();
printf("VM %d:",i); printf("%-5c", vm_type[index]);
switch(vm_type[index]) {
case 'S':
cv[i]= 2; rv[i]= 4; dv[i]= 50; rw[i] = 69;
break;
case 'M':
cv[i]= 3; rv[i]= 8; dv[i]= 100; rw[i] = 139;
break;
case 'L':
cv[i]= 5; rv[i]= 12; dv[i]= 150; rw[i] = 207;
break;
case 'X':
cv[i]= 6; rv[i]= 18; dv[i]= 200; rw[i] = 345;
break;
default:
printf("That was not a valid option :(");
}
printf("\n");
}
printf("\n1.First fit 2.Best fit 3.Worst fit\n");
do
{
int nc[10]={0}; int dc[10]={0};
int mc[10]={0}; int rd[10]={0};
int host[10] = {0};
for (i=1; i<=m; i++)
{
if (i==1){
c[i] = 64; r[i] = 64; d[i] = 700; ff[i] = i;
}
if (i==2){
c[i] = 32; r[i] = 32; d[i] = 500; ff[i] = i;
}
}
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nFirst Fit\n");
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(j=1; j<=m; j++)
{
if(cv[i] <= c[j] && rv[i]<= r[j] && dv[i] <= d[j]){
printf("%-15d\t%-15d\t%\n",i,ff[j]); //printf("\n");
count++;
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 || ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
nc[j] = nc[j] + cv[i];
mc[j] = mc[j] + rv[i];
dc[j]= dc[j] + dv[i];
rd[j]= rd[j] + rw[i];
host[j] = host[j]+ 1;
}
break;
}
else
{
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT..\n");
// rejectvm++;
}
}
printf("\n");
//printf("%d , %d , %d , %d \n", nc[i], rd[i], mc[i], dc[i]);
break;
case 2: printf("\nBest Fit\n");
int y1, z1, temp1,temp_r1, temp_d1;
//placing the vms
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(y1=1;y1<=m;y1++)
{
for(z1=y1;z1<=m;z1++)
{
if (c[y1] !=0 && r[y1] !=0 || d[y1] !=0)
{
if(c[y1] > c[z1] && r[y1] > r[z1] && d[y1] > d[z1])
{
temp1 = c[y1]; temp_r1 = r[y1]; temp_d1 = d[y1];
c[y1]= c[z1]; r[y1]= r[z1]; d[y1]= d[z1];
c[z1]=temp1; d[z1]=temp_r1; d[z1]=temp_d1;
temp1=ff[y1];
ff[y1]=ff[z1];
ff[z1]=temp1;
}
}
}
}
for(j=1; j<=m; j++)
{
if( c[j] >= cv[i] && r[j] >= rv[i] && d[j] >= dv[i]){
printf("%-15d\t%-15d\t%\n",i,ff[j]);
count++;
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 ||
ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
//ctr[i] = ctr[i] + 1;
nc[j] = nc[j] + cv[i];
mc[j] = mc[j] + rv[i];
dc[j]= dc[j] + dv[i];
rd[j]= rd[j] + rw[i];
host[j] = host[j]+ 1;
}
break;
}
else {
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT...\n");
}
}
printf("\n");
break;
case 3: printf("\nWorst Fit\n");
int y, z, temp,temp_r, temp_d;
//placing the vms
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(j=1; j<=m; j++)
{
for(y=1;y<=m;y++)
{
for(z=y;z<=m;z++)
{
if(c[y] < c[z] && r[y] < r[z] && d[y] < d[z])
{
temp = c[y]; temp_r = r[y]; temp_d = d[y];
c[y]= c[z]; r[y]= r[z]; d[y]= d[z];
c[z]=temp; r[z]=temp_r; d[z]=temp_d;
temp1=ff[y];
ff[y]=ff[z];
ff[z]=temp1;
}
}
}
if( c[j] >= cv[i] && r[j] >= rv[i] && d[j] >= dv[i]){
printf("%-15d\t%-15d\t%d\n",i,ff[j],j);
count++; g = ff[j];
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 || ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
// ctr[i] = ctr[i] + 1;
nc[g] = nc[g] + cv[i];
mc[g] = mc[g] + rv[i];
dc[g]= dc[g] + dv[i];
rd[g]= rd[g] + rw[i];
host[g] = host[g]+ 1;
}
break;
}
else
{
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT...\n");
}
}
printf("\n");
break;
default:
printf("That was not a valid option :(");
break;
}
}
while(ch<=3);
}
}
int my_rand ()
{
static int first = 0;
if (first == 0)
{
srand (time (NULL));
first = 1;
}
return (rand ()% 4);
}