From 8dc5f0528644fea73019a862500a52895f6fd2a4 Mon Sep 17 00:00:00 2001 From: jjak0b <32840785+jjak0b@users.noreply.github.com> Date: Mon, 11 May 2020 22:56:43 +0200 Subject: [PATCH] Fix semafori e gestione sospensione processo #12 --- src/handler/shared.c | 24 +++++++++++++----------- src/scheduler/scheduler.c | 12 +++--------- src/utilities/semaphore.c | 9 +++++++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/handler/shared.c b/src/handler/shared.c index a5e63c1..b21fbfa 100644 --- a/src/handler/shared.c +++ b/src/handler/shared.c @@ -156,21 +156,23 @@ void Sys6_DoIO( word command, word *devregAddr, int subdevice ) { int *semKey = device_GetSem( devLine, devNo, subdevice ); pcb_t *pid = scheduler_GetRunningProcess(); // un semaforo di un device è sempre almeno inizializzato a 0, e quindi è sempre sospeso - int b_error = semaphore_P( semKey, pid ); - if( b_error ) { - scheduler_StateToTerminate( pid, FALSE ); - } - - if( devLine == IL_TERMINAL ) { - if( subdevice ) { - devreg->term.transm_command = command; + if( *semKey >= 0 ){ + if( devLine == IL_TERMINAL ) { + if( subdevice ) { + devreg->term.transm_command = command; + } + else { + devreg->term.recv_command = command; + } } else { - devreg->term.recv_command = command; + devreg->dtp.command = command; } } - else { - devreg->dtp.command = command; + + int b_error = semaphore_P( semKey, pid ); + if( b_error ) { + scheduler_StateToTerminate( pid, FALSE ); } } diff --git a/src/scheduler/scheduler.c b/src/scheduler/scheduler.c index 64d2b6f..47884e3 100644 --- a/src/scheduler/scheduler.c +++ b/src/scheduler/scheduler.c @@ -98,16 +98,10 @@ int scheduler_StateToWaiting( int* semKey ) { if( p == NULL ) { return -1; } - + scheduler_StateToReady(); + scheduler_RemoveProcess( p ); int b_result = insertBlocked( semKey, p ); - if( !b_result ) { - scheduler->running_p = NULL; - scheduler->b_force_switch = TRUE; - return 0; - } - else { - return 1; - } + return b_result; } /** diff --git a/src/utilities/semaphore.c b/src/utilities/semaphore.c index 088e086..0768897 100644 --- a/src/utilities/semaphore.c +++ b/src/utilities/semaphore.c @@ -9,8 +9,13 @@ int semaphore_P( int *semkey, pcb_t * p ) { return !b_error; if( --(*semkey) < 0 ) { - scheduler_RemoveProcess( p ); - b_error = insertBlocked( semkey, p ); + if( p == scheduler_GetRunningProcess() ){ + b_error = scheduler_StateToWaiting( semkey ); + } + else { + scheduler_RemoveProcess( p ); + b_error = insertBlocked( semkey, p ); + } } return b_error; }