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;
}
希望這有助於。