功能函数:
3.打印所有模拟分配序列
思路:
使用递归调用。找到符合条件的i进程,tmp_ProcessSet的第i位置为count,再调用这个函数,其中形参count+1,
最后找不到符合的进程时,判断是否所有进程都分配,若都分配完成则输出当中的序列。
程序:
void allocate_list(int source_count,int process_count,int **allocation,int **need,int *CurrentAvailable,int *ProcessSet,int count)
{
int i;
int j;
int k;
int *tmp_CurrentAvailable;
int *tmp_ProcessSet;
tmp_CurrentAvailable = (int *)malloc(sizeof(int) * source_count);
tmp_ProcessSet = (int *)malloc(sizeof(int) * process_count);
//拷贝
for(i = 0; i < process_count; i++)
{
tmp_ProcessSet[i] = ProcessSet[i];
}
for(j = 0; j < source_count; j++)
{
tmp_CurrentAvailable[j] = CurrentAvailable[j];
}
j = 0;
while(j < process_count)
{
if(IN == tmp_ProcessSet[j])//找是否有可分配的
{
for(k = 0; k < source_count; k++)//每个资源是否符合
{
if(need[j][k] > tmp_CurrentAvailable[k])
{
break;
}
}
if(k == source_count)//符合
{
tmp_ProcessSet[j] = count;
for(k = 0; k < source_count; k++)
{
tmp_CurrentAvailable[k] += allocation[j][k];
}
allocate_list(source_count,process_count,allocation,need,tmp_CurrentAvailable,tmp_ProcessSet,count + 1);
//拷贝
for(i = 0; i < process_count; i++)
{
tmp_ProcessSet[i] = ProcessSet[i];
}
for(k = 0; k < source_count; k++)
{
tmp_CurrentAvailable[k] = CurrentAvailable[k];
}
}
}
j++;
}
for(i = 0; i < process_count; i++)
{
if(IN == tmp_ProcessSet[i])
{
break;
}
}
if(i == process_count)//都排完了
{
print_list(process_count,tmp_ProcessSet);
}
free(tmp_CurrentAvailable);
free(tmp_ProcessSet);
}