mirror of
https://github.com/Hizenberg469/Inter-Process-Communication-IPC-.git
synced 2026-04-19 18:02:24 +03:00
Shared Memory for IPC
This commit is contained in:
21
Shared Memory/CMakeLists.txt
Normal file
21
Shared Memory/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
# CMakeList.txt : CMake project for Shared Memory, include source and define
|
||||
# project specific logic here.
|
||||
#
|
||||
cmake_minimum_required (VERSION 3.8)
|
||||
|
||||
# Enable Hot Reload for MSVC compilers if supported.
|
||||
if (POLICY CMP0141)
|
||||
cmake_policy(SET CMP0141 NEW)
|
||||
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
|
||||
endif()
|
||||
|
||||
project("Shared Memory" VERSION 1.0.0 LANGUAGES C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
add_executable(reader "reader.c" "shm_demo.c")
|
||||
add_executable(writer "writer.c" "shm_demo.c")
|
||||
|
||||
# TODO: Add tests and install targets if needed.
|
||||
26
Shared Memory/reader.c
Normal file
26
Shared Memory/reader.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
extern int
|
||||
read_from_shared_memory(char* mmap_key,
|
||||
char* buffer,
|
||||
unsigned int buff_size,
|
||||
unsigned int bytes_to_read);
|
||||
|
||||
int
|
||||
main(int argc, char* argv[]) {
|
||||
|
||||
char* key = "/introduction";
|
||||
char read_buffer[128];
|
||||
memset(read_buffer, 0, 128);
|
||||
|
||||
int rc = read_from_shared_memory(key, read_buffer, 128, 128);
|
||||
if (rc < 0) {
|
||||
printf("Error reading from shared memory\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Data read = %s\n", (char*)read_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
97
Shared Memory/shm_demo.c
Normal file
97
Shared Memory/shm_demo.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
int
|
||||
create_and_write_shared_memory(char* mmap_key, char* value, unsigned int size) {
|
||||
|
||||
int shm_fd;
|
||||
|
||||
/*
|
||||
Create a shared memory object in kernel space. If shared memory
|
||||
already exists it will truncate it to zero bytes again.
|
||||
*/
|
||||
|
||||
shm_fd = shm_open(mmap_key, O_CREAT | O_RDWR | O_TRUNC, 0660);
|
||||
|
||||
if (shm_fd < 0) {
|
||||
printf("Failure on shm_open on shm_fd, errcode = %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ftruncate(shm_fd, size) == -1) {
|
||||
printf("Error on ftruncate to allocate size of shared memory region\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*Now map the shared memory in kernel space into process's Virtual address space.*/
|
||||
void* shm_reg = mmap(NULL, // let the kernel chose to return the base address of shm memory
|
||||
size, // size of the shared memory to map to virtual address of
|
||||
PROT_READ | PROT_WRITE,// shared memory is Read and Writable
|
||||
MAP_SHARED, // shared memory is accessible by different processes
|
||||
shm_fd, // file descriptor of the shared memory
|
||||
0); // offset from the base address of the physical/shared memory to be mapped
|
||||
|
||||
|
||||
/*
|
||||
shm_reg is the address in process's Virtual address space, just like any other address.
|
||||
The Linux paging mechanism maps this address to starting address of the shared memory region
|
||||
in kernel space. Any operation performed by process on shm_reg address is actually the operation
|
||||
performed in shared memory which resides in kernel.
|
||||
*/
|
||||
|
||||
memset(shm_reg, 0, size);
|
||||
memcpy(shm_reg, value, size);
|
||||
munmap(shm_reg, size);
|
||||
|
||||
/*
|
||||
Reader Process will not able to read shm if writer unlink
|
||||
the name below.
|
||||
*/
|
||||
|
||||
//shm_unlink(mmap_key);
|
||||
close(shm_fd);
|
||||
return size;
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
read_from_shared_memory(char* mmap_key,
|
||||
char* buffer,
|
||||
unsigned int buff_size,
|
||||
unsigned int bytes_to_read) {
|
||||
|
||||
int shm_fd = 0, rc = 0;
|
||||
|
||||
shm_fd = shm_open(mmap_key, O_CREAT | O_RDONLY, 0660);
|
||||
|
||||
if (shm_fd < 0) {
|
||||
|
||||
printf("Failure on shm_open on shm_fd, error code = %d\n", errno);
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
void* shm_reg = mmap(NULL, bytes_to_read, PROT_READ, MAP_SHARED, shm_fd, 0);
|
||||
|
||||
if (shm_reg == MAP_FAILED) {
|
||||
printf("Error on mapping\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(buffer, shm_reg, bytes_to_read);
|
||||
rc = munmap(shm_reg, bytes_to_read);
|
||||
|
||||
if (rc < 0) {
|
||||
printf("munmap failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(shm_fd);
|
||||
return bytes_to_read; /* Return the number of bytes read */
|
||||
}
|
||||
16
Shared Memory/writer.c
Normal file
16
Shared Memory/writer.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
extern int
|
||||
create_and_write_shared_memory(char* mmap_key,
|
||||
char* value,
|
||||
unsigned int size);
|
||||
|
||||
int
|
||||
main(int argc, char* argv[]) {
|
||||
|
||||
char* key = "/introduction";
|
||||
char* intro = "Hello, I'm Hizenberg.";
|
||||
create_and_write_shared_memory(key, intro, strlen(intro));
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user