-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_memory.c
41 lines (30 loc) · 921 Bytes
/
shared_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* Compiler flags: -Wall -Werror -Wextra -std=gnu11 -lrt
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define SHARED_MEMORY_SIZE 256
#define SHARED_MEMORY_PATH "/ps_os"
int main(int argc, char const *argv[]) {
//creating shared memory file descriptors and error checking
int smfd = shm_open(SHARED_MEMORY_PATH, O_RDWR | O_CREAT, 0666);
if (smfd == -1) {
// shared memory error
}
//setting the size of the shared memory
int trun_err = ftruncate(smfd, SHARED_MEMORY_SIZE);
if (trun_err == -1) {
// shared memory error
}
//mapping shared memory into heap
void * shared_memory = mmap(NULL, SHARED_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, smfd, 0);
if (shared_memory == MAP_FAILED) {
// shared memory mapping error
}
//unlinking the shared memory
shm_unlink(SHARED_MEMORY_PATH);
return EXIT_SUCCESS;
}