Continue nzg buffer implementation

This commit is contained in:
Glenn Y. Rolland 2019-09-17 14:45:48 +02:00
parent 62808caa29
commit 160848d1f9
5 changed files with 123 additions and 76 deletions

View file

@ -1,4 +1,15 @@
/* #include "libnazgul.h"
int msgBufferDetachProc(msgSpace * space, int poolIndex){
int msgBufferGetAttachedProcIndex(
msgPoolData * poolDataTabAddr,
int poolIndex,
void * addr
){
//TODO: parcourrir tous les index, et regarder s'il y
//a une addresse qui correspond avec le meme processus...
//et renvoyer l'index du buffer correspondant a l'addresse...
return 0;
} }
*/

View file

@ -0,0 +1,37 @@
#include "libnazgul.h"
int msgBufferAttachProc(
msgPoolData * poolDataTabAddr,
int poolIndex,
int bufferIndex,
void * addr)
{
msgBufferInfoTabId bufferInfoTabId;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
//récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE);
if (bufferInfoTabFd<0){
perror("shm_open");
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0);
bufferInfoTabAddr[bufferIndex].ownerPid = getpid();
bufferInfoTabAddr[bufferIndex].addr = addr;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ perror("munmap"); return -1; }
return 0;
}

View file

@ -0,0 +1,36 @@
#include "libnazgul.h"
int msgBufferDetachProc(
msgPoolData * poolDataTabAddr,
int poolIndex,
int bufferIndex,
void * addr
){
msgBufferInfoTabId bufferInfoTabId;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
//récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE);
if (bufferInfoTabFd<0){
perror("shm_open");
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0);
bufferInfoTabAddr[bufferIndex].ownerPid = (pid_t)-1;
bufferInfoTabAddr[bufferIndex].addr = NULL;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ perror("munmap"); return -1; }
return 0;
}

View file

@ -0,0 +1,35 @@
#include "libnazgul.h"
int msgBufferGetFreeIndex(msgPoolData * poolDataTabAddr,int poolIndex){
msgBufferInfoTabId bufferInfoTabId;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
int bufferFreeIndex;
//récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE);
if (bufferInfoTabFd<0){
perror("shm_open");
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0);
int i=0;
while ((i<bufferInfoNb)
&& (bufferInfoTabAddr[i].ownerPid != (pid_t)-1)){ i++; }
if (i == bufferInfoNb){ return -1; }
bufferFreeIndex=i;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ perror("munmap"); return -1; }
return bufferFreeIndex;
}

View file

@ -1,72 +0,0 @@
#include "libnazgul.h"
#include "nzg_ids.h"
int msgPoolCreate(
msgSpaceId spaceId,
int poolIdx,
int buffNb,
int buffSize
) {
int poolFd;
int bufferTabFd;
sem_t * ressourceSem;
msgPoolId poolId;
msgBufferInfoTabId bufferInfoTabId;
msgPoolSemId poolRessourceSemId;
/* creation des infos sur buffers DEBUT */
if (msgBufferInfoTabIdIntern(bufferInfoTabId,spaceId,poolIdx) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n", (char*)poolId );
return -1;
}
bufferTabFd=shm_open(bufferInfoTabId,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE);
if (bufferTabFd == -1 ) {
fprintf( stderr, "msgInfoTab : %s creation failed: %s\n",bufferInfoTabId,
strerror( errno ) );
return -1;
}
if (ftruncate(bufferTabFd, buffNb*sizeof(msgBufferInfo)) == -1){
fprintf( stderr, "msgBufferInfoTab resizing failed: %s\n",
strerror( errno ) );
return -1;
}
/* creation des infos sur buffers FIN */
/* creation des buffers DEBUT */
if (msgPoolIdIntern(poolId,spaceId,poolIdx) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n", (char*)poolId );
return -1;
}
poolFd=shm_open(poolId,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE);
if (poolFd == -1 ) {
fprintf( stderr, "msgPool : %s creation failed: %s\n",poolId,
strerror( errno ) );
return -1;
}
if (ftruncate(poolFd, (buffSize*buffNb)) == -1){
fprintf( stderr, "msgPool resizing failed: %s\n",
strerror( errno ) );
return -1;
}
/* creation des buffers FIN */
if (msgPoolSemIdIntern(poolRessourceSemId,spaceId,poolIdx) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n",
(char*)poolRessourceSemId );
return -1;
}
// on met un semaphore sur le pool
ressourceSem = sem_open(poolRessourceSemId, O_CREAT|O_EXCL, 0666, buffNb);
//TODO: verrifier les erreurs sur l'ouverture de la sem
// on met le pool a la taille voulue pour qu'il
// puisse contenir les buffs
close(poolFd);
return 0;
}