Creating and Unlinking a File Descriptor from a Shared Memory

The shm_open() function enables you to create shared memory and associate a file descriptor. Use the shm_unlink() function to remove (unlink a file descriptor from a shared memory) the name of the shared memory object.

Example:

#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
int main(void)
    {
    int fd;// File Descriptor
    int ret;
    if((fd = shm_open("page", O_RDWR|O_CREAT, 0666)) < 0)
        {
        printf("Shared memory creation failed with errno %d\n", errno);
        }
    else
        {
        printf("Shared memory creation was successful\n");
        }
    close(fd);//closing the file descriptor
    if((ret = shm_unlink("page")) < 0) 
        {
        printf("Shared memory unlinking failed with errno %d\n", errno);
        }
    else
        {
        printf("Shared memory unlinking was successful\n");
        }
    return ret;
    }

Related concepts