[英]How to search array from last index?
My program is suppose to replicate the memory management function Next-fit. 我的程序假设要复制内存管理功能Next-fit。 Essentially my program is suppose to have a job and a partition. 基本上我的程序假设有一个作业和一个分区。 It will check to see if the job is less than or equal to the partition size. 它将检查作业是否小于或等于分区大小。 If it is less than or equal to then the job is stored. 如果小于或等于,则存储作业。 A value (index) should hold in which partition the job was stored in last and starts searching from that location. 值(索引)应该保存作业最后存储在哪个分区中,并从该位置开始搜索。
Problem: I do not know how to continuously call the search from the where the last job was stored. 问题:我不知道如何从最后一个作业的存储位置继续调用搜索。
void Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
Partition temp;
int Next;
int index;
//loops the amount of jobs
for (int i = 0; i < numberOfJobs; i++)
{
//job less than or = partition
if (job[i].jobSize <= partitionArray[i].size)
{
//partition = partition - job
partitionArray[i].size = partitionArray[i].size -job[i].jobSize;
cout << "Unused space (Hole): " << partitionArray[i].size << "Kb" << endl;
cout << endl;
}
//index becomes the location of where the last job was stored
index = i;
//relaunch loop to search from point index
}
void Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
int j = 0;
int count=0;
int locationOfMemory;
//finds the largest partition
int maxsize = partitionArray[0].size;
for (int l = 0; l < partitionArraySize; l++)
{
if (maxsize <= partitionArray[l].size)
{
maxsize = partitionArray[l].size;
}
}
//loops the amount of jobs
for (int i = 0; i < numberOfJobs; i++)
{
if (job[i].jobSize < maxsize)
{
//job less than or = partition
if (job[i].jobSize <= partitionArray[j].size)
{
locationOfMemory = j+1;
//partition = partition - job
cout << "Job: " << i + 1 << " was allocated in Partition: " << locationOfMemory<< endl;
partitionArray[j].size = partitionArray[j].size - job[i].jobSize;
cout << "Unused space (Hole): " << partitionArray[j].size << "Kb" << endl;
cout << endl;
if (j != partitionArraySize - 1)
{
j++;
}
else
{
j = 0;
}
}
else
{ //if the job is larger than the partition stay at the current job and increment the partition index to the next one
i = i - 1;
j = j + 1;
}
}
else
{
cout << "Job" << "is too big and cannot be allocated" << endl;
}
}
//partition busy or not
for (int m = 0; m < partitionArraySize; m++)
{
if (partitionArray[m].size == 0)
{
count = m + 1;
cout << "Partition # " << count << " Status: Busy" << endl;
}
else
{
count = m + 1;
cout << "Partition # " << count << " Status: Free" << endl;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.