diff --git a/C_Language/TP_SYS/Exemples_semaphores/a.out b/C_Language/TP_SYS/Exemples_semaphores/a.out index feedb49..034006d 100755 Binary files a/C_Language/TP_SYS/Exemples_semaphores/a.out and b/C_Language/TP_SYS/Exemples_semaphores/a.out differ diff --git a/C_Language/TP_SYS/Exemples_semaphores/creaZ b/C_Language/TP_SYS/Exemples_semaphores/creaZ old mode 100644 new mode 100755 diff --git a/C_Language/TP_SYS/Exemples_semaphores/ecrireZ b/C_Language/TP_SYS/Exemples_semaphores/ecrireZ deleted file mode 100644 index ea8e2f4..0000000 Binary files a/C_Language/TP_SYS/Exemples_semaphores/ecrireZ and /dev/null differ diff --git a/C_Language/TP_SYS/tp_sema/Makefile b/C_Language/TP_SYS/tp_sema/Makefile index 81b662e..bca8952 100644 --- a/C_Language/TP_SYS/tp_sema/Makefile +++ b/C_Language/TP_SYS/tp_sema/Makefile @@ -1,10 +1,10 @@ CC = gcc CONS = consommateur PROD = producteur -CFLAGS = -Wall -g +CFLAGS = -Wall -g COMMUN = sema.o global.o all: $(COMMUN) consommateur.o producteur.o - $(CC) consommateur.o -o $(CONS) sema.o + $(CC) consommateur.o -o $(CONS) -std=c99 sema.o $(CC) producteur.o -o $(PROD) sema.o sema.o : sema.c sema.h diff --git a/C_Language/TP_SYS/tp_sema/consommateur.c b/C_Language/TP_SYS/tp_sema/consommateur.c index b60c493..f79336d 100644 --- a/C_Language/TP_SYS/tp_sema/consommateur.c +++ b/C_Language/TP_SYS/tp_sema/consommateur.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "consommateur.h" #include "types.h" #include "sema.h" @@ -17,22 +18,51 @@ int main( int argc, char **argv) { - if(argc < 2) { printf("Usage : %s numIPC\n", argv[0]); exit(EXIT_FAILURE); } + int shmid, shm_key; + int mutex_data, mutex_tpa; + key_t sem_key_data; + key_t sem_key_tpa; + const char CTRL_D = 4; - key_t clef = (key_t) atoi(argv[1]); + if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); } - MEMP memoireP; - memoireP.tete = 0; - memoireP.queue = 0; + shm_key = (key_t) atoi(argv[1]); + sem_key_tpa = MUTEX_1; + sem_key_data = MUTEX_2; - int semid; - semid = creat_sem( clef, MAX_PROD); - if(semid >= 0) - { + MEMP * memoireP; - sleep(10); - del_sem(clef); - } + if((shmid = shmget(shm_key, sizeof(MEMP), IPC_CREAT|IPC_EXCL|0766)) == -1) + { perror("shmget"); exit(EXIT_FAILURE);} + + if((memoireP = (MEMP *) shmat(shmid, 0 , 0766)) == (void *) -1) + { perror("shmat"); exit(EXIT_FAILURE); } + + // MUTEX_2 => DATA + if((mutex_data = creat_sem( sem_key_data, MAX_PROD)) == -1) + { perror("creat_sem"); exit(EXIT_FAILURE); } + + if((mutex_tpa = creat_sem( sem_key_tpa, MAX_PROD)) == -1) + { perror("creat_sem"); exit(EXIT_FAILURE); } + + P(mutex_data); + + memoireP->tete = 0; + memoireP->queue = 0; + V(mutex_data); + + for(int i = 0; i < MAX_PROD ; i++) + memoireP->tpa[i] = -1; + + + + sleep(10); + + if(shmctl(shmid, IPC_RMID, 0) < 0) + { perror("shmctl"); exit(EXIT_FAILURE); } + + if(mutex_data >= 0) { del_sem(sem_key_data); } + if(mutex_tpa >= 0) { del_sem(sem_key_tpa); } exit(EXIT_SUCCESS); } diff --git a/C_Language/TP_SYS/tp_sema/producteur.c b/C_Language/TP_SYS/tp_sema/producteur.c index 9fcabaf..ccfeac6 100644 --- a/C_Language/TP_SYS/tp_sema/producteur.c +++ b/C_Language/TP_SYS/tp_sema/producteur.c @@ -11,20 +11,20 @@ int main( int argc, char **argv) { if(argc < 2) { printf("Usage %s numIPC\n", argv[0]); exit(EXIT_FAILURE); } - int *zone; - int memid; - int clef = atoi(argv[1]); - if(clef == -1) { printf("Usage %s numIPC message\n", argv[0]); exit(EXIT_FAILURE); } + MEMP *memoireP; + int shmid; + int shm_key = atoi(argv[1]); + if(shm_key == -1) { printf("Usage %s numIPC message\n", argv[0]); exit(EXIT_FAILURE); } -/* création ou lien avec une zone partagée */ - memid = shmget(clef, 100, 0700 | IPC_CREAT); - if (memid == -1) { perror("shmget"); return (EXIT_FAILURE); } +/* création ou lien avec une memoireP partagée */ + shmid = shmget(shm_key, 100, 0700 | IPC_CREAT); + if (shmid == -1) { perror("shmget"); return (EXIT_FAILURE); } /* montage en mémoire */ - zone = shmat(memid, NULL, 0); + memoireP = shmat(shmid, NULL, 0); /* utilisation */ - printf("zone[0] = %d\n", zone[0]++ ); +// printf("memoireP[0] = %d\n", memoireP[0]++ ); return (EXIT_SUCCESS);