diff --git a/ANALE-1/RESOLUTION/Makefile b/ANALE-1/RESOLUTION/Makefile new file mode 100644 index 0000000..097fdb0 --- /dev/null +++ b/ANALE-1/RESOLUTION/Makefile @@ -0,0 +1,19 @@ +CFLAGS=-Wall -Wextra -g +OBJS=p1.o p2.o p3.o dijkstra.o launcher.o +all: p1 p2 p3 launcher + +p1: p1.o dijkstra.o +p2: p2.o dijkstra.o +p3: p3.o dijkstra.o +launcher: launcher.o dijkstra.o + +p1.o: p1.c dijkstra.h commun.h +p2.o: p2.c dijkstra.h commun.h +p3.o: p3.c dijkstra.h commun.h +launcher.o: launcher.c dijkstra.h commun.h +dijkstra.o: dijkstra.c dijkstra.h + +.PHONY: clean +clean: + rm $(OBJS) p1 p2 p3 launcher + diff --git a/ANALE-1/RESOLUTION/commun.h b/ANALE-1/RESOLUTION/commun.h new file mode 100644 index 0000000..474258b --- /dev/null +++ b/ANALE-1/RESOLUTION/commun.h @@ -0,0 +1,8 @@ +#ifndef COMMUN_H +#define COMMUN_H + +#define CLE_SEM_T1 10 +#define CLE_SEM_T2 20 +#define CLE_SEM_T3 30 + +#endif diff --git a/ANALE-1/RESOLUTION/dijkstra.c b/ANALE-1/RESOLUTION/dijkstra.c new file mode 100644 index 0000000..135570d --- /dev/null +++ b/ANALE-1/RESOLUTION/dijkstra.c @@ -0,0 +1,57 @@ +#include +#include +#include + +#include "dijkstra.h" + +int sem_create(key_t cle, int initval) { + int semid; + union semun { int val; struct semid_ds *buf; ushort *array; } arg_ctl; + + semid = semget(cle, 1 , IPC_CREAT|IPC_EXCL|0666); + if (semid == -1) + { + semid = semget(cle,1, 0666); + if (semid == -1) + { + perror("Erreur semget()"); + exit(1); + } + } + else + { + arg_ctl.val = initval; + if (semctl(semid, 0, SETVAL, arg_ctl) == -1) + { + perror("Erreur initialisation sémaphore"); + exit(1); + } + } + return(semid) ; +} + +void P(int semid) +{ + struct sembuf sempar; + sempar.sem_num = 0; + sempar.sem_op = -1; + sempar.sem_flg = 0; + if (semop(semid, &sempar, 1) == -1) + perror("Erreur operation P"); +} + +void V(int semid) +{ + struct sembuf sempar; + sempar.sem_num = 0; + sempar.sem_op = 1; + sempar.sem_flg = 0; + if (semop(semid, &sempar, 1) == -1) + perror("Erreur opération V"); +} + +void sem_delete(int semid) +{ + if (semctl(semid,0,IPC_RMID,0) == -1) + perror("Erreur dans destruction sémaphore"); +} diff --git a/ANALE-1/RESOLUTION/dijkstra.h b/ANALE-1/RESOLUTION/dijkstra.h new file mode 100644 index 0000000..25abaef --- /dev/null +++ b/ANALE-1/RESOLUTION/dijkstra.h @@ -0,0 +1,27 @@ +#ifndef DIJKSTRA_H +#define DIJKSTRA_H + +#include + +/** + * return the semaphore associated to the key cle + * if the semaphore doesn't already exist the function create it with initval token then return it + */ +int sem_create(key_t cle, int initval); + +/** + * take a token on the semaphore semid or wait if none available + */ +void P(int semid); + +/** + * put a token on the semaphore semid + */ +void V(int semid); + +/** + * delete the semaphore semid + */ +void sem_delete(int semid); + +#endif diff --git a/ANALE-1/RESOLUTION/dijkstra.o b/ANALE-1/RESOLUTION/dijkstra.o new file mode 100644 index 0000000..1fe6333 Binary files /dev/null and b/ANALE-1/RESOLUTION/dijkstra.o differ diff --git a/ANALE-1/RESOLUTION/launcher b/ANALE-1/RESOLUTION/launcher new file mode 100755 index 0000000..64e9160 Binary files /dev/null and b/ANALE-1/RESOLUTION/launcher differ diff --git a/ANALE-1/RESOLUTION/launcher.c b/ANALE-1/RESOLUTION/launcher.c new file mode 100644 index 0000000..02193ab --- /dev/null +++ b/ANALE-1/RESOLUTION/launcher.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include "dijkstra.h" +#include "commun.h" + +int main () { + if (fork() == 0) + { + execlp("./p1","./p2",NULL); + } + if (fork() == 0) + { + execlp("./p2","./p2",NULL); + } + if (fork() == 0) + { + execlp("./p3","./p3",NULL); + } + wait(NULL); + wait(NULL); + wait(NULL); + return 0; +} diff --git a/ANALE-1/RESOLUTION/launcher.o b/ANALE-1/RESOLUTION/launcher.o new file mode 100644 index 0000000..cd2d637 Binary files /dev/null and b/ANALE-1/RESOLUTION/launcher.o differ diff --git a/ANALE-1/RESOLUTION/p1 b/ANALE-1/RESOLUTION/p1 new file mode 100755 index 0000000..be072fb Binary files /dev/null and b/ANALE-1/RESOLUTION/p1 differ diff --git a/ANALE-1/RESOLUTION/p1.c b/ANALE-1/RESOLUTION/p1.c new file mode 100644 index 0000000..7c62505 --- /dev/null +++ b/ANALE-1/RESOLUTION/p1.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include "dijkstra.h" +#include "commun.h" + +void t1() +{ + puts("t1"); + sleep(10); + puts("end t1"); +} + +void t2() +{ + puts("t2"); + sleep(2); + puts("end t2"); +} + +int main () { + int sem_t1 = sem_create (CLE_SEM_T1,0); + int sem_t2 = sem_create (CLE_SEM_T2,0); + t1(); + V(sem_t1); + t2(); + V(sem_t2); + return 0; +} diff --git a/ANALE-1/RESOLUTION/p1.o b/ANALE-1/RESOLUTION/p1.o new file mode 100644 index 0000000..85af8fa Binary files /dev/null and b/ANALE-1/RESOLUTION/p1.o differ diff --git a/ANALE-1/RESOLUTION/p2 b/ANALE-1/RESOLUTION/p2 new file mode 100755 index 0000000..765bc19 Binary files /dev/null and b/ANALE-1/RESOLUTION/p2 differ diff --git a/ANALE-1/RESOLUTION/p2.c b/ANALE-1/RESOLUTION/p2.c new file mode 100644 index 0000000..a8dddf2 --- /dev/null +++ b/ANALE-1/RESOLUTION/p2.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include "dijkstra.h" +#include "commun.h" + +void t3() +{ + puts("t3"); + sleep(7); + puts("end t3"); +} + +int main () { + int sem_t1 = sem_create (CLE_SEM_T1,0); + int sem_t3 = sem_create (CLE_SEM_T3,0); + P(sem_t1); + t3(); + V(sem_t3); + return 0; +} diff --git a/ANALE-1/RESOLUTION/p2.o b/ANALE-1/RESOLUTION/p2.o new file mode 100644 index 0000000..ed48234 Binary files /dev/null and b/ANALE-1/RESOLUTION/p2.o differ diff --git a/ANALE-1/RESOLUTION/p3 b/ANALE-1/RESOLUTION/p3 new file mode 100755 index 0000000..0ace242 Binary files /dev/null and b/ANALE-1/RESOLUTION/p3 differ diff --git a/ANALE-1/RESOLUTION/p3.c b/ANALE-1/RESOLUTION/p3.c new file mode 100644 index 0000000..b5df4ba --- /dev/null +++ b/ANALE-1/RESOLUTION/p3.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include "dijkstra.h" +#include "commun.h" + +void t4() +{ + puts("t4"); + sleep(5); + puts("end t4"); +} + +int main () { + int sem_t2 = sem_create (CLE_SEM_T2,0); + int sem_t3 = sem_create (CLE_SEM_T3,0); + P(sem_t2); + P(sem_t3); + t4(); + return 0; +} diff --git a/ANALE-1/RESOLUTION/p3.o b/ANALE-1/RESOLUTION/p3.o new file mode 100644 index 0000000..dea1b1e Binary files /dev/null and b/ANALE-1/RESOLUTION/p3.o differ