This repository was archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
scheduler : changeScheduling.c\h
sᴀʟᴠᴀᴛᴏʀᴇ ʙ edited this page Aug 22, 2017
·
2 revisions
#define POLICY_PRIORITY 0
#define POLICY_EXEC_CYCLES 1
/* policySheduling = 0 -> PRIORITA (DEFAULT)
* policySheduling = 1 -> ESECUZIONI RIMANENTI
*/
extern int policyScheduling;int policyScheduling = POLICY_PRIORITY; //politica di default
/*
* Cambia la politica di scheduling attuale
*/
void changeScheduling() {
policyScheduling = ((policyScheduling + 1) % 2);
}Lo scambio avviene automaticamente da priorità a esecuzioni, come descritto dalle macro in changeScheduling.h.
/*
* Realizza l'ordinamento della lista attraverso bubblesort
*/
void bubbleSort(ReadyQueue **headQueue) {
if (isEmpty(*headQueue))
return;
bool swapped;
ReadyQueue *current;
do {
swapped = false;
current = *headQueue;
while (current->next != NULL) {
if (compare_policy(current, current->next) > 0) {
swap(current, current->next);
if (current == *headQueue) {
//è stata modificata la testa della lista
(*headQueue) = current->previous;
}
swapped = true;
} else {
current = current->next;
}
}
} while (swapped);
}L’algoritmo di ordinamento si basa su un bubblesort con sentinella, che delega la condizione di swap alla funzione compare_policy() e lo swap effettivo dei nodi alla procedura swap(). Se lo swap di un nodo prevede il cambiamento di head, il valore del puntatore di quest’ultimo viene modificato opportunamente (per far ciò è stato necessario passare un puntatore al puntatore dalla funzione main() ).
/*
* Determina se i due nodi debbano essere scambiati, in base alla politica di * * scheduling corrente.
*/
int compare_policy(ReadyQueue *left, ReadyQueue *right) {
// Priority = DESC //Exec_Cycles = ASC
// If Equal value : Sort by ID DESC
// return 1 se è necessario uno swap, altrimenti -1
if (policyScheduling == POLICY_EXEC_CYCLES) {
return left->task.exec_cycles > right->task.exec_cycles ? 1 :
(left->task.exec_cycles < right->task.exec_cycles ? -1 :
left->task.id < right->task.id ? 1 : -1);
} else {
return left->task.priority > right->task.priority ? -1 :
(left->task.priority < right->task.priority ? 1 :
left->task.id < right->task.id ? 1 : -1);
}
}compare_policy() restituisce 1 se è necessario scambiare il nodo left con il nodo right, altrimenti -1.
void swap(ReadyQueue *left, ReadyQueue *right) {
//MAX 6 puntatori da modificare,
//se left è il primo nodo o right è l'ultimo nodo della lista diventano 5,
//se entrambi i casi diventano 4.
if (right->next != NULL) {
right->next->previous = left;
}
if (left->previous != NULL) {
left->previous->next = right;
}
left->next = right->next;
right->next = left;
right->previous = left->previous;
left->previous = right;
}Made with ❤️ by Owanesh and MatteoMauro | MIT ©