POSIX shared-memory API

Several IPC mechanisms are available for POSIX systems, including shared memory and message passing. Here, we explore the POSIX API for shared memory.

POSIX shared memory is organized using memory-mapped files, which associate the region of shared memory with a file. A process must first create a shared-memory object using the shm_open() system call, as follows:

shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
Parameters:
name: The first parameter specifies the name of the shared-memory object. 
Processes that wish to access this shared memory must refer to the 
object by this name.
O_CREAT | O_RDWR : The subsequent parameters specify that the shared-memory 
object is to be created if it does not yet exist (O_CREAT) and that the object is 
open for reading and writing (O_RDWR).

The last parameter establishes the directory permissions of the 
shared-memory object.

A successful call to shm_open() returns an integer file descriptor for the shared-memory object. Once the object is established, the ftruncate() function is used to configure the size of the object in bytes. The call

 ftruncate(shm_fd, 4096);

sets the size of the object to 4, 096 bytes. Finally, the mmap() function establishes a memory-mapped file containing the shared-memory object. It also returns a pointer to the memory-mapped file that is used for accessing the shared-memory object.

Programs showing POSIX shared memory API for producer and consumer



filter_none

edit
close

play_arrow

link
brightness_4
code

C program for Producer process illustrating POSIX shared-memory API.
  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
  
int main()
{
    /* the size (in bytes) of shared memory object */
    const int SIZE = 4096;
  
    /* name of the shared memory object */
    const char* name = "OS";
  
    /* strings written to shared memory */
    const char* message_0 = "Hello";
    const char* message_1 = "World!";
  
    /* shared memory file descriptor */
    int shm_fd;
  
    /* pointer to shared memory obect */
    void* ptr;
  
    /* create the shared memory object */
    shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
  
    /* configure the size of the shared memory object */
    ftruncate(shm_fd, SIZE);
  
    /* memory map the shared memory object */
    ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
  
    /* write to the shared memory object */
    sprintf(ptr, "%s", message_0);
  
    ptr += strlen(message_0);
    sprintf(ptr, "%s", message1);
    ptr += strlen(message_1);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for Consumer process illustrating
// POSIX shared-memory API.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
  
int main()
{
    /* the size (in bytes) of shared memory object */
    const int SIZE = 4096;
  
    /* name of the shared memory object */
    const char* name = "OS";
  
    /* shared memory file descriptor */
    int shm_fd;
  
    /* pointer to shared memory object */
    void* ptr;
  
    /* open the shared memory object */
    shm_fd = shm_open(name, O_RDONLY, 0666);
  
    /* memory map the shared memory object */
    ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
  
    /* read from the shared memory object */
    printf("%s", (char*)ptr);
  
    /* remove the shared memory object */
    shm_unlink(name);
    return 0;
}
chevron_right

The above programs use the producer–consumer model in implementing shared memory.

References
Silberschatz’s Operating System concepts

This article is contributed by Mayank Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





Improved By : HamidRamazani

Article Tags :