Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
BroadcastCommunity.qnx.com will be offline from May 31 6:00pm until June 2 12:00AM for upcoming system upgrades. For more information please go to https://community.qnx.com/sf/discussion/do/listPosts/projects.bazaar/discussion.bazaar.topc28418
Forum Topic - shared memory and shm fd: (4 Items)
   
shared memory and shm fd  
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...
Re: shared memory and shm fd  
Since you mentioned transfering fd cross unix domain socket, there is a discussion (and sample code) in core-network 
project. (http://community.qnx.com/sf/discussion/do/listPosts/projects.networking/discussion.technology.topc1501)

You need to make sure your sender does not close fd too early to be able to successfully transfer an fd cross UDS.
RE: shared memory and shm fd  
Wouldn't it be much easier to just send the name of the shared memory
object?
BTW is this Qt code? 

[snip]
> The second one, through a Unix Domain Socket, receive the file descriptor
and execute:
Re: RE: shared memory and shm fd  
You are right...
It is a possible porting of the QT code, but QNX doesn't support call like shmat, shmdt, shmget, shmctl, as expected by 
the library. I'm trying to make a workaround to the default behaviour...