Hi All!,
I been trying to share memory between two process but without luck. The
first process will create the share memory without problem, but it’s the
second process that giving me problems. The second process will open the
share memory object without problems with sh_open(), but when I use mmap()
it always returns NOT SUPPORTED(48). Also the share memory name and size
is: “/dev/shmem/SCDM” and 8192bytes, thus I am using shmem an not a file
system. Below is the code I am using:
////////////////////////////////////////////////////////////////////////////
/////
Process #1: (Creates the Share memory object)
////////////////////////////////////////////////////////////////////////////
////
fw_error CDataModule::Create( const char* name, fw_int32 size )
{
FW_PRECONDITION( name != 0 );
FW_PRECONDITION( size > 0 );
fw_int16 osErr, shmem_fd;
unsigned char *addr; // Use for the share memory addresse
osErr = EOK; // No error set
// What do we create here share memory
if ( (shmem_fd = shm_open( name, O_EXCL|O_CREAT|O_RDWR, 0777 )) == -1)
{
osErr = errno;
}
else // We need to size and map the share memory
{
// Size the File
ftruncate( shmem_fd, size + sizeof(_Header));
// the only process that can get here is ESLServices
if ((addr = (unsigned char*)mmap(0, size + sizeof(_Header),
PROT_WRITE|PROT_EXEC|PROT_READ, MAP_SHARED, shmem_fd, 0)) == MAP_FAILED)
{
osErr = errno;
}
// closing the file decriptor since we do not need it anymore
close(shmem_fd);
}
if ( osErr == ENAMETOOLONG )
return fw_errmsg( err_BadName, “CDataModule::Create( %s ) Name to long\n”,
name );
else if ( osErr == EEXIST )
return err_KnownModule;
else if ( osErr == ENOSPC )
return err_KnownModule; // No RAM results even though the DM exists.
else if ( osErr != fw_success )
return fw_errmsg( fw_err_unknown, “CDataModule::Create( %s ) returned
unexcepted error %d\n”, name, osErr );
FW_POSTCONDITION( addr != 0 ); // check to make sure we ahve a address
(unsigned char*)m_pHeader = addr; // Address back from mmap.
m_pHeader->MagicNum = MagicNum;
m_pHeader->MaxSize = size;
m_pHeader->AllocatedSize = 0;
m_pHeader->pRoot = 0;
m_pDataStart = (fw_int8*)( m_pHeader+1 ); // data starts right after the
header
m_Lock.Initialize( m_pHeader->Lock, fw_true );
Dump();
FW_POSTCONDITION( m_pDataStart != 0 );
return fw_success;
}
////////////////////////////////////////////////////////////////////////////
/////////////////////////////
Process #2:
////////////////////////////////////////////////////////////////////////////
////////////////////////////
fw_error CDataModule::Open( const char* name, fw_int32 size )
{
FW_PRECONDITION( name != 0 );
fw_int16 osErr = 0;
fw_int16 shmem_fd;
unsigned char* addr; // Use for the share memory addresse
//unsigned char* tmpaddr; // Use for the share memory addresse
//tmpaddr = 0x40100000;
//Dump();
// What do we create here share memory
if ( shmem_fd = shm_open( name, O_EXCL|O_RDWR, 0777 ) == -1)
{
osErr = errno;
}
else // We need to size and map the share memory
{
// Lets get the address space of the share Memory
//addr = (unsigned char*)m_pHeader; // Address back from mmap.
//ftruncate( shmem_fd, size);
// We map to the same space has ESLServices
if ((addr =(unsigned char*)mmap( 0, size + sizeof(_Header),
PROT_WRITE|PROT_EXEC|PROT_READ, MAP_NOSYNCFILE|MAP_SHARED, shmem_fd, 0)) ==
MAP_FAILED)
//if ((addr =(unsigned char*)mmap( 0, size + sizeof(_Header),
PROT_WRITE|PROT_READ, MAP_SHARED, shmem_fd, 0)) == MAP_FAILED)
{
osErr = errno;
}
close(shmem_fd);
}
if ( osErr == ENAMETOOLONG )
return fw_errmsg( err_BadName, “CDataModule::Open( %s ) Name to long\n”,
name );
else if ( osErr != fw_success )
return fw_errmsg( fw_err_unknown, “CDataModule::Open returns unexpected
error %d\n”, osErr );
FW_POSTCONDITION( addr != 0 );
(unsigned char*)m_pHeader = addr; // Address back from mmap.
// debug
Dump();
// debug
if ( m_pHeader->MagicNum != MagicNum )
return fw_errmsg( err_InvalidModule, “CDataModule::Open( %s ) invalid
module\n”, name );
m_pDataStart = (fw_int8*)( m_pHeader+1 );
m_Lock.Initialize( m_pHeader->Lock );
FW_POSTCONDITION( m_pDataStart != 0 );
return fw_success;
}