* renommages

This commit is contained in:
glenux 2004-02-22 11:50:29 +00:00
parent b6f07d0e1e
commit 634e65599b
7 changed files with 88 additions and 82 deletions

View file

@ -29,11 +29,14 @@ void * msgAllocate(msgSpace *space,
int * ressourceSemVal=NULL; int * ressourceSemVal=NULL;
float minPoolCoef; float minPoolCoef;
int selectedPoolIndex; int selectedPoolIndex;
int bufferFreeIndex;
selectedPoolIndex=-1; selectedPoolIndex=-1;
/* TODO: verifier le premier arg du shm_open */ /* TODO: verifier le premier arg du shm_open */
mSPoolDataTabFd=shm_open(space->poolDataTabId,O_RDWR,MSGSPACE_DEFAULT_MODE); mSPoolDataTabFd=shm_open(space->poolDataTabId,
O_RDWR,
MSGSPACE_DEFAULT_MODE);
if (mSPoolDataTabFd == -1 ) { if (mSPoolDataTabFd == -1 ) {
fprintf( stderr, "Allocate %s failed: %s\n", fprintf( stderr, "Allocate %s failed: %s\n",
(char*)space->poolDataTabId, (char*)space->poolDataTabId,
@ -41,13 +44,13 @@ void * msgAllocate(msgSpace *space,
return NULL; return NULL;
} }
msgPoolData * mSPoolDataTab; msgPoolData * mSPoolDataTabAddr;
mSPoolDataTab = mmap( 0, (space->poolNb) * sizeof( msgPoolData ), mSPoolDataTabAddr = mmap( 0, (space->poolNb) * sizeof( msgPoolData ),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, mSPoolDataTabFd, 0 ); MAP_SHARED, mSPoolDataTabFd, 0 );
if( mSPoolDataTab == MAP_FAILED) { if( mSPoolDataTabAddr == MAP_FAILED) {
fprintf( stderr, "mmap failed: %s\n", fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) ); strerror( errno ) );
return NULL; return NULL;
@ -64,26 +67,24 @@ void * msgAllocate(msgSpace *space,
if ( pool == ANYPOOL){ if ( pool == ANYPOOL){
// choisir le pool au hasard (ou presque) // choisir le pool au hasard (ou presque)
for(i=0; i<(space->poolNb); i++) { for(i=0; i<(space->poolNb); i++) {
if(mSPoolDataTab[i].bufferSize >= taille) { if(mSPoolDataTabAddr[i].bufferSize >= taille) {
/* choisir le numero du semaphore /* choisir le numero du semaphore
en fonction du nombre de lock poses / nombre de buffer */ en fonction du nombre de lock poses / nombre de buffer */
msgPoolSemIdIntern(ressourceSemId,space->id,i); msgPoolSemIdIntern(ressourceSemId,space->id,i);
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
if (ressourceSemFd == SEM_FAILED){ if (ressourceSemFd == SEM_FAILED){
#warning "Gérer mieu les erreurs..."
perror("sem_open"); return NULL; perror("sem_open"); return NULL;
} }
/* on remplit le tableau avec les valeurs des semaphores */ /* on remplit le tableau avec les valeurs des semaphores */
if (sem_getvalue(ressourceSemFd, ressourceSemVal) < 0){ if (sem_getvalue(ressourceSemFd, ressourceSemVal) < 0){
#warning "Gérer mieu les erreurs..."
perror("sem_getvalue"); return NULL; perror("sem_getvalue"); return NULL;
} }
if ((*ressourceSemVal) < 0){ if ((*ressourceSemVal) < 0){
/* il y a ressourceSemVal processus qui attendent déja... */ /* il y a ressourceSemVal processus qui attendent déja... */
semPoolCoef[nbLockedSem] = semPoolCoef[nbLockedSem] =
(float) (- (*ressourceSemVal) / mSPoolDataTab[i].bufferNb); (float) (- (*ressourceSemVal) / mSPoolDataTabAddr[i].bufferNb);
nbLockedSem++; nbLockedSem++;
} }
if(sem_trywait(ressourceSemFd)) { if(sem_trywait(ressourceSemFd)) {
@ -94,7 +95,6 @@ void * msgAllocate(msgSpace *space,
break; break;
} }
if( sem_close(ressourceSemFd) <0){ if( sem_close(ressourceSemFd) <0){
#warning "Gérer mieu les erreurs..."
perror("sem_getvalue"); return NULL; perror("sem_getvalue"); return NULL;
} }
} // if buffSize > taille } // if buffSize > taille
@ -125,7 +125,7 @@ void * msgAllocate(msgSpace *space,
} }
if (!gotRessourceSem){ if (!gotRessourceSem){
strcpy(resultPoolId,mSPoolDataTab[selectedPoolIndex].id); strcpy(resultPoolId,mSPoolDataTabAddr[selectedPoolIndex].id);
msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex); msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex);
ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
if(ressourceSemFd==SEM_FAILED){ if(ressourceSemFd==SEM_FAILED){
@ -146,8 +146,13 @@ void * msgAllocate(msgSpace *space,
/* on modifie maintenant les données */ /* on modifie maintenant les données */
/* - on récupere l'index du premier buffer libre */ /* - on récupere l'index du premier buffer libre */
//TODO:int bufferFreeIndex = msgBufferGetFreeIndex(space,selectedPoolIndex); bufferFreeIndex = msgBufferGetFreeIndex(mSPoolDataTabAddr,selectedPoolIndex);
if (bufferFreeIndex < 0){
sem_close(poolDataTabSemFd);
// aucun buffer libre ?
return NULL;
}
printf("Buffer %d libre a attacher !\n",bufferFreeIndex);
/*TODO: mapper le buffer libre dans l'esp addr du proc */ /*TODO: mapper le buffer libre dans l'esp addr du proc */
//TODO: //TODO:
/* - on s'enregistre aupres de ce buffer */ /* - on s'enregistre aupres de ce buffer */

View file

@ -1,10 +1,4 @@
#include "libnazgul.h" /*
#include "nzg_ids.h" int msgBufferDetachProc(msgSpace * space, int poolIndex){
}
int msgBufferGetFreeIndex(msgSpaceId spaceId,int poolIndex){ */
return -1;
}
int msgBufferAttachProc(msgSpaceId spaceId,int poolIndex,void * addr){
return -1;
}

View file

@ -5,7 +5,6 @@ int msgBufferInit(msgPoolData * poolDataTabAddr,int poolIndex){
int bufferInfoTabFd; int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr; msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb; int bufferInfoNb;
int bufferFreeIndex;
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
@ -21,16 +20,14 @@ int bufferFreeIndex;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0); PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0);
int i=0; int i;
while ((i<bufferInfoNb) for (i=0;i<bufferInfoNb;i++){
&& (bufferInfoTabAddr[i].ownerPid != (pid_t)-1)){ i++; } bufferInfoTabAddr[i].ownerPid = (pid_t)-1;
if (i == bufferInfoNb){ return -1; } }
bufferFreeIndex=i;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0) if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ perror("munmap"); return -1; } { perror("munmap"); return -1; }
return 0;
return -1;
} }

View file

@ -14,7 +14,7 @@ msgSpace *msgSpaceOpen(msgSpaceId spaceId);
int msgSpaceDelete(msgSpaceId spaceId); int msgSpaceDelete(msgSpaceId spaceId);
/* src/nzg_buffer.c */ /* src/nzg_buffer.c */
int msgBufferGetFreeIndex(msgPoolData * poolDataTab, int poolIndex); int msgBufferGetFreeIndex(msgPoolData * poolDataTab, int poolIndex);
int msgBufferAttachProc(msgPoolData * poolDataTab, int poolIndex, void *addr); int msgBufferAttachProc(msgPoolData * poolDataTab, int poolIndex, int bufferIndex,void *addr);
#endif #endif

View file

@ -1,10 +1,7 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "nzg_ids.h" #include "nzg_ids.h"
/* prototypes des f#define MSGSPACE_DEFAULT_MODE 0600 /* prototypes des fonctions annexes à ne pas exporter */
#define MSGSPACE_ID_LEN 32
onctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src );
@ -146,51 +143,4 @@ msgSpace * msgSpaceCreate(
} }
/*
* spaceId : blabla
*/
msgSpace * msgSpaceOpen(msgSpaceId spaceId){
int shmFd;
msgSpaceId nzgId;
msgSpace * mSAddr;
if (msgSpaceIdIntern(nzgId,spaceId) < 0){
return NULL;
}
if ((shmFd=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){
perror("shm_open");
return NULL;
}
mSAddr=mmap(NULL,sizeof(msgSpace),PROT_READ|PROT_WRITE,MAP_SHARED,shmFd,(off_t)0);
return mSAddr;
}
int msgSpaceDelete(msgSpaceId spaceId){
fprintf(stderr,"Deleting msgSpace with id : %s\n",spaceId);
//int shmId;
msgSpaceId nzgId;
msgSpace * space;
if (msgSpaceIdIntern(nzgId,spaceId) == -1){
//TODO: message d'erreur
return -1;
}
space = msgSpaceOpen(spaceId);
/* TODO: supprimer chaque pool */
printf("openned successfully !\n");
printf("Unlinking DataTab... ");
if (shm_unlink(space->poolDataTabId) < 0){
perror("shm_unlink");
return -1;
}
printf("ok\n");
if (shm_unlink(nzgId)<0){
perror("shm_unlink");
return -1;
};
return 0;
}

35
src/nzg_spaceDelete.c Normal file
View file

@ -0,0 +1,35 @@
#include "libnazgul.h"
#include "nzg_ids.h"
/* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgSpaceDelete(msgSpaceId spaceId){
fprintf(stderr,"Deleting msgSpace with id : %s\n",spaceId);
//int shmId;
msgSpaceId nzgId;
msgSpace * space;
if (msgSpaceIdIntern(nzgId,spaceId) == -1){
//TODO: message d'erreur
return -1;
}
space = msgSpaceOpen(spaceId);
/* TODO: supprimer chaque pool */
printf("openned successfully !\n");
printf("Unlinking DataTab... ");
if (shm_unlink(space->poolDataTabId) < 0){
perror("shm_unlink");
return -1;
}
printf("ok\n");
if (shm_unlink(nzgId)<0){
perror("shm_unlink");
return -1;
};
return 0;
}

25
src/nzg_spaceOpen.c Normal file
View file

@ -0,0 +1,25 @@
#include "libnazgul.h"
#include "nzg_ids.h"
/* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src );
msgSpace * msgSpaceOpen(msgSpaceId spaceId){
int shmFd;
msgSpaceId nzgId;
msgSpace * mSAddr;
if (msgSpaceIdIntern(nzgId,spaceId) < 0){
return NULL;
}
if ((shmFd=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){
perror("shm_open");
return NULL;
}
mSAddr=mmap(NULL,sizeof(msgSpace),PROT_READ|PROT_WRITE,MAP_SHARED,shmFd,(off_t)0);
return mSAddr;
}