From 8120c1e35152d9c13986bdd13ef9b0434a3ed275 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 20 Mar 2011 01:21:44 +0100 Subject: [PATCH] Changements mineurs --- C_Language/TP_SYS/tp_sema/consommateur.c | 9 ++++----- C_Language/TP_SYS/tp_sema/producteur.c | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/C_Language/TP_SYS/tp_sema/consommateur.c b/C_Language/TP_SYS/tp_sema/consommateur.c index f79336d..c2b3c42 100644 --- a/C_Language/TP_SYS/tp_sema/consommateur.c +++ b/C_Language/TP_SYS/tp_sema/consommateur.c @@ -22,7 +22,6 @@ int main( int argc, char **argv) int mutex_data, mutex_tpa; key_t sem_key_data; key_t sem_key_tpa; - const char CTRL_D = 4; if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); } @@ -38,11 +37,10 @@ int main( int argc, char **argv) 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) + if((mutex_data = creat_sem( sem_key_data, 1)) == -1) { perror("creat_sem"); exit(EXIT_FAILURE); } - if((mutex_tpa = creat_sem( sem_key_tpa, MAX_PROD)) == -1) + if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1) { perror("creat_sem"); exit(EXIT_FAILURE); } P(mutex_data); @@ -51,9 +49,10 @@ int main( int argc, char **argv) memoireP->queue = 0; V(mutex_data); + P(mutex_tpa); for(int i = 0; i < MAX_PROD ; i++) memoireP->tpa[i] = -1; - + V(mutex_tpa); sleep(10); diff --git a/C_Language/TP_SYS/tp_sema/producteur.c b/C_Language/TP_SYS/tp_sema/producteur.c index ccfeac6..df9a3a0 100644 --- a/C_Language/TP_SYS/tp_sema/producteur.c +++ b/C_Language/TP_SYS/tp_sema/producteur.c @@ -12,17 +12,29 @@ int main( int argc, char **argv) { if(argc < 2) { printf("Usage %s numIPC\n", argv[0]); exit(EXIT_FAILURE); } MEMP *memoireP; + int mutex_data, mutex_tpa; + key_t sem_key_data; + key_t sem_key_tpa; 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 memoireP partagée */ - shmid = shmget(shm_key, 100, 0700 | IPC_CREAT); + shmid = shmget(shm_key, sizeof(MEMP), 0766 | IPC_CREAT); if (shmid == -1) { perror("shmget"); return (EXIT_FAILURE); } -/* montage en mémoire */ - memoireP = shmat(shmid, NULL, 0); + if((memoireP = (MEMP *) shmat(shmid, 0 , 0766)) == (void *) -1) + { perror("shmat"); exit(EXIT_FAILURE); } + + if((mutex_data = creat_sem( sem_key_data, 1)) == -1) + { perror("creat_sem"); exit(EXIT_FAILURE); } + if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1) + { perror("creat_sem"); exit(EXIT_FAILURE); } + + P(mutex_data); + + V(mutex_data); + /* utilisation */ // printf("memoireP[0] = %d\n", memoireP[0]++ );