diff --git a/src/nzg_allocate.c b/src/nzg_allocate.c index 9becc5a..c3b66af 100644 --- a/src/nzg_allocate.c +++ b/src/nzg_allocate.c @@ -29,7 +29,7 @@ void * msgAllocate(msgSpace *space, float minPoolCoef; int selectedPoolIndex; int bufferFreeIndex; - + selectedPoolIndex=-1; /* TODO: verifier le premier arg du shm_open */ @@ -55,16 +55,16 @@ void * msgAllocate(msgSpace *space, return NULL; } - gotRessourceSem=false; + gotRessourceSem=false; /* initialisation des coefs */ for (i=0;i<(space->poolNb);i++){ semPoolCoef[i]=-1; } - + int nbLockedSem=0; if ( pool == ANYPOOL){ - fprintf(stderr,">>>> ANYPOOL selection <<<<\n"); + fprintf(stderr,"[ ALLOCATION ANYPOOL : %d ]\n",(int)getpid()); // choisir le pool au hasard (ou presque) for(i=0; i<(space->poolNb); i++) { printf("ERRORDETECT boucle %d\n",i); fflush(stdout); @@ -79,40 +79,44 @@ void * msgAllocate(msgSpace *space, return NULL; } /* on remplit le tableau avec les valeurs des semaphores */ - - if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){ - NZG_ERROR("sem_getvalue",ressourceSemId); - return NULL; - } - if ((ressourceSemVal) < 0){ + if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){ + NZG_ERROR("sem_getvalue",ressourceSemId); + return NULL; + } + printf("RESSOURCESEMVAL %d\n",ressourceSemVal); + if (ressourceSemVal <= 0){ + printf("resVal < 0 : %d\n",ressourceSemVal); /* il y a ressourceSemVal processus qui attendent déja... */ semPoolCoef[nbLockedSem] = (float) (- (ressourceSemVal) / mSPoolDataTabAddr[i].bufferNb); nbLockedSem++; } if(sem_trywait(ressourceSemFd)==0) { + printf("got try_wait\n"); /* choisir la 1ere pool de taille plus grande * libre si possible */ gotRessourceSem=true; selectedPoolIndex=i; break; } - if( sem_close(ressourceSemFd) <0){ - NZG_ERROR("sem_getvalue",ressourceSemId); - return NULL; - } + if( sem_close(ressourceSemFd) <0){ + NZG_ERROR("sem_getvalue",ressourceSemId); + return NULL; + } } // if buffSize > taille } // for - printf("ERRORDETECT\n"); fflush(stdout); + printf("ERRORDETECT outFor\n"); fflush(stdout); if (!gotRessourceSem) { + printf("Calcul du meilleur en cas de liberation\n"); minPoolCoef= semPoolCoef[0]; idxPoolOptimum = 0; /* on cherche le pool avec le moins de lock poses / nbre de buffer * le numéro du pool est stocké dans idxPoolOptimum */ for(i=0; i%s<=\n",poolDataTabAddr[poolIndex].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); diff --git a/src/nzg_free.c b/src/nzg_free.c index 85286e2..b99a2f6 100644 --- a/src/nzg_free.c +++ b/src/nzg_free.c @@ -1,4 +1,6 @@ #include "libnazgul.h" -int msgFree(msgSpace * space, void * addr); +int msgFree(msgSpace * space, void * addr){ + return -1; +} diff --git a/src/nzg_poolCreate.c b/src/nzg_poolCreate.c index 5670ce9..b3ac79a 100644 --- a/src/nzg_poolCreate.c +++ b/src/nzg_poolCreate.c @@ -9,7 +9,7 @@ int msgPoolCreate( ) { int poolFd; - sem_t * ressourceSem; + sem_t * ressourceSemFd; msgPoolId poolId; msgPoolSemId poolRessourceSemId; @@ -38,9 +38,17 @@ int msgPoolCreate( } // on met un semaphore sur le pool - ressourceSem = sem_open(poolRessourceSemId, O_CREAT|O_EXCL, 0666, buffNb); + ressourceSemFd = sem_open(poolRessourceSemId, O_CREAT|O_EXCL, 0666, buffNb); + if (ressourceSemFd == SEM_FAILED){ + NZG_ERROR("sem_open : creation de la ressource",poolRessourceSemId); + return -1; + } else { + NZG_ERROR("sem_open : creation oki",poolRessourceSemId); + } + //TODO: verrifier les erreurs sur l'ouverture de la sem + sem_close(ressourceSemFd); // on met le pool a la taille voulue pour qu'il // puisse contenir les buffs diff --git a/src/nzg_proto.h b/src/nzg_proto.h index 4cade55..4c4d241 100644 --- a/src/nzg_proto.h +++ b/src/nzg_proto.h @@ -12,9 +12,11 @@ int msgBufferDetachProc(msgPoolData *poolDataTabAddr, int poolIndex, int bufferI /* nzg_bufferGetFreeIndex.c */ int msgBufferGetFreeIndex(msgPoolData *poolDataTabAddr, int poolIndex); /* nzg_bufferInfoTabCreate.c */ -int msgBufferInfoTabCreate(msgSpaceId externId,msgPoolData * poolDataTabAddr, int poolIdx, int buffNb); +int msgBufferInfoTabCreate(msgSpaceId externId, msgPoolData *poolDataTabAddr, int poolIdx, int bufferNb); /* nzg_bufferInfoTabInit.c */ int msgBufferInfoTabInit(msgPoolData *poolDataTabAddr, int poolIndex); +/* nzg_free.c */ +int msgFree(msgSpace *space, void *addr); /* nzg_ids.c */ int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src); int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId, int poolIdx); @@ -23,7 +25,7 @@ int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num); int msgBufferInfoTabIdIntern(msgBufferInfoTabId dest, msgSpaceId src, int num); /* nzg_list.c */ /* nzg_msgList.c */ -msgSpaceListElem *addBegining(msgSpaceListElem *list, int ownerpid); +void *add(char *idAncien, char *idNouveau); /* nzg_poolCreate.c */ int msgPoolCreate(msgSpaceId externId, int poolIdx, int buffNb, int buffSize); /* nzg_poolDelete.c */ @@ -35,6 +37,5 @@ int msgSpaceDelete(msgSpaceId externId); /* nzg_spaceOpen.c */ msgSpace *msgSpaceOpen(msgSpaceId spaceId); /* nzg_state.c */ -int msgFree(msgSpace * space, void * addr); #endif diff --git a/src/nzg_spaceCreate.c b/src/nzg_spaceCreate.c index f410e7e..215843b 100644 --- a/src/nzg_spaceCreate.c +++ b/src/nzg_spaceCreate.c @@ -69,7 +69,7 @@ msgSpace * msgSpaceCreate( return NULL; } - printf( "msgSpace map addr is 0x%08x : %d\n", (int)mSAddr,mSFd); + printf( "CREAT: msgSpace mapped to 0x%08x in %d\n", (int)mSAddr,(int)getpid()); /* on ferme le descripteur du fichier */ close(mSFd); diff --git a/src/nzg_spaceOpen.c b/src/nzg_spaceOpen.c index c29bb56..ec21eeb 100644 --- a/src/nzg_spaceOpen.c +++ b/src/nzg_spaceOpen.c @@ -6,11 +6,11 @@ int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); -msgSpace * msgSpaceOpen(msgSpaceId spaceId){ +msgSpace * msgSpaceOpen(msgSpaceId externId){ int shmFd; msgSpaceId nzgId; msgSpace * mSAddr; - if (msgSpaceIdIntern(nzgId,spaceId) < 0){ + if (msgSpaceIdIntern(nzgId,externId) < 0){ return NULL; } if ((shmFd=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){ @@ -19,6 +19,7 @@ msgSpace * msgSpaceOpen(msgSpaceId spaceId){ } mSAddr=mmap(NULL,sizeof(msgSpace),PROT_READ|PROT_WRITE,MAP_SHARED,shmFd,(off_t)0); + printf( "OPEN: msgSpace mapped to 0x%08x in %d\n", (int)mSAddr,(int)getpid()); return mSAddr; }