Browse Source

Ajout shm

Loquicom 6 years ago
parent
commit
a3f9d34da9
3 changed files with 192 additions and 2 deletions
  1. 3 2
      makefile
  2. 126 0
      shm.c
  3. 63 0
      shm.h

+ 3 - 2
makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = mysh
-OBJETS = error.o str.o parser.o wildcard.o command.o execute.o sem.o
+OBJETS = error.o str.o parser.o wildcard.o command.o execute.o sem.o shm.o
 NOM_PROJET = mini-shell
 
 #
@@ -100,5 +100,6 @@ 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
+shm.o: shm.c error.h shm.h constante.h
 mysh.o: mysh.c error.h str.h parser.h constante.h command.h execute.h \
- mysh.h sem.h
+ mysh.h

+ 126 - 0
shm.c

@@ -0,0 +1,126 @@
+/* 
+ * File:   shm.c
+ * Author: Arthur Brandao
+ *
+ * Created on 21 décembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h> /* Constante option */
+#include <errno.h>
+#include "error.h"
+#include "shm.h"
+
+/* --- Extern --- */
+extern int errno;
+
+/* --- Fonctions prviées --- */
+
+/**
+ * Indique si il reste des processus utilsant le segment
+ * @param shared_mem* La mémoire partagée
+ * @return Peut ont supprimer ou non
+ */
+boolean can_delete_shm(shared_mem* shm){
+    struct shmid_ds buf;
+    /* Recup les infos de la mémoire */
+    if (shmctl(shm->id, IPC_STAT, &buf) == ERR) {
+        addperror("Impossible de récupérer les données du segment");
+        return false;
+    }
+    /* Verifie qu'il n'y a plus d'utilisateur actif */
+    return buf.shm_nattch == 0;
+}
+
+/* --- Fonctions publiques --- */
+boolean create_shm(shared_mem* shm, int code, int size) {
+    int id;
+    void* adr;
+    /* Création de la shm */
+    key_t key = ftok(IPCKEYPATH, code);
+    if (key == ERR) {
+        addperror("Impossible de générer la clef");
+        return false;
+    }
+    id = shmget(key, size, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL);
+    if (id == ERR) {
+        /* Si erreur existe deja appel get_shm */
+        if (errno == EEXIST) {
+            adderror("Le segment existe deja, tentative de récupération");
+            return get_shm(shm, code, size);
+        } else {
+            addperror("Impossible de créer la mémoire partagée");
+            return false;
+        }
+    }
+    /* On attache */
+    adr = shmat(id, NULL, 0);
+    if (adr == (void*) ERR) {
+        addperror("Impossible d'attacher le segment");
+        return false;
+    }
+    /* Initialisation de la structure */
+    shm->id = id;
+    shm->key = key;
+    shm->size = size;
+    shm->adr = adr;
+    return true;
+}
+
+boolean get_shm(shared_mem* shm, int code, int size) {
+    int id;
+    void* adr;
+    /* Création de la shm */
+    key_t key = ftok(IPCKEYPATH, code);
+    if (key == ERR) {
+        addperror("Impossible de générer la clef");
+        return false;
+    }
+    id = shmget(key, size, S_IRUSR | S_IWUSR);
+    if (id == ERR) {
+        addperror("Impossible de récupérer la mémoire partagée");
+        return false;
+    }
+    /* On attache */
+    adr = shmat(id, NULL, 0);
+    if (adr == (void*) ERR) {
+        addperror("Impossible d'attacher le segment");
+        return false;
+    }
+    /* Initialisation de la structure */
+    shm->id = id;
+    shm->key = key;
+    shm->size = size;
+    shm->adr = adr;
+    return true;
+}
+
+boolean unset_shm(shared_mem* shm){
+	/* On détache la shm */
+	if(shmdt(shm->adr) == ERR){
+		perror("Erreur lors du détachement de la mémoire ");
+		return false;
+	}
+	shm->adr = NULL;
+        shm->size = 0;
+	/* Retour */
+	return true;
+}
+
+boolean delete_shm(shared_mem* shm) {
+    /* Regarde si on peut supprimer */
+    if(!can_delete_shm(shm)){
+        adderror("Des processus utilise encore le segment");
+        return false;
+    }
+    /* Supprime */
+    if (shmctl(shm->id, IPC_RMID, 0) == ERR) {
+        addperror("Impossible de supprimer le segment");
+        return false;
+    }
+    /* Reset la structure */
+    shm->id = 0;
+    shm->key = 0;
+    return true;
+}

+ 63 - 0
shm.h

@@ -0,0 +1,63 @@
+/* 
+ * File:   shm.h
+ * Author: Arthur Brandao
+ *
+ * Created on 21 décembre 2018
+ */
+
+#ifndef SHM_H
+#define SHM_H
+
+/* --- Include --- */
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include "constante.h"
+
+/* --- Structure --- */
+typedef struct{
+    //Id de shmget
+    int id;
+    //Clef d'accés à la shm
+    key_t key;
+    //Taille de la zone mémoire
+    int size;
+    //Pointeur vers la zone mémoire
+    void* adr;
+}shared_mem;
+
+/* --- Fonctions --- */
+/**
+ * Création d'un segment de mémoire partagée
+ * @param shared_mem* La mémoire partagée
+ * @param int La clef
+ * @param int La taille de la zone
+ * @return boolean Reussite
+ */
+boolean create_shm(shared_mem*, int, int);
+
+/**
+ * Création d'un segment de mémoire partagée
+ * @param shared_mem* La mémoire partagée
+ * @param int La clef
+ * @param int La taille de la zone
+ * @return boolean Reussite
+ */
+boolean get_shm(shared_mem*, int, int);
+
+/**
+ * Retire le segment de mémoire partagée
+ * @param shared_mem* La mémoire partagée
+ * @return boolean Reussite
+ */
+boolean unset_shm(shared_mem*);
+
+/**
+ * Supprime le segment de memoire partagée
+ * @param shared_mem* La mémoire partagée
+ * @return boolean Reussite
+ */
+boolean delete_shm(shared_mem*);
+
+#endif /* SHM_H */
+