struct PCB
{
int PID;
int burstTime;
int arrivalTime;
int priorityScore;
int startTime;
int finishTime;
};
struct Queue
{
int front;
int rear;
int length; // Stores the maximum no. of processes that can be stored processes in the queue
int size; // Stores the current no. of processes in the queue
struct PCB **arr; // Array of pointers storing the pointers to PCB. Storing "struct PCB*" type item in arr
};
void arrangeProcess(struct Queue *readyQ)
{
if (isEmpty(readyQ))
{
printf("\nNo elements in Queue.\n");
return;
}
int i = readyQ->front, temp = readyQ->size;
int j, tempj;
struct PCB *key;
i = (i 1) % readyQ->length;
while (i < temp)
{
key = readyQ->arr[i];
j = (i (readyQ->length) - 1) % readyQ->length; // Getting the previous element of i
int lastIndex = (readyQ->front readyQ->length - 1) % readyQ->length;
// The while loop is executed if (j >= readyQ->front) and AT of arr[j] > AT of key
while ((j != lastIndex) && ((readyQ->arr[j]->arrivalTime) > (key->arrivalTime)))
{
tempj = (j 1) % readyQ->length; // Getting the next element of j
readyQ->arr[tempj] = readyQ->arr[j];
j = (j (readyQ->length) - 1) % readyQ->length;
}
tempj = (j 1) % readyQ->length;
readyQ->arr[tempj] = key;
i = (i 1) % readyQ->length;
}
}
這里的主要目的是根據到達時間對readyQ 中的PCB進行排序,我嘗試使用插入排序進行此操作,但是我找不到插入排序的內部回圈為佇列運行的合適條件,直到迭代器i大于等于readyQ的前面元素。如果readyQ已滿,即當readyQ 中存在最后一個元素時,我在程式中撰寫的條件將繼續回圈,否則它將完美運行。
請建議合適的回圈條件,以便即使在readyQ 中存在最后一個元素的情況下代碼也能完美運行
uj5u.com熱心網友回復:
不要使用實際的偏移量。根據 撰寫回圈0..size-1,但實際上比較元素(front i) % length和(front j) % length
void arrangeProcess(struct Queue *readyQ)
{
size_t num_eles = readyQ->size;
if (!num_eles)
return;
size_t base_idx = readyQ->front;
size_t max_eles = readyQ->length;
for (size_t i=0; i<num_eles-1; i) {
size_t ii = ( base_idx i ) % max_eles;
struct PCB *a = readyQ->arr[ii];
for (size_t j=i 1; j<num_eles; j) {
size_t jj = ( base_idx j ) % max_eles;
struct PCB *b = readyQ->arr[jj];
...
}
}
}
它更簡單,更不容易出錯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379870.html
下一篇:使用指標的字符陣列未在C中更新
