0
我是QNX平台的新手,我们正在将Linux项目移植到QNX。并发现与使用shmget系统调用的linux中的共享内存创建相关的代码。但是在QNX中没有出现。我看到了类似的调用shm_open,我不知道两者的区别。我可以在QNX上使用shm_open而不是shmget
我的直接问题是,我应该在QNX平台中使用shm_open而不是shmget吗?如果是,如何?如果没有,为什么不呢?
我是QNX平台的新手,我们正在将Linux项目移植到QNX。并发现与使用shmget系统调用的linux中的共享内存创建相关的代码。但是在QNX中没有出现。我看到了类似的调用shm_open,我不知道两者的区别。我可以在QNX上使用shm_open而不是shmget
我的直接问题是,我应该在QNX平台中使用shm_open而不是shmget吗?如果是,如何?如果没有,为什么不呢?
首先QNX不支持shmget()
API。
您将需要使用
shm_open()
来代替。
下面是从网上QNX文档
演示上QNX正确使用shm_open()
一个示例程序:
#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;
}
希望这有助于。