In Linux, a shared memory is a type of inter-process communication mechanism, a segment is part of the memory which is shared between two or more processes to facilitate the communication and sharing of data between these processes.
Please find below example to understand more,
Process 1: Writer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHM_KEY 12345
#define SHM_SIZE 1024
int main() {
int shm_id;
char *shm_ptr;
char buf[SHM_SIZE];
// Create a shared memory segment with key SHM_KEY and size SHM_SIZE
shm_id = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | S_IRUSR | S_IWUSR);
if (shm_id < 0) {
perror("Error creating shared memory segment");
exit(1);
}
// Attach the shared memory segment to the process's address space
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("Error attaching shared memory segment");
exit(1);
}
// Write a message to the shared memory segment
sprintf(buf, "Hello from Writer process !");
memcpy(shm_ptr, buf, strlen(buf) + 1);
// Detach the shared memory segment
shmdt(shm_ptr);
return 0;
}
Process 2: Reader
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHM_KEY 12345
#define SHM_SIZE 1024
int main() {
int shm_id;
char *shm_ptr;
char buf[SHM_SIZE];
// Create a shared memory segment with key SHM_KEY and size SHM_SIZE
shm_id = shmget(SHM_KEY, SHM_SIZE, S_IRUSR | S_IWUSR);
if (shm_id < 0) {
perror("Error creating shared memory segment");
exit(1);
}
// Attach the shared memory segment to the process's address space
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("Error attaching shared memory segment");
exit(1);
}
// Read the message from the shared memory segment
memcpy(buf, shm_ptr, SHM_SIZE);
printf("Received message: %s\n", buf);
// Detach the shared memory segment
shmdt(shm_ptr);
// Delete the shared memory segment
shmctl(shm_id, IPC_RMID, NULL);
return 0;
}
In this example code, the writer process creates a shared memory segment using the "shmget" system call and attaches it to its address space by using the "shmat" system call. Then writes a message to the shared memory segment using the standard library function "sprintf" and copies the message into the shared memory segment using the "memcpy" function. After that, it detaches the shared memory segment using the "shmdt" system call.
The reader process creates a shared memory segment using the "shmget" system call and attaches it to its address space using the "shmat" system call. Then reads the message from the shared memory segment using the "memcpy" function and prints it to the console using the "printf" function. After that, it detaches the shared memory segment using the "shmdt" system call and deletes the shared memory segment using the "shmctl".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHM_KEY 12345
#define SHM_SIZE 1024
int main() {
int shm_id;
char *shm_ptr;
char buf[SHM_SIZE];
// Create a shared memory segment with key SHM_KEY and size SHM_SIZE
shm_id = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | S_IRUSR | S_IWUSR);
if (shm_id < 0) {
perror("Error creating shared memory segment");
exit(1);
}
// Attach the shared memory segment to the process's address space
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("Error attaching shared memory segment");
exit(1);
}
// Write a message to the shared memory segment
sprintf(buf, "Hello from Writer process !");
memcpy(shm_ptr, buf, strlen(buf) + 1);
// Detach the shared memory segment
shmdt(shm_ptr);
return 0;
}
Process 2: Reader
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHM_KEY 12345
#define SHM_SIZE 1024
int main() {
int shm_id;
char *shm_ptr;
char buf[SHM_SIZE];
// Create a shared memory segment with key SHM_KEY and size SHM_SIZE
shm_id = shmget(SHM_KEY, SHM_SIZE, S_IRUSR | S_IWUSR);
if (shm_id < 0) {
perror("Error creating shared memory segment");
exit(1);
}
// Attach the shared memory segment to the process's address space
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("Error attaching shared memory segment");
exit(1);
}
// Read the message from the shared memory segment
memcpy(buf, shm_ptr, SHM_SIZE);
printf("Received message: %s\n", buf);
// Detach the shared memory segment
shmdt(shm_ptr);
// Delete the shared memory segment
shmctl(shm_id, IPC_RMID, NULL);
return 0;
}
In this example code, the writer process creates a shared memory segment using the "shmget" system call and attaches it to its address space by using the "shmat" system call. Then writes a message to the shared memory segment using the standard library function "sprintf" and copies the message into the shared memory segment using the "memcpy" function. After that, it detaches the shared memory segment using the "shmdt" system call.
The reader process creates a shared memory segment using the "shmget" system call and attaches it to its address space using the "shmat" system call. Then reads the message from the shared memory segment using the "memcpy" function and prints it to the console using the "printf" function. After that, it detaches the shared memory segment using the "shmdt" system call and deletes the shared memory segment using the "shmctl".