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

Commit

Permalink
Aggiunti semafori con implementazione generica
Browse files Browse the repository at this point in the history
Aggiornato uso di semafori per handler uMPS #20
Aggiornato uso di semafori per Syscall 4-5 #19
Aggiunte funzioni P e V per semafori in utilities
Aggiunta scheduler_RemoveProcess per rimuovere un preciso pcb dallo scheduler #12
  • Loading branch information
jjak0b committed May 5, 2020
1 parent 7146b2e commit 81fac62
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 29 deletions.
5 changes: 4 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ system_s = dir_s + '/system'
handler_s = dir_s + '/handler'
sysshared_s = system_s + '/shared'
device_s = sysshared_s + '/device'
utilities_f = dir_s + '/utilities'

# SOURCE dir specifiche per architettura
uarm_s = dir_s + '/uarm'
Expand Down Expand Up @@ -93,6 +94,8 @@ pcb_utils_f = pcb_s + '/utils'
#--------------------
scheduler_f = scheduler_s + '/scheduler'

# Utilities Module
util_semaphore_f = utilities_f + '/semaphore'
#System Module
#--------------------

Expand Down Expand Up @@ -173,7 +176,7 @@ for i,x in enumerate(umps_headers_list):

# Source (NOEXT) lists
#-------------------
shared_noext_list = [main_f, p2test_f, test_f, handler_f, scheduler_f, pcb_f, pcb_utils_f, asl_f, shared_device_f ]
shared_noext_list = [main_f, p2test_f, test_f, handler_f, scheduler_f, pcb_f, pcb_utils_f, asl_f, shared_device_f, util_semaphore_f ]

# Per favore, lascia i file crtso____ e lib_____ per ultimi
uarm_noext_list = [uarm_shared_f, uarm_handler_f, uarm_sysinit_f, crtso_uarm, libuarm, libdiv_uarm]
Expand Down
10 changes: 10 additions & 0 deletions include/scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ int scheduler_CreateProcess( memaddr func, int priority );
*/
void scheduler_AddProcess( pcb_t *p );

/**
* @brief Rimuove il descrittore di processo dalla ready queue ed eventualmente disassocia il processo attuale, se è lo stesso pcb fornito
*
* @param p pcb da rimuovere dallo scheduler
* @return int
* TRUE se è stato rimosso con successo
* FALSE se non è presente nella ready queue
*/
int scheduler_RemoveProcess( pcb_t *p );

/**
* @brief wrapper di pcb_RemoveProgenyQ con passata la ready queue dello scheduler
* @PostCondition Se p è il processo in esecuzione allora viene deassociato nella struttura dello scheduler e deallocato.
Expand Down
29 changes: 29 additions & 0 deletions include/utilities/semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _UTIL_SEMAPHORE_
#define _UTIL_SEMAPHORE_

#include <pcb/pcb.h>

/**
* @brief Decrementa il valore del semaforo, se diventa < 0, allora il processo indicato viene sospeso;
* cioè rimosso dalla ready queue dello scheduler e posto in attesa sulla coda del semaforo specificato
* @PreCondition semkey != NULL
*
* @param semkey
* @param p
* @return int
* FALSE se è stato sospeso correttamente
* TRUE se p == NULL oppure se non è stato possibile allocare un semd per il processo specificato
*/
int semaphore_P( int *semkey, pcb_t * p );

/**
* @brief Incrementa il valore del semaforo, se il suo valore diventa <= 0, allora il primo processo in coda
* associato al semaforo specificato è risvegliato; cioè aggiunto alla ready queue
* @PreCondition semkey != NULL
*
* @param semkey
* @return pcb_t* il puntatore al pcb risvegliato
*/
pcb_t *semaphore_V( int *semkey );

#endif
33 changes: 13 additions & 20 deletions src/handler/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <asl/asl.h>
#include <pcb/pcb.h>
#include <system/shared/device/device.h>
#include <utilities/semaphore.h>

HIDDEN state_t specPassup[3]; /* stati del processore dedicati a handler di livello superiore specifici */
HIDDEN byte bitmap_specPassup; /* bitmap con i flag settati sulle i-esime posizioni se la specPassup[ i ] è assegnata */
Expand Down Expand Up @@ -126,26 +127,16 @@ int Sys3_TerminateProcess( void *pid ) {
}

void Sys4_Verhogen( int* semaddr ) {
pcb_t *blocked = removeBlocked( semaddr );
if( blocked != NULL ) {
/* dovremmo ripristinare la priorità ? */
scheduler_AddProcess( blocked );
}
else {
(*semaddr) ++;
}
semaphore_V( semaddr );
/* dovremmo ripristinare la priorità ? */
}

void Sys5_Passeren( int* semaddr ) {
if( *semaddr <= 0 ) {
int b_result = scheduler_StateToWaiting( semaddr );
// nel caso ritornasse 1 non può accadere perchè ad ogni processo può essere nella ASL al massimo 1 volta
// infatti se non fosse disponibile un semd, non esisterebbe neanche questo processo
if( b_result == 1 ) scheduler_StateToTerminate( FALSE );
// sia in caso ritorni 0 o -1 lo scheduler deve proseguire
}
else {
(*semaddr) --;
int status = semaphore_P( semaddr, scheduler_GetRunningProcess() );
// nel caso ritornasse 1 non può accadere perchè ad ogni processo può essere nella ASL al massimo 1 volta
// infatti se non fosse disponibile un semd, non esisterebbe neanche questo processo
if( status == 1 ) {
scheduler_StateToTerminate( FALSE );
}
}

Expand All @@ -155,9 +146,12 @@ void Sys6_DoIO( word command, word *devregAddr, int subdevice ) {
device_GetInfo( devreg, &devLine, &devNo );

int *semKey = device_GetSem( devLine, devNo, subdevice );
if( *semKey > 0 ) {
--(*semKey);
// un semaforo di un device è sempre almeno inizializzato a 0, e quindi è sempre sospeso
int b_error = semaphore_P( semKey, scheduler_GetRunningProcess() );
if( b_error ) {
scheduler_StateToTerminate( FALSE );
}

if( devLine == IL_TERMINAL ) {
if( subdevice ) {
devreg->term.transm_command = command;
Expand All @@ -169,7 +163,6 @@ void Sys6_DoIO( word command, word *devregAddr, int subdevice ) {
else {
devreg->dtp.command = command;
}
scheduler_StateToWaiting( semKey );
}

int Sys7_SpecPassup( state_t* currState, int type, state_t *old_area, state_t *new_area ) {
Expand Down
11 changes: 4 additions & 7 deletions src/handler/umps/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <shared/device/device.h>
#include <shared/device/terminal.h>
#include <utilities/semaphore.h>

int get_interrupting_line( state_t *request );

Expand Down Expand Up @@ -145,13 +146,9 @@ void Handler_Interrupt(void) {
}

int *sem = device_GetSem( line, dev, GET_SEM_OFFSET(dev_reg, line) ); /*sem associated with device*/
if(++(*sem) <= 0){ /*V on this sem*/
pcb_t *p = removeBlocked(sem);
if(p!=NULL){
p->p_s.reg_v0 = dev_status;
/*add process to ready queue*/
scheduler_AddProcess(p);
}
pcb_t *p = semaphore_V( sem );
if( p != NULL ){
p->p_s.reg_v0 = dev_status;
}
scheduler_schedule(FALSE);
}
Expand Down
11 changes: 11 additions & 0 deletions src/scheduler/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,14 @@ int scheduler_CreateProcess( memaddr func, int priority ) {
void scheduler_AddProcess( pcb_t *p ) {
insertProcQ( &scheduler->ready_queue, p );
}

int scheduler_RemoveProcess( pcb_t *p ) {
if( scheduler->running_p == p ) { /* rimuove quello attuale */
scheduler->running_p = NULL;
scheduler->b_force_switch = TRUE;
return TRUE;
}
else { /* rimuove dalla coda */
return NULL != outProcQ( &(scheduler->ready_queue), p );
}
}
5 changes: 4 additions & 1 deletion src/system/shared/device/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
HIDDEN int _semdev[SEM_DEV_N];

void device_init() {

/* semafori device inizializzati a 0, in modo che attendano fino alla risposta (interrupt) */
int i;
for( i = 0; i < SEM_DEV_N; i++ )
_semdev[ i ] = 0;
}

void device_GetInfo( devreg_t *devreg, int *_line, int *_devNo ) {
Expand Down
26 changes: 26 additions & 0 deletions src/utilities/semaphore.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <utilities/semaphore.h>
#include <scheduler/scheduler.h>
#include <asl/asl.h>
#include <pcb/pcb.h>

int semaphore_P( int *semkey, pcb_t * p ) {
int b_error = FALSE;
if( p == NULL )
return !b_error;

if( --(*semkey) < 0 ) {
scheduler_RemoveProcess( p );
b_error = insertBlocked( semkey, p );
}
return b_error;
}

pcb_t *semaphore_V( int *semkey ) {
pcb_t *p = NULL;
if( ++(*semkey) <= 0 ) {
p = removeBlocked( semkey );
if( p != NULL )
scheduler_AddProcess( p );
}
return p;
}

0 comments on commit 81fac62

Please sign in to comment.