some-usable-scripts/c/semaphores_manuel_exemples/Exemples_semaphores/ecrireZ.c

83 lines
1.5 KiB
C
Raw Normal View History

2011-03-10 14:08:58 +01:00
/* Mecanismes de communication entre processus - IPC */
/* Ecriture dans une zone de m<>moire partaj<61>e */
#include <stdlib.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#define SHMEM_SIZE 0xff
#define SHMEM_RIGHTS 0x01ff
int main (int argc, char ** argv) {
long i , k , temp;
long * ptz ;
int p1 , p2, p3, p4;
int ipc_key, ipc_result ;
if (argc<2)
exit(1) ;
ipc_key = atoi(argv[1]) ;
srand ( time(0) ); // initialisation g<>n<EFBFBD>rateur
ipc_result = shmget(ipc_key, 0 , 0) ; // ouverture m<>moire partag<61>e
ptz = (long *) shmat(ipc_result, 0, SHMEM_RIGHTS) ; // attachement
if ((p1 = fork()) == 0) {
for(i=0;i<10;i++) {
temp = * ptz;
temp++;
sleep( rand() % 2 ); // attente al<61>atoire
*ptz = temp;
}
exit(0) ;
}
// while(wait(0) != p1);
if ((p2 = fork()) == 0) {
for(i=0;i<10;i++) {
temp = * ptz;
temp++;
sleep( rand() % 2 ); // attente al<61>atoire
*ptz = temp;
}
exit(0) ;
}
// while(wait(0) != p2);
if ((p3 = fork()) == 0) {
for(i=0;i<10;i++) {
temp = * ptz;
temp++;
sleep( rand() % 2 ); // attente al<61>atoire
*ptz = temp;
}
exit(0) ;
}
// while(wait(0) != p3);
if ((p4 = fork()) == 0) {
for(i=0;i<10;i++) {
temp = * ptz;
temp++;
sleep( rand() % 2 ); // attente al<61>atoire
*ptz = temp;
}
exit(0) ;
}
printf("\n Arret processus pid = %d compteur = ( %d )\n\n", getpid(), *ptz) ;
exit(0) ;
}