shm_open¶
shm_open() creates or opens a POSIX shared memory object.
Prototype¶
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int shm_open(const char *name, int oflag, mode_t mode);
Typical Usage¶
int fd = shm_open("/demo_shm", O_CREAT | O_RDWR, 0600);
if (fd < 0) {
perror("shm_open");
return -1;
}
Important Rules¶
- The name should start with
/. - Use
ftruncate()to set the object size beforemmap(). - Use
shm_unlink()to remove the name when cleanup is required.
Return Value¶
- file descriptor on success
-1on error, witherrnoset
Common Pitfalls¶
Warning
shm_open() creates a memory object, but it does not map it. Use mmap() after setting the size.