From 179668953a385f0e51df9d6bf571de357957cc52 Mon Sep 17 00:00:00 2001 From: stormrider1982 Date: Thu, 19 Feb 2004 23:30:05 +0000 Subject: [PATCH] re --- src/nzg_global.h | 1 + src/nzg_msgList.c | 2 +- src/nzg_pool.c | 191 +++++++++++++++++++++++++--------------------- 3 files changed, 105 insertions(+), 89 deletions(-) diff --git a/src/nzg_global.h b/src/nzg_global.h index 7daaa20..ba58659 100644 --- a/src/nzg_global.h +++ b/src/nzg_global.h @@ -9,6 +9,7 @@ #include #include /* shm_open */ #include +#include #define PAGESIZE sysconf(_SC_PAGESIZE) #define MSGSPACE_DEFAULT_MODE 0600 diff --git a/src/nzg_msgList.c b/src/nzg_msgList.c index 17a8706..80f899a 100644 --- a/src/nzg_msgList.c +++ b/src/nzg_msgList.c @@ -25,7 +25,7 @@ int test(int argc,char **argv){ ma_liste=addBegining(ma_liste, 5); for (listMove=ma_liste ;listMove!=NULL ; listMove=listMove->next) { - printf("valaeur :%d\n",listMove->ownerpid); + printf("valaeur :%d\n",listMove->ownerPid); } return(0); } diff --git a/src/nzg_pool.c b/src/nzg_pool.c index 29aed38..114d9ec 100644 --- a/src/nzg_pool.c +++ b/src/nzg_pool.c @@ -1,88 +1,103 @@ -# include "libnazgul.h" - -/* pid[] - liste process demandeurs */ - -int msgPoolCreate( - msgSpacePoolId poolId, - int buffNb, - int buffSize - ) { - - int poolFd; - static int poolNb; - msgSpacePoolId id; - - if (msgSpacePoolId2nzgPoolId(id,poolId,poolNb) == -1){ - fprintf( stderr, "msgPoolId creation failed for id %s\n", - (char*)poolId ); - return -1; - } - - poolFd=shm_open(id,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE); - if (poolFd == -1 ) { - fprintf( stderr, "msgPool creation failed: %s\n", - strerror( errno ) ); - return -1; - } - - // on met le pool a la taille voulue pour qu'il - // puisse contenir les buffs - if (ftruncate(poolFd, (buffSize*buffNb)) == -1){ - fprintf( stderr, "msgPool resizing failed: %s\n", - strerror( errno ) ); - return -1; - } - - close(poolFd); - - return 0; -} - -int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest,msgSpacePoolId src, int num){ - if (strlen(src)>MSGSPACE_ID_LEN){ - return -1; - } -#ifdef _NZG_REALFILEID - sprintf(dest,"/tmp/nzgSpacePool%s%d",(char *)src,num); -#else - sprintf(dest,"/nzgSpacePool%s%d",(char *)src,num); -#endif - return 0; -} - -//TODO: msgPoolAllocate -// //donne l'adr en mappant in the memory - //poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); - -msgPool * msgPoolAllocate(int poolIdx, msgSpace *msg) { - msgPool * poolAddr; - int dataId; - - // TODO:récupérer l'id du poolData dans le msgSpace - // détacher le morceau de mémoire du processus - - // TODO:récupérer le poolData qui correspond à notre poolIdx. - // attacher la zone mémoire correspondant à l'Id - // utiliser des sémaphores pour vérouiller les acces en modif - // sur la poolData de notre - //msgSpacePoolData spoolData; - - //dataId = msg->poolDataId; - - //spoolData[dataId]; - - //donne l'adr en mappant in the memory - //GYR:poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); - if(poolAddr == MAP_FAILED) { - fprintf( stderr, "mmap failed: %s\n", - strerror( errno ) ); - return NULL; - } - - - // on renvoie le pointeur sur le bon pool - return poolAddr; -} - - +# include "libnazgul.h" + +/* pid[] + liste process demandeurs */ + +int msgPoolCreate( + msgSpacePoolId poolId, + int buffNb, + int buffSize + ) { + + int poolFd; + static int poolNb; + msgSpacePoolId id; + + if (msgSpacePoolId2nzgPoolId(id,poolId,poolNb) == -1){ + fprintf( stderr, "msgPoolId creation failed for id %s\n", + (char*)poolId ); + return -1; + } + + poolFd=shm_open(id,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE); + if (poolFd == -1 ) { + fprintf( stderr, "msgPool creation failed: %s\n", + strerror( errno ) ); + return -1; + } + + // on met le pool a la taille voulue pour qu'il + // puisse contenir les buffs + if (ftruncate(poolFd, (buffSize*buffNb)) == -1){ + fprintf( stderr, "msgPool resizing failed: %s\n", + strerror( errno ) ); + return -1; + } + + close(poolFd); + + return 0; +} + +int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest,msgSpacePoolId src, int num){ + if (strlen(src)>MSGSPACE_ID_LEN){ + return -1; + } +#ifdef _NZG_REALFILEID + sprintf(dest,"/tmp/nzgSpacePool%s%d",(char *)src,num); +#else + sprintf(dest,"/nzgSpacePool%s%d",(char *)src,num); +#endif + return 0; +} + +//TODO: msgPoolAllocate +// //donne l'adr en mappant in the memory + //poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); + +msgPool * msgPoolAllocate(int poolIdx, msgSpace *msg) { + msgPool * poolAddr; + //msgSpacePoolDataId dataId; + msgPoolData * poolData; + int i, buffSize, poolId; + sem_t * sem; + + poolId = -1; + + // TODO:récupérer l'id du poolData dans le msgSpace + // détacher le morceau de mémoire du processus + + //dataId = msg->poolDataId; + + // TODO:récupérer le poolData qui correspond à notre poolIdx. + // attacher la zone mémoire correspondant à l'Id + // utiliser des sémaphores pour vérouiller les acces en modif + // sur la poolData de notre + + buffSize = poolData[poolIdx].bufferSize; + if(poolData[poolIdx].allocOverload == 1) { + for(i=0; i<128; i++) { + if( (poolData[i].allocOverload != 1) + && (poolData[i].bufferSize >= buffSize) ) { + buffSize = poolData[i].bufferSize; + poolId = i; + } + } + } + + //donne l'adr en mappant in the memory + //GYR:poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); + if(poolAddr == MAP_FAILED) { + fprintf( stderr, "mmap failed: %s\n", strerror( errno ) ); + return NULL; + } + + // semaphores + sem = sem_open(poolData[(poolId != -1)?poolId:poolIdx].id, O_CREAT|O_EXCL, 0); + sem_wait(sem); + + // on renvoie le pointeur sur le bon pool + return poolAddr; +} + +