mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-25 14:10:41 +00:00
37 lines
849 B
C
37 lines
849 B
C
// gcc consumer.c -o consumer -lrt
|
|
#include <fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main() {
|
|
const char *name = "/my_shared_memory";
|
|
const int SIZE = 4096; // Size of the shared memory object
|
|
|
|
// Open the shared memory object
|
|
int shm_fd = shm_open(name, O_RDONLY, 0666);
|
|
if (shm_fd == -1) {
|
|
perror("shm_open");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// Memory map the shared memory
|
|
void *ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
|
|
if (ptr == MAP_FAILED) {
|
|
perror("mmap");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// Read from the shared memory
|
|
printf("Consumer received: %s\n", (char *)ptr);
|
|
|
|
// Cleanup
|
|
munmap(ptr, SIZE);
|
|
close(shm_fd);
|
|
shm_unlink(name); // Optionally unlink
|
|
|
|
return 0;
|
|
}
|
|
|