Przeglądaj źródła

Ajout gestion semaphore

Loquicom 6 lat temu
rodzic
commit
b71faec676
4 zmienionych plików z 188 dodań i 2 usunięć
  1. 3 0
      constante.h
  2. 4 2
      makefile
  3. 115 0
      sem.c
  4. 66 0
      sem.h

+ 3 - 0
constante.h

@@ -39,6 +39,9 @@
 #define SHELLR_ALL 5 // >>&
 #define SHELLRE_ALL 6 // >&
 
+/* --- Semaphore --- */
+#define SEMKEYPATH "/bin/ls"
+
 /* --- Boolean --- */
 #define boolean int
 #define true 1

+ 4 - 2
makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = mysh
-OBJETS = error.o str.o parser.o wildcard.o command.o execute.o
+OBJETS = error.o str.o parser.o wildcard.o command.o execute.o sem.o
 NOM_PROJET = mini-shell
 
 #
@@ -96,7 +96,9 @@ error.o: error.c str.h error.h
 str.o: str.c str.h
 parser.o: parser.c error.h str.h wildcard.h constante.h parser.h
 wildcard.o: wildcard.c error.h wildcard.h constante.h
-command.o: command.c error.h str.h parser.h constante.h command.h
+command.o: command.c error.h str.h parser.h constante.h mysh.h execute.h \
+ command.h
 execute.o: execute.c error.h execute.h constante.h
+sem.o: sem.c error.h sem.h constante.h
 mysh.o: mysh.c error.h str.h parser.h constante.h command.h execute.h \
  mysh.h

+ 115 - 0
sem.c

@@ -0,0 +1,115 @@
+#define _XOPEN_SOURCE
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h> /* Constante option */
+#include <errno.h>
+#include "error.h"
+#include "sem.h"
+
+boolean create_sem(semaphore* sem, int code, int nb) {
+    int id;
+    extern int errno;
+    /* Création du tableau */
+    key_t key = ftok(SEMKEYPATH, code);
+    id = semget(key, nb, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL);
+    if (id == ERR) {
+        /* Si le tableau existe deja */
+        if (errno == EEXIST) {
+            adderror("Le tableau de sémaphore existe deja, tentaive de récupération");
+            return get_sem(sem, code);
+        }
+        /* Sinon erreur */
+        addperror("Impossible de créer le tableau de sémaphore");
+        return false;
+    }
+    /* Paramétrage de la structure */
+    sem->id = id;
+    sem->key = key;
+    sem->nb = nb;
+    /* Retour */
+    return true;
+}
+
+boolean get_sem(semaphore* sem, int code) {
+    int id;
+    struct semid_ds sem_buf;
+    /* Création du tableau */
+    key_t key = ftok(SEMKEYPATH, code);
+    id = semget(key, 0, S_IRUSR | S_IWUSR);
+    if (id == ERR) {
+        /* Sinon erreur */
+        addperror("Impossible de récupèrer le tableau de sémaphore");
+        return false;
+    }
+    /* Récupération du nombre de semaphore */
+    if (semctl(id, 0, IPC_STAT, &sem_buf) == ERR) {
+        addperror("Impossible de récupèrer les informations du tableau de sémaphore");
+        return false;
+    }
+    /* Paramétrage de la structure */
+    sem->id = id;
+    sem->key = key;
+    sem->nb = sem_buf.sem_nsems;
+    /* Retour */
+    return true;
+}
+
+boolean ini_sem(semaphore* sem, int* iniVal) {
+    /* Initialisation des valeurs */
+    if (semctl(sem->id, 0, SETALL, iniVal) == ERR) {
+        addperror("Impossible d'initialiser le tableau de semaphore");
+        return false;
+    }
+    /* Retour */
+    return true;
+}
+
+boolean P(semaphore* sem, int num) {
+    struct sembuf action;
+    /* Le num valide */
+    if (num < 0 || num > (sem->nb - 1)) {
+        adderror("Index tableau semaphore invalide");
+        return false;
+    }
+    /* Parametre sembuf */
+    action.sem_num = num;
+    action.sem_op = -1;
+    /* Puis je */
+    if (semop(sem->id, &action, 1) == ERR) {
+        addperror("Impossible d'effectuer P");
+        return false;
+    }
+    return true;
+}
+
+boolean V(semaphore* sem, int num) {
+    struct sembuf action;
+    /* Le num valide */
+    if (num < 0 || num > (sem->nb - 1)) {
+        adderror("Index tableau semaphore invalide");
+        return false;
+    }
+    /* Parametre sembuf */
+    action.sem_num = num;
+    action.sem_op = 1;
+    /* Vas y */
+    if (semop(sem->id, &action, 1) == ERR) {
+        addperror("Impossible d'effectuer V");
+        return false;
+    }
+    return true;
+}
+
+boolean delete_sem(semaphore* sem) {
+    /* Suppr */
+    if (semctl(sem->id, 0, IPC_RMID) == ERR) {
+        addperror("Impossible de supprimer le tableau de semaphore");
+        return false;
+    }
+    /* Reset struct */
+    sem->id = 0;
+    sem->key = 0;
+    sem->nb = 0;
+    return true;
+}

+ 66 - 0
sem.h

@@ -0,0 +1,66 @@
+#ifndef SEM_H
+#define SEM_H
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include "constante.h"
+
+typedef struct{
+	/* Id retourner par semget */
+	int id;
+	/* Clef d'acces au semaphores */
+	key_t key;
+	/* Nombre de semaphore */
+	int nb;
+}semaphore;
+
+/**
+ * Creation d'un tableau de semaphores
+ * @param semaphore* Le tableau de semaphore
+ * @param int La clef
+ * @param int Le nombre semaphores
+ * @return boolean Reussite
+ */
+boolean create_sem(semaphore*, int, int);
+
+/**
+ * Récuperation d'un tableau de semaphores
+ * @param semaphore* Le tableau de semaphore
+ * @param int La clef
+ * @return boolean Reussite
+ */
+boolean get_sem(semaphore*, int);
+
+/**
+ * Initialise les semaphores
+ * @param semaphore* Le tableau de semaphore
+ * @param int* Valeur à initialiser
+ * @return boolean Reussite
+ */
+boolean ini_sem(semaphore*, int*);
+
+/**
+ * Demande l'acces et decremente le semaphore (Puis je)
+ * @param semaphore* Le tableau de semaphore
+ * @param int Le numero de la semaphore
+ * @return boolean Reussite
+ */
+boolean P(semaphore*, int);
+
+/**
+ * Augmente le compteur d'une semaphore (Vas y)
+ * @param semaphore* Le tableau de semaphore
+ * @param int Le numero de la semaphore
+ * @return boolean Reussite
+ */
+boolean V(semaphore*, int);
+
+/**
+ * Supprime un tableau de semaphores
+ * @param semaphore* Le tableau de semaphore
+ * @return boolean Reussite
+ */
+boolean delete_sem(semaphore*);
+
+#endif