Mario Mastrodicasa
12/06/2007 5:06 AM
post3410
|
I have two processes which need to share the same shared memory. The shared memory represents the video memory of the
VNC Server.
The two processes use this shared memory to write on the video.
There is a common class that manages all and is instatiated by the two processes. The name of the shared memory isn't
constant by design.
If the first one create the shared memory as:
bool CSharedMemory::create(int size)
{
sprintf(shmFile, "/qws_%p", this);
shmId = shm_open(shmFile, O_RDWR | O_CREAT , S_IRUSR | S_IWUSR );
if (shmId == -1)
{
perror("QSharedMemory::create allocating shared memory");
return false;
}
ftruncate( shmId, size );
shmBase = mmap( 0, size, PROT_READ|PROT_WRITE, MAP_SHARED, shmId, 0 );
// close(shmId);
// if (shm_unlink(shmFile.toLatin1().constData()) == -1)
// {
// perror("QSharedMemory::create shm_unlink shared memory");
// }
if (shmBase == (void*)-1) {
#warning "SHM_DEBUG attivo"
perror("QSharedMemory::create attaching to shared memory");
shmBase = 0;
return false;
}
return true;
}
The second one, through a Unix Domain Socket, receive the file descriptor and execute:
bool CSharedMemory::attach(int id)
{
if (shmId == id)
return id != -1;
if (shmId != -1)
detach();
struct stat buf;
fstat(id, &buf);
if (S_TYPEISSHM(&buf))
shmBase = mmap( 0, size(), PROT_READ|PROT_WRITE, MAP_SHARED, id, 0 );
if (shmBase == (void*)-1) {
perror("QSharedMemory::attach attaching to shared memory");
shmBase = 0;
return false;
}
shmId = id;
return true;
}
When the attach methos is called the error is:
QSharedMemory::attach attaching to shared memory: Not supported.
What means? Is not possible to attach to a common shared memory through a file descriptor? Or there are problems with
the shared fd? (The fd is local to the process and can't be exported outside...)
Through the system process inspector I can see the open file descriptor and in /dev/shmem there is the shared memory...
|
|
|