This commit is contained in:
stormrider1982 2004-02-19 23:30:05 +00:00
parent 7c84387476
commit a10c94b3b9
3 changed files with 105 additions and 89 deletions

View file

@ -9,6 +9,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> /* shm_open */ #include <sys/mman.h> /* shm_open */
#include <errno.h> #include <errno.h>
#include <semaphore.h>
#define PAGESIZE sysconf(_SC_PAGESIZE) #define PAGESIZE sysconf(_SC_PAGESIZE)
#define MSGSPACE_DEFAULT_MODE 0600 #define MSGSPACE_DEFAULT_MODE 0600

View file

@ -25,7 +25,7 @@ int test(int argc,char **argv){
ma_liste=addBegining(ma_liste, 5); ma_liste=addBegining(ma_liste, 5);
for (listMove=ma_liste ;listMove!=NULL ; listMove=listMove->next) for (listMove=ma_liste ;listMove!=NULL ; listMove=listMove->next)
{ {
printf("valaeur :%d\n",listMove->ownerpid); printf("valaeur :%d\n",listMove->ownerPid);
} }
return(0); return(0);
} }

View file

@ -1,88 +1,103 @@
# include "libnazgul.h" # include "libnazgul.h"
/* pid[] /* pid[]
liste process demandeurs */ liste process demandeurs */
int msgPoolCreate( int msgPoolCreate(
msgSpacePoolId poolId, msgSpacePoolId poolId,
int buffNb, int buffNb,
int buffSize int buffSize
) { ) {
int poolFd; int poolFd;
static int poolNb; static int poolNb;
msgSpacePoolId id; msgSpacePoolId id;
if (msgSpacePoolId2nzgPoolId(id,poolId,poolNb) == -1){ if (msgSpacePoolId2nzgPoolId(id,poolId,poolNb) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n", fprintf( stderr, "msgPoolId creation failed for id %s\n",
(char*)poolId ); (char*)poolId );
return -1; return -1;
} }
poolFd=shm_open(id,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE); poolFd=shm_open(id,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE);
if (poolFd == -1 ) { if (poolFd == -1 ) {
fprintf( stderr, "msgPool creation failed: %s\n", fprintf( stderr, "msgPool creation failed: %s\n",
strerror( errno ) ); strerror( errno ) );
return -1; return -1;
} }
// on met le pool a la taille voulue pour qu'il // on met le pool a la taille voulue pour qu'il
// puisse contenir les buffs // puisse contenir les buffs
if (ftruncate(poolFd, (buffSize*buffNb)) == -1){ if (ftruncate(poolFd, (buffSize*buffNb)) == -1){
fprintf( stderr, "msgPool resizing failed: %s\n", fprintf( stderr, "msgPool resizing failed: %s\n",
strerror( errno ) ); strerror( errno ) );
return -1; return -1;
} }
close(poolFd); close(poolFd);
return 0; return 0;
} }
int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest,msgSpacePoolId src, int num){ int msgSpacePoolId2nzgPoolId(msgSpacePoolId dest,msgSpacePoolId src, int num){
if (strlen(src)>MSGSPACE_ID_LEN){ if (strlen(src)>MSGSPACE_ID_LEN){
return -1; return -1;
} }
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgSpacePool%s%d",(char *)src,num); sprintf(dest,"/tmp/nzgSpacePool%s%d",(char *)src,num);
#else #else
sprintf(dest,"/nzgSpacePool%s%d",(char *)src,num); sprintf(dest,"/nzgSpacePool%s%d",(char *)src,num);
#endif #endif
return 0; return 0;
} }
//TODO: msgPoolAllocate //TODO: msgPoolAllocate
// //donne l'adr en mappant in the memory // //donne l'adr en mappant in the memory
//poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0); //poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0);
msgPool * msgPoolAllocate(int poolIdx, msgSpace *msg) { msgPool * msgPoolAllocate(int poolIdx, msgSpace *msg) {
msgPool * poolAddr; msgPool * poolAddr;
int dataId; //msgSpacePoolDataId dataId;
msgPoolData * poolData;
// TODO:récupérer l'id du poolData dans le msgSpace int i, buffSize, poolId;
// détacher le morceau de mémoire du processus sem_t * sem;
// TODO:récupérer le poolData qui correspond à notre poolIdx. poolId = -1;
// attacher la zone mémoire correspondant à l'Id
// utiliser des sémaphores pour vérouiller les acces en modif // TODO:récupérer l'id du poolData dans le msgSpace
// sur la poolData de notre // détacher le morceau de mémoire du processus
//msgSpacePoolData spoolData;
//dataId = msg->poolDataId;
//dataId = msg->poolDataId;
// TODO:récupérer le poolData qui correspond à notre poolIdx.
//spoolData[dataId]; // attacher la zone mémoire correspondant à l'Id
// utiliser des sémaphores pour vérouiller les acces en modif
//donne l'adr en mappant in the memory // sur la poolData de notre
//GYR:poolAddr = mmap(NULL, sizeof(*msgPool), PROT_NONE, MAP_SHARED, poolFd, 0);
if(poolAddr == MAP_FAILED) { buffSize = poolData[poolIdx].bufferSize;
fprintf( stderr, "mmap failed: %s\n", if(poolData[poolIdx].allocOverload == 1) {
strerror( errno ) ); for(i=0; i<128; i++) {
return NULL; if( (poolData[i].allocOverload != 1)
} && (poolData[i].bufferSize >= buffSize) ) {
buffSize = poolData[i].bufferSize;
poolId = i;
// on renvoie le pointeur sur le bon pool }
return poolAddr; }
} }
//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;
}