我可以在 QNX 上使用 shm_open 而不是 shmget 吗

can I use shm_open instead of shmget on QNX

我是 QNX 平台的新手,我们正在将 Linux 项目移植到 QNX。并找到与使用 shmget 系统调用在 linux 中创建的共享内存相关的代码。但 shmget 不存在于 QNX 中。我看到了类似的调用 shm_open,我不知道两者的区别 b/w。

我的直接问题是,我应该在 QNX 平台中使用 shm_open 而不是 shmget 吗?如果是,如何?如果没有,为什么不呢?

首先QNX不支持shmget()API。

You will need to use shm_open() instead.

下面是在线 QNX 文档中的示例程序
这演示了 shm_open()QNX:

上的正确使用
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>

int main( int argc, char** argv )
{
    int fd;
    unsigned* addr;

    /*
     * In case the unlink code isn't executed at the end
     */
    if( argc != 1 ) {
        shm_unlink( "/bolts" );
        return EXIT_SUCCESS;
    }

    /* Create a new memory object */
    fd = shm_open( "/bolts", O_RDWR | O_CREAT, 0777 );
    if( fd == -1 ) {
        fprintf( stderr, "Open failed:%s\n",
            strerror( errno ) );
        return EXIT_FAILURE;
    }

    /* Set the memory object's size */
    if( ftruncate( fd, sizeof( *addr ) ) == -1 ) {
        fprintf( stderr, "ftruncate: %s\n",
            strerror( errno ) );
       return EXIT_FAILURE;
    }

    /* Map the memory object */
    addr = mmap( 0, sizeof( *addr ),
            PROT_READ | PROT_WRITE,
            MAP_SHARED, fd, 0 );
    if( addr == MAP_FAILED ) {
        fprintf( stderr, "mmap failed: %s\n",
            strerror( errno ) );
        return EXIT_FAILURE;
    }

    printf( "Map addr is 0x%08x\n", addr );

    /* Write to shared memory */
    *addr = 1;

    /*
     * The memory object remains in
     * the system after the close
     */
    close( fd );

    /*
     * To remove a memory object
     * you must unlink it like a file.
     *
     * This may be done by another process.
     */
    shm_unlink( "/bolts" );

    return EXIT_SUCCESS;
}

希望这对您有所帮助。