#include #include "ipcTools.h" #include #include int semalloc(key_t key, int valInit) { int semid = semget(key,1,0); if (semid == -1){ semid = semget(key,1,IPC_CREAT|IPC_EXCL|0644); if (semid == -1) return -1; if (semctl(semid,0,SETVAL,valInit) == -1) { /* il faut detruire le semaphore */ semctl(semid,0,IPC_RMID,0); return -1; } } return semid; } static struct sembuf sP = {0,-1,0}; static struct sembuf sV = {0, 1,0}; void P(int semid){ semop(semid,&sP,1); } void V(int semid) { semop(semid,&sV,1); } int semfree(int semid) { return semctl(semid,0,IPC_RMID); } void* shmalloc(key_t cle, int taille){ void* res; int id = shmget(cle,1, 0600);/*si zone existe deja :*/ if(id == -1){ /*sinon creation*/ id = shmget(cle, taille, IPC_CREAT|IPC_EXCL|0644); } if(id==-1) return 0; res = shmat(id,0,0); /*attachement a la mem loc*/ if(res==(void*)-1){ /*si echec liberation*/ shmctl(id,IPC_RMID, 0); return 0; } return res; } // 0 = echec int shmfree(key_t cle){ int id = shmget(cle, 1, 0); return shmctl(id, IPC_RMID,0); } // -1 impossible (ou deja recup) 0 si ok