Basic example shared memory reader and writer

  • In this program, you are going to learn

  • How to create or open shared memory objects?

  • How to map a virtual address space?

  • How to share and recv data in shared memory objects?

Let us answer few basic questions in this shared memory

What is the purpose of shm_open in the code?

Why is O_RDWR used as a flag in shm_open?

What does the 0666 permission represent in shm_open?

How is the shared memory object identified in the code?

What is the purpose of mmap in the code?

Why is PROT_READ | PROT_WRITE used in mmap?

What does MAP_SHARED indicate in mmap?

How is the shared memory size determined in the code?

Is it possible to use shm_open on an existing shared memory object?

How does error handling work for shm_open and mmap in this code?

Why is ptr used as a pointer to the shared memory?

Why is close used in the shared memory?

https://www.plantuml.com/plantuml/svg/ROyn2y8m58Jt_8fdGKWbGwSE5S522tQGbYW8BYKshxRGf269dVpnHbqSt8wxUz_nziuBwnzg8Yi_e49mHdGuYHlv12HpWsfdWvfgeP11tVBYmXaaQPh6Mv8PRs47IWb34mQdwbeoQ7XzRdcP5_3ywWklpi4_varxEkIyB1YyiRqCd0_4ZjhtDE8e95gmsE6ueCnWxI86j1k4tSHXwRVhKCjHt-5lYIxpAEDkcXtIxr9mdPyDsOUpyEqB
  • There are many functions used in shared memory . We can classify those functions based on functionalities.

    • shm_open

    • mmap

    • ptr shared data

    • close

  • shm_open is used to opens a shared memory object. For example,

shm_fd = shm_open(name, O_RDWR, 0666);
  • mmap is used to maps it into the process’s address space. For example,

ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  • ptr is a pointer to the mapped region of the shared memory, allowing easy access to the shared data. For example,

printf("Reader received: %s", (char*)ptr);
printf("sending : %s", (char*)ptr);
  • close used to close the file descriptor associated with the shared memory object. For example,

(void)close(shm_fd);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

int main() 
{
  int shm_fd;
  const int SIZE = 4096;
  const char* name = "/my_shm";
  int ret;
  void* ptr;

  shm_fd = shm_open(name, 
  O_RDWR, 0666);

  ptr = mmap(0, SIZE, 
  PROT_READ | PROT_WRITE, 
  MAP_SHARED, shm_fd, 0);

  printf("Reader received: %s", 
  (char*)ptr);
  printf("\n");

  ret = sprintf(
  ptr, "Hello from Reader!");
  
  if (ret > 0) {
    printf("sending : %s", 
    (char *)ptr);
    printf("\n");
  } else {
    perror("snprintf");
    (void)close(shm_fd);
    return -1;
  }

  sleep(2);
  (void)close(shm_fd);

  return 0;
}

1$ gcc -o reader reader.c -lrt
2
3$ sudo ./reader
4
5Reader received: Hello from Writer!
6sending : Hello from Reader!
https://www.plantuml.com/plantuml/svg/RO-nYiCm44HxVSLU82075QbS-3Y8YGrn4HmKW-4QezZhM639Gb9IvUEpvskkk6wOVJl3749KFZxCp3xYX0OXEDNZh6viFR0iJAPR75fkbK41TNUKPTx0YvGiMYbWdwPfyiao5pryWJ7AyRs0Q_LT2hZ8kkc8BuXVTIkhXlnpVkckfroMXO2VWd6Wd3N4Qnj7lcczZkZ1Ou_wYKC6sx0HmFj9-Lr2JN_PW7RGzWx_Glovw27fvoKW_sqYDyJ5iGEzqUut
  • There are many functions used in shared memory. We can classify those functions based on functionalities.

    • shm_open

    • ftruncate

    • mmap

    • ptr shared data

    • close

  • shm_open is used to opens a shared memory object. For example,

shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
  • ftruncate is used to truncate a file to a specified length and the file must be open for writing. For example,

ftruncate(shm_fd, SIZE);
  • mmap is used to maps it into the process’s address space. For example,

ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  • ptr is a pointer to the mapped region of the shared memory, allowing easy access to the shared data. For example,

printf("Writer received: %s", (char*)ptr);
printf("sending : %s", (char*)ptr);
  • close used to close the file descriptor associated with the shared memory object. For example,

(void)close(shm_fd);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

int main() 
{
  int shm_fd;
  const int SIZE = 4096;
  const char* name = "/my_shm";
  int ret;
  void* ptr;

  shm_fd = shm_open(name, 
  O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, SIZE);

  ptr = mmap(0, SIZE, 
  PROT_READ | PROT_WRITE, 
  MAP_SHARED, shm_fd, 0);

  printf("Writer received: %s", 
  (char*)ptr);
  printf("\n");

  ret = sprintf(
  ptr, "Hello from Writer!");
  
  if (ret > 0) {
    printf("sending : %s", 
    (char *)ptr);
    printf("\n");
  } else {
    perror("sprintf");
    (void)close(shm_fd);
    return -1;
  }

  sleep(2);
  (void)close(shm_fd);

  return 0;
}

1$ gcc -o writer writer.c -lrt
2
3$ sudo ./writer
4
5Writer received: Hello from Reader!
6sending : Hello from Writer!
https://www.plantuml.com/plantuml/svg/RP31QiCm38RlVWgjCB1N7tBA8TcWWOIjXwp5wIZiOhn4NGonOsolOx27dvlsk9E4zF7fHviGbOzVPY9tSKI349pgSL8VP5cG8en6pWujjSeWXvqKzL5moF8yPoKfNFJm1COeHpCEtVQzuR0NkuCKJLN3xxK_YkqXpTjgBxkNIZGrXulsD2JFHQAJ8Yl9zwWd17eheE4HNjzQsJPTLpqt7MEW0z0V32pTzTh64rq9L0DwyDYZFkDGm7rOSQ3zgFm3I-6MZ3Sse1sq_OH_eVLQboG1otLIqFEi1zPFSq1wpPgW46T7DWbA__e3
  • There are many functions used in shared memory. We can classify those functions based on functionalities.

    • shm_open

    • mmap

    • ptr shared data

    • close

  • shm_open is used to opens a shared memory object. For example,

shm_fd = shm_open(name, O_RDWR, 0666);
  • mmap is used to maps it into the process’s address space. For example,

ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  • ptr is a pointer to the mapped region of the shared memory, allowing easy access to the shared data. For example,

printf("Reader received: %s", (char*)ptr);
printf("sending : %s", (char*)ptr);
  • close used to close the file descriptor associated with the shared memory object. For example,

(void)close(shm_fd);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

#define NUM_MESSAGES 10

int main() 
{
  const int SIZE = 4096;
  const char* name = "/my_shm";
  int shm_fd;
  int ret, i;
  void* ptr;

  shm_fd = shm_open(name, 
  O_RDWR, 0666);

  ptr = mmap(0, SIZE, 
  PROT_READ | PROT_WRITE, 
  MAP_SHARED, shm_fd, 0);

  i = 0;
  while ( i < NUM_MESSAGES) {
    printf("Reader received: %s", 
    (char*)ptr);
    printf("\n");
    sleep(1);

    ret = sprintf(
    ptr, "Hello from Reader!");
    
    if (ret > 0) {
      printf("sending : %s", 
      (char *)ptr);
      printf("\n");
    } else {
      perror("snprintf");
      (void)close(shm_fd);
      return -1;
    }
    ++i;
    sleep(2);
  }

  (void)close(shm_fd);

  return 0;
}

 1$ gcc -o reader reader.c -lrt
 2
 3$ sudo ./reader
 4
 5Reader received: Hello from Writer!
 6sending : Hello from Reader!
 7Reader received: Hello from Writer!
 8sending : Hello from Reader!
 9Reader received: Hello from Writer!
10sending : Hello from Reader!
11Reader received: Hello from Writer!
12sending : Hello from Reader!
13Reader received: Hello from Writer!
14sending : Hello from Reader!
15Reader received: Hello from Writer!
16sending : Hello from Reader!
17Reader received: Hello from Writer!
18sending : Hello from Reader!
19Reader received: Hello from Writer!
20sending : Hello from Reader!
21Reader received: Hello from Writer!
22sending : Hello from Reader!
23Reader received: Hello from Writer!
24sending : Hello from Reader!
https://www.plantuml.com/plantuml/svg/RP11IyGm48Nl-HNZWf2uEVJKGwkmnHRTGzqbhHIybDZEsa3JbYIk2FvuP-iUFSrZvkEzn-nyq2vysf7TX04jWbzqXwD-P-k19NwmxRpWn2TjKSAXVLH5LiCFAPKtIa8Knx58MR841mzWhLvu9A7QlnKIZkfGjyJdnA-wKVkQzcLsRAldJ1MvX4l0gIUVYuaXYoXbNuCP4VXrW85xU7ajsxAegkofg8G0uu5_enUKwymKJdpJE1FGWSCEpHdx16xzHWBl1ktk19LRErvPZrDlfW_u5zfkJSe8MDDPmi-pwKKtpXxvNrU2V9WNjYE8FlSB
  • There are many functions used in shared memory. We can classify those functions based on functionalities.

    • shm_open

    • ftruncate

    • mmap

    • ptr shared data

    • close

  • shm_open is used to opens a shared memory object. For example,

shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
  • ftruncate is used to truncate a file to a specified length and the file must be open for writing. For example,

ftruncate(shm_fd, SIZE);
  • mmap is used to maps it into the process’s address space. For example,

ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  • ptr is a pointer to the mapped region of the shared memory, allowing easy access to the shared data. For example,

printf("Writer received: %s", (char*)ptr);
printf("sending : %s", (char*)ptr);
  • close used to close the file descriptor associated with the shared memory object. For example,

(void)close(shm_fd);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

#define NUM_MESSAGES 10

int main() 
{
  const int SIZE = 4096;
  const char* name = "/my_shm";
  int shm_fd;
  int ret, i;
  void* ptr;

  shm_fd = shm_open(name,
  O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, SIZE);

  ptr = mmap(0, SIZE, 
  PROT_READ | PROT_WRITE, 
  MAP_SHARED, shm_fd, 0);

  i = 0;
  while ( i < NUM_MESSAGES) {
    printf("Writer received: %s", 
    (char*)ptr);
    printf("\n");
    sleep(1);
    
    ret = sprintf(
    ptr, "Hello from Writer!");
     
    if (ret > 0) {
      printf("sending : %s", 
      (char *)ptr);
      printf("\n");
    } else {
      perror("sprintf");
      (void)close(shm_fd);
      return -1;
    }
    ++i;
    sleep(2);
  }

  (void)close(shm_fd);

  return 0;
}

 1$ gcc -o writer writer.c -lrt
 2
 3$ sudo ./writer
 4
 5Writer received: Hello from Reader!
 6sending : Hello from Writer!
 7Writer received: Hello from Reader!
 8sending : Hello from Writer!
 9Writer received: Hello from Reader!
10sending : Hello from Writer!
11Writer received: Hello from Reader!
12sending : Hello from Writer!
13Writer received: Hello from Reader!
14sending : Hello from Writer!
15Writer received: Hello from Reader!
16sending : Hello from Writer!
17Writer received: Hello from Reader!
18sending : Hello from Writer!
19Writer received: Hello from Reader!
20sending : Hello from Writer!
21Writer received: Hello from Reader!
22sending : Hello from Writer!
23Writer received: Hello from Reader!
24sending : Hello from Writer!

Shared Memory API

Learning

shm_open

opens a shared memory object.

mmap

maps it into the process’s address space.

ftruncate

truncate a file to a specified length and the file must be open for writing.

close

close the file descriptor associated with the shared memory object.

See Also