From f9de7f93bc6efc740ab892d364021a09324b85cb Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 10 May 2023 12:51:12 +0200 Subject: [PATCH] =?UTF-8?q?remplacement=20du=20lecteur=20r=C3=A9dacteur=20?= =?UTF-8?q?par=20les=20lock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 +- src/kernel/exception.rs | 2 +- test/syscall_tests/Makefile | 2 +- test/syscall_tests/lecteur_redacteur.c | 60 -------------------------- test/syscall_tests/lock.c | 29 +++++++++++++ 5 files changed, 33 insertions(+), 64 deletions(-) delete mode 100644 test/syscall_tests/lecteur_redacteur.c create mode 100644 test/syscall_tests/lock.c diff --git a/Makefile b/Makefile index 7d23052..3bf184c 100644 --- a/Makefile +++ b/Makefile @@ -48,8 +48,8 @@ pc: syscall matmult: syscall ${CARGO} -x ./target/guac/matmult.guac -d2 -lr: syscall - ${CARGO} -x ./target/guac/lecteur_redacteur.guac -d2 +lock: syscall + ${CARGO} -x ./target/guac/lock.guac -d2 prints: syscall ${CARGO} -x ./target/guac/prints.guac -d2 diff --git a/src/kernel/exception.rs b/src/kernel/exception.rs index 3252b85..8505a87 100644 --- a/src/kernel/exception.rs +++ b/src/kernel/exception.rs @@ -183,7 +183,7 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result sc_lock_create(machine, system), SC_LOCK_DESTROY => sc_lock_destroy(machine, system), SC_LOCK_ACQUIRE => sc_lock_acquire(machine, system), - SC_LOCK_RELEASE => todo!(), + SC_LOCK_RELEASE => sc_lock_release(machine, system), SC_COND_CREATE => todo!(), SC_COND_DESTROY => todo!(), SC_COND_WAIT => todo!(), diff --git a/test/syscall_tests/Makefile b/test/syscall_tests/Makefile index 25f5240..fbea48b 100644 --- a/test/syscall_tests/Makefile +++ b/test/syscall_tests/Makefile @@ -1,4 +1,4 @@ -PROGRAMS = halt.guac prints.guac producteur_consommateur.guac lecteur_redacteur.guac join.guac matmult.guac +PROGRAMS = halt.guac prints.guac producteur_consommateur.guac lock.guac join.guac matmult.guac TOPDIR = ../.. include $(TOPDIR)/Makefile.rules diff --git a/test/syscall_tests/lecteur_redacteur.c b/test/syscall_tests/lecteur_redacteur.c deleted file mode 100644 index bd6c9ef..0000000 --- a/test/syscall_tests/lecteur_redacteur.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "userlib/syscall.h" -#include "userlib/libnachos.h" - -SemId red; -SemId mutex; -SemId util; -int nblect = 0; -int nbWrite = 0; - -void lecteur() { - while(1) { - P(red); - P(mutex); - if (nblect == 0) { - V(util); - } - nblect++; - V(mutex); - V(red); - n_printf("Lecture de l'information \n"); - if(nbWrite == 10) - return; - P(mutex); - nblect--; - if(nblect == 0) { - V(util); - } - V(mutex); - } - Exit(nblect); -} - -void redacteur() { - while(1) { - P(red); - P(util); - V(red); - n_printf((char*)"Ecriture de l'information\n"); - nbWrite++; - if(nbWrite == 10) - return; - V(util); - } - Exit(nbWrite); -} - -int main() { - red = SemCreate((char*)"redacteur", 1); - mutex = SemCreate((char*)"mutex lecteur redacteur", 1); - util = SemCreate((char*)"Mutex util lecteur redacteur", 1); - ThreadId lecteurTh = threadCreate((char*)"Lecteur", (VoidNoArgFunctionPtr) lecteur); - ThreadId lecteur1 = threadCreate((char*)"Lecteur", (VoidNoArgFunctionPtr) lecteur); - ThreadId lecteur2 = threadCreate((char*)"Lecteur", (VoidNoArgFunctionPtr) lecteur); - ThreadId redacteurTh = threadCreate((char*)"redacteur", (VoidNoArgFunctionPtr) redacteur); - Join(lecteurTh); - Join(lecteur1); - Join(lecteur2); - Join(redacteurTh); - return 0; -} diff --git a/test/syscall_tests/lock.c b/test/syscall_tests/lock.c new file mode 100644 index 0000000..a592bd9 --- /dev/null +++ b/test/syscall_tests/lock.c @@ -0,0 +1,29 @@ +#include "userlib/syscall.h" +#include "userlib/libnachos.h" + +LockId mutex; +int glob_int = 0; + +void increment(void){ + LockAcquire(mutex); + glob_int++; + LockRelease(mutex); +} + +void counter(int n){ + for (int i = 0; i < 50; i++){ + increment(); + } +} + +int main(void){ + mutex = LockCreate("Lock_debug"); + + ThreadId th1 = threadCreate("Thread1", (VoidNoArgFunctionPtr) counter); + ThreadId th2 = threadCreate("Thread2",(VoidNoArgFunctionPtr) counter); + + Join(th1); + Join(th2); + Exit(glob_int); + return 0; +}