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?
Topics in this section,
Topics in this section,
Let us answer few basic questions in this shared memory
What is the purpose of shm_open
in the code?
See Answer
shm_open
is used to create or open a shared memory object identified by the specified name.
Why is O_RDWR
used as a flag in shm_open
?
See Answer
O_RDWR
specifies that the shared memory object should be opened for both reading and writing.
What does the 0666
permission represent in shm_open
?
See Answer
It sets the permissions for the shared memory object, allowing read and write access for all users.
How is the shared memory object identified in the code?
See Answer
The shared memory object is identified by the parameter name
passed to shm_open
.
What is the purpose of mmap
in the code?
See Answer
mmap
is used to map the shared memory object into the process’s address space,
creating a pointer (ptr)
to the shared memory.
Why is PROT_READ | PROT_WRITE
used in mmap
?
See Answer
It specifies that the mapped region should have read and write access.
What does MAP_SHARED
indicate in mmap
?
See Answer
MAP_SHARED
indicates that the mapping should be shared between multiple processes.
How is the shared memory size determined in the code?
See Answer
The SIZE
parameter passed to mmap determines the size of the shared memory region.
Is it possible to use shm_open
on an existing shared memory object?
See Answer
Yes, if the shared memory object with the specified name already exists, shm_open
will open it.
How does error handling work for shm_open
and mmap
in this code?
See Answer
Check the return values of shm_open
and mmap
for errors. If they return -1, examine the errno
variable for details.
Why is ptr
used as a pointer to the shared memory?
See Answer
ptr
is a pointer to the mapped region of the shared memory, allowing easy access to the shared data.
Why is close
used in the shared memory?
See Answer
After the shared memory is mapped and used, the file descriptor is closed using close
.
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!
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!
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!
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. |
Previous topic
Current topic
Other IPCs