some-usable-scripts/C_Language/TP_SYS/tp_sema/consommateur.c

69 lines
1.4 KiB
C
Raw Normal View History

2011-03-17 16:37:36 +01:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2011-03-19 12:29:52 +01:00
#include <sys/types.h>
#include <sys/ipc.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/sem.h>
2011-03-20 01:03:54 +01:00
#include <sys/shm.h>
2011-03-19 12:29:52 +01:00
#include "consommateur.h"
#include "types.h"
2011-03-19 12:29:52 +01:00
#include "sema.h"
#include "constantes.h"
2011-03-17 16:37:36 +01:00
int main( int argc, char **argv)
{
2011-03-20 01:03:54 +01:00
int shmid, shm_key;
int mutex_data, mutex_tpa;
key_t sem_key_data;
key_t sem_key_tpa;
2011-03-19 12:29:52 +01:00
2011-03-20 01:03:54 +01:00
if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); }
2011-03-19 12:29:52 +01:00
2011-03-20 01:03:54 +01:00
shm_key = (key_t) atoi(argv[1]);
2011-03-20 11:13:18 +01:00
sem_key_tpa = MUTEX_TPA;
sem_key_data = MUTEX_DATA;
2011-03-19 12:29:52 +01:00
2011-03-20 01:03:54 +01:00
MEMP * memoireP;
2011-03-17 17:31:26 +01:00
2011-03-20 01:03:54 +01:00
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); }
2011-03-20 01:21:44 +01:00
if((mutex_data = creat_sem( sem_key_data, 1)) == -1)
2011-03-20 01:03:54 +01:00
{ perror("creat_sem"); exit(EXIT_FAILURE); }
2011-03-20 01:21:44 +01:00
if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1)
2011-03-20 01:03:54 +01:00
{ perror("creat_sem"); exit(EXIT_FAILURE); }
P(mutex_data);
memoireP->tete = 0;
memoireP->queue = 0;
V(mutex_data);
2011-03-20 01:21:44 +01:00
P(mutex_tpa);
2011-03-20 01:03:54 +01:00
for(int i = 0; i < MAX_PROD ; i++)
memoireP->tpa[i] = -1;
2011-03-20 01:21:44 +01:00
V(mutex_tpa);
2011-03-20 01:03:54 +01:00
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); }
2011-03-17 17:31:26 +01:00
2011-03-17 16:37:36 +01:00
exit(EXIT_SUCCESS);
}