Hello Dadji
Where do you actually check to see if the return is 0? In the
Config_ShMem_Create function or somewhere else in code? Is there a
chance that the pointer is getting modified somewhere else?
Also, what name are you using for the shared memory object?
Thanks,
Rodney
dadji wrote:
Hello Rodney,
in the attachment there is the code to create and to release the shared
memory. I hope you will have any suggestion.
Thanks.
Dadji
“Rodney Dowdall” <> rdowdall@qnx.com> > schrieb im Newsbeitrag
news:ep5e27$74b$> 1@inn.qnx.com> …
Hello Dadji
Can you post a code sample of what you are doing?
Thanks,
Rodney
dadji wrote:
hallo,
i m programming a middelware application on and i have some problems
with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call
gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.
Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while
opening
a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?
thanks for helping me.
best regards
Dadji
/**
- function used to create a shared memory
/
void Config_ShMem_Create( char* pcMsgName, t_int32 *pFd_ShMem, t_uint32
pLength )
{
char acShMemName[ MAX_SHMEM_NAME_LENGTH ];
void pMemory;
// make ShMemName conform to POSIX standard (leading “/”)
acShMemName[0] = ‘/’;
acShMemName[1] = 0;
strcat( acShMemName, pcMsgName );
// get file descriptor for SharedMem
if( ( *pFd_ShMem = shm_open( acShMemName, O_RDWR | O_CREAT, 0777 ) ) ==
FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf ( stderr, __STR_WARNING__SHMEM_CREATION_FAILED, acModuleName,
pcMsgName );
printf(""%s" shmemCreate[%s] error ERRNO[%d] MEANING[%s]\n",
acModuleName, acShMemName, errno, strerror(errno));
}
return( NULL );
}
// define length of SharedMem region to register
if( ftruncate( *pFd_ShMem, *pLength ) == FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_SIZE_FAILED, acModuleName,
pcMsgName );
}
return( NULL );
}
// get address of SharedMem object
if( ( pMemory = mmap( 0, *pLength, PROT_READ | PROT_WRITE |
PROT_NOCACHE, MAP_SHARED, *pFd_ShMem, 0 ) ) == MAP_FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_MAPPING_FAILED, acModuleName,
pcMsgName );
}
return( NULL );
}
// fill SharedMem with MIRPA_SHMEM_INIT_VALUE
memset( pMemory, MIRPA_SHMEM_INIT_VALUE, *pLength );
// print debug data
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
printf("\nCreatesharedmem(): Name[%s], file descriptor[%d], length[%d]
[0x%0x]\n\n", pcMsgName, *pFd_ShMem, *pLength, pMemory);
}
// return pointer to shared mem
return( pMemory );
}
/**
- function to delete the shared memory
*/
void DeleteShMem(char * name, void * pMemory, int *pLength, int
fd_ShMem )
{
// unmap shared memory
if( munmap(pMemory, *pLength ) == -1)
{
printf(""%s" error while umaping shared memory[%s] length[%d]\n",
acModuleName, name,*pLength);
}
// close file descriptor
if ( fd_ShMem) == -1)
{
if ( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf(stderr, “%s WARNING: couldn’t close shared memory file
descriptor[%d]\n”, acModuleName,fd_ShMem);
printf( “%s: WARNING : ERRNO[%d] MEANING[%s]\n”, acModuleName, errno,
strerror(errno) );
}
}
// unlink the memory
strcpy(acTempShMem, “/”);
strcat(acTempShMem, name);
if ((iUnlintvalue = shm_unlink(acTempShMem)) == -1)
{
printf(""%s" shm_unlink[%s] error ERRNO[%d] MEANING[%s]\n",
acModuleName, acTempShMem, errno, strerror(errno));
}
}