Skip to content
This repository has been archived by the owner on Nov 2, 2022. It is now read-only.

Commit

Permalink
Implementate funzionalità a stati dello scheduler #12
Browse files Browse the repository at this point in the history
Implementato sistema di Aging per processi in ready queue
  • Loading branch information
jjak0b committed Mar 20, 2020
1 parent db9e590 commit ac80d67
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 4 deletions.
48 changes: 47 additions & 1 deletion include/scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ struct scheduler_t {

/* coda dei processi in stato ready */
struct list_head ready_queue;

/* puntatore al descrittore del processo in esecuzione */
pcb_t* running_p;

};
typedef struct scheduler_t scheduler_t;
Expand All @@ -21,11 +24,49 @@ int scheduler_init();

/**
* @brief Avvia lo scheduler
* @PreCondition Deve essere richiamato dopo scheduler_init()
*
* @return int
*/
int scheduler_main();

/**
* @brief Se la coda dei processi ready non è vuota,
* Sceglie il processo dalla ready queue con priorità più alta
* Imposta Il timer del processore a TIME_SLICE
* e carica lo stato del processo sul processore
* Dopo questo la funzione non dovrebbe restituire poichè il controllo dell'esecuzione è passato al processo
* Se ciò avviene è dovuto ad un errore di LDST.
* @return int
* * 1 se la coda dei processi è vuota
* * -1 se non è stato caricato lo stato di un nuovo processo, senza cambiare il controllo di esecuzione ( malfunzionamento LDST )
*/
int scheduler_StateToRunning();

/**
* @brief Assegna lo stato del processore fornito al processo corrente;
* effettua un azione di aging per tutti gli elementi nella ready queue;
* ripristina la priorità originale del processo che era in esecuzione
* Disassocia il processo in esecuzione dallo scheduler
*
* @param state stato del processo che era in esecuzione
* @return int
* * 1 se non c'è alcun processo tracciato dallo scheduler in esecuzione
* * 0 altrimenti se l'inserimento in ready queue è avvenuto correttamente
*/
int scheduler_StateToReady( state_t* state );

int scheduler_StateToWaiting();

/**
* @brief Dealloca il descritto del processo che era in esecuzione
*
* @return int
* * 1 se non c'è alcun processo tracciato dallo scheduler in esecuzione
* * 0 altrimenti se è avvenuto tutto correttamente
*/
int scheduler_StateToTerminate();

/**
* @brief Crea un processo, aggiungendolo al ciclo di vita dello scheduler
* Quando il processo sarà scelto, inizierà l'esecuzine a partire dall'indirizzo di funzione fornito
Expand All @@ -35,7 +76,12 @@ int scheduler_main();
*/
int scheduler_CreateProcess( function_t func, int priority );


/**
* @brief Inserisce un descrittore di processo nella ready queue
*
* @param p descrittore del processo
* @return int
*/
int scheduler_AddProcess( pcb_t *p );

#endif
5 changes: 5 additions & 0 deletions include/system/shared/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/

#include <system/system.h>
#include <shared/types.h>

#define TIME_SLICE 0xFFFF0000 /* TODO: da sistemare a valore effettivo */ /* Time slice di CPU dedicato ad ogni processo */

void EnableInterrupts( state_t *state, int b_flag );

Expand All @@ -21,6 +24,8 @@ void SetPC( state_t *state, memaddr value );

void SetSP( state_t *state, memaddr value );

void SetLR( state_t *state, memaddr value );

/**
* @brief Copia before in after
*
Expand Down
6 changes: 6 additions & 0 deletions src/handler/handler.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <handler/handler.h>
#include <scheduler/scheduler.h>
#include <system/shared/shared.h>

void Handler_SysCall( word sysNum, word arg1, word arg2, word arg3 ) {

Expand All @@ -14,4 +16,8 @@ void Handler_TLB( word arg0, word arg1, word arg2, word arg3 ) {

void Handler_Interrupt( word arg0, word arg1, word arg2, word arg3 ) {

/* TIMER INT DETECTED */
if( getCAUSE() == IL_TIMER ) { /* TODO: provvisorio, per ora è SOLO per struttura concettuale */
scheduler_StateToReady( (state_t *) INT_OLDAREA );
}
}
65 changes: 64 additions & 1 deletion src/scheduler/scheduler.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,86 @@
#include <scheduler.h>
#include <system/shared/shared.h>
#include <shared/types.h>

static scheduler_t *scheduler;

int scheduler_init() {
static scheduler_t scheduler_struct; /* Tutte le funzioni di questo Header fanno riferimento implicito a questa struttura */
mkEmptyProcQ( &scheduler_struct.ready_queue );
scheduler_struct.running_p = NULL;
scheduler = &scheduler_struct;
}

int scheduler_main() {
return scheduler_StateToRunning();
}

void scheduler_DoAging() {
struct list_head *it;
pcb_t *dummy;
if( !emptyProcQ( &scheduler->ready_queue ) ) {
list_for_each(it, &scheduler->ready_queue ) {
dummy = headProcQ( it );
dummy->priority += 1;
}
}
}

int scheduler_StateToRunning(){
if( emptyProcQ( &scheduler->ready_queue ) ) {
return 1;
}
scheduler->running_p = removeProcQ( &scheduler->ready_queue );

setTIMER( TIME_SLICE );
LDST( &scheduler->running_p->p_s );
return -1;
}

int scheduler_StateToReady( state_t* state ) {
if( scheduler->running_p == NULL ){
return 1;
}

scheduler_DoAging(); /* Incrementa priorità per evitare starvation dei processi */

moveState( state, &scheduler->running_p->p_s ); /* aggiorno lo stato del pcb con lo stato del processore fornito */

/* ripristino priorita ed inserisco nella coda */
scheduler->running_p->priority = scheduler->running_p->original_priority;
scheduler_AddProcess( scheduler->running_p );
scheduler->running_p = NULL;

return 0;
}

int scheduler_StateToWaiting() {
/* TODO */
}

int scheduler_StateToTerminate() {
if( scheduler->running_p == NULL ){
return 1;
}

freePcb( scheduler->running_p );
scheduler->running_p = NULL;

return 0;
}

int scheduler_CreateProcess( function_t func, int priority ) {
pcb_t* pcb = allocPcb();
if( pcb == NULL ) {
return -1;
}
/* TODO */
SetPC( &pcb->p_s, func );
SetLR( &pcb->p_s, scheduler_StateToTerminate ); /* Temporaneamente questo è l'indirizzo di ritorno */
/* TODO: SetSP( ); SP dovrebbe essere dinamicamente in base al gestore della memoria */
/* TODO: flag status, ecc ... */
EnableKernelMode( &pcb->p_s, FALSE );

scheduler_AddProcess( pcb );
}

int scheduler_AddProcess( pcb_t *p ) {
Expand Down
6 changes: 5 additions & 1 deletion src/system/uarm/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void SetSP( state_t *state, memaddr value ) {
state->sp = value;
}

void SetLR( state_t *state, memaddr value ) {
state->lr = value;
}

void moveState(state_t *before, state_t *after){
after->a1 = before->a1;
after->a2 = before->a2;
Expand All @@ -59,4 +63,4 @@ void moveState(state_t *before, state_t *after){
after->CP15_Cause= before->CP15_Cause;
after->TOD_Hi = before->TOD_Hi;
after->TOD_Low = before->TOD_Low;
}
}
2 changes: 1 addition & 1 deletion src/system/umps/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ void moveState(state_t *before, state_t *after){
for(int i=0; i<STATE_GPR_LEN+1;i++){
after->gpr[i] = before->gpr[i];
}
}
}

0 comments on commit ac80d67

Please sign in to comment.