shm_open() 在使用相同名称重新打开时失败,但再次使用 O_CREAT
shm_open() fails while reopening with same name though O_CREAT is used again
根据 http://man7.org/linux/man-pages/man3/shm_open.3.html,它表示
After a successful shm_unlink(), attempts to shm_open() an object with the same name fail (unless O_CREAT was
specified, in which case a new, distinct object is created).
S,这个我试过了。我正在使用下面的示例,它在执行 shm_unlink 之后创建新的共享内存对象,正如他们所说,我使用 O_CREAT.
但是当我 运行 这个问题时,它给了我相关的错误 bus error
。
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
// Open shared memory
int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int));
// Map shared memory
int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
close(fd);
// Access shared memory
*shm = 0;
// Unmap shared memory
munmap(shm, sizeof(int));
if(shm_unlink("TEST")){
printf("************success****************");
}
fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
*shm2 = 0;
return 0;
}
在shm_unlink之后再次创建同名共享内存的正确过程是什么?
您在第二次尝试时访问了错误的共享内存(应该是 shm2,而不是 shm),并且不要忘记截断。
同样不相关,但 shm_unlink returns 0 成功。
#include <sys/mman.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void) {
// Open shared memory
int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int));
// Map shared memory
int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
close(fd);
// Access shared memory
*shm = 0;
// Unmap shared memory
munmap(shm, sizeof(int));
if(!shm_unlink("TEST")){
printf("************success****************");
}
fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
ftruncate(fd, sizeof(int));
*shm2 = 0;
return 0;
}
根据 http://man7.org/linux/man-pages/man3/shm_open.3.html,它表示
After a successful shm_unlink(), attempts to shm_open() an object with the same name fail (unless O_CREAT was
specified, in which case a new, distinct object is created).
S,这个我试过了。我正在使用下面的示例,它在执行 shm_unlink 之后创建新的共享内存对象,正如他们所说,我使用 O_CREAT.
但是当我 运行 这个问题时,它给了我相关的错误 bus error
。
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
// Open shared memory
int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int));
// Map shared memory
int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
close(fd);
// Access shared memory
*shm = 0;
// Unmap shared memory
munmap(shm, sizeof(int));
if(shm_unlink("TEST")){
printf("************success****************");
}
fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
*shm2 = 0;
return 0;
}
在shm_unlink之后再次创建同名共享内存的正确过程是什么?
您在第二次尝试时访问了错误的共享内存(应该是 shm2,而不是 shm),并且不要忘记截断。
同样不相关,但 shm_unlink returns 0 成功。
#include <sys/mman.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void) {
// Open shared memory
int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int));
// Map shared memory
int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
close(fd);
// Access shared memory
*shm = 0;
// Unmap shared memory
munmap(shm, sizeof(int));
if(!shm_unlink("TEST")){
printf("************success****************");
}
fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
ftruncate(fd, sizeof(int));
*shm2 = 0;
return 0;
}