remplacement du lecteur rédacteur par les lock
This commit is contained in:
parent
a16d92ab7e
commit
f9de7f93bc
4
Makefile
4
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
|
||||
|
@ -183,7 +183,7 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, Mach
|
||||
SC_LOCK_CREATE => 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!(),
|
||||
|
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
29
test/syscall_tests/lock.c
Normal file
29
test/syscall_tests/lock.c
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user