Avancement certain

master
Philippe Pittoli 2011-03-20 01:03:54 +01:00
parent 13119a2d53
commit 07778c1db7
6 changed files with 53 additions and 23 deletions

0
C_Language/TP_SYS/Exemples_semaphores/creaZ Normal file → Executable file
View File

View File

@ -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

View File

@ -9,6 +9,7 @@
#include <stdarg.h>
#include <errno.h>
#include <sys/sem.h>
#include <sys/shm.h>
#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);
}

View File

@ -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);