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

44 lines
1.0 KiB
C
Raw Normal View History

2011-03-17 16:37:36 +01:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "constantes.h"
#include "types.h"
2011-03-17 16:37:36 +01:00
#include "sema.h"
int main( int argc, char **argv)
{
2011-03-17 17:31:26 +01:00
if(argc < 2) { printf("Usage %s numIPC\n", argv[0]); exit(EXIT_FAILURE); }
2011-03-20 01:03:54 +01:00
MEMP *memoireP;
2011-03-20 01:21:44 +01:00
int mutex_data, mutex_tpa;
key_t sem_key_data;
key_t sem_key_tpa;
2011-03-20 01:03:54 +01:00
int shmid;
int shm_key = atoi(argv[1]);
2011-03-17 16:37:36 +01:00
2011-03-20 01:03:54 +01:00
/* création ou lien avec une memoireP partagée */
2011-03-20 01:21:44 +01:00
shmid = shmget(shm_key, sizeof(MEMP), 0766 | IPC_CREAT);
2011-03-20 01:03:54 +01:00
if (shmid == -1) { perror("shmget"); return (EXIT_FAILURE); }
2011-03-17 16:37:36 +01:00
2011-03-20 01:21:44 +01:00
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); }
2011-03-17 16:37:36 +01:00
2011-03-20 01:21:44 +01:00
if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1)
{ perror("creat_sem"); exit(EXIT_FAILURE); }
P(mutex_data);
V(mutex_data);
2011-03-17 16:37:36 +01:00
/* utilisation */
2011-03-20 01:03:54 +01:00
// printf("memoireP[0] = %d\n", memoireP[0]++ );
2011-03-17 16:37:36 +01:00
return (EXIT_SUCCESS);
}