forked from chennin/adom-sage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshm.cc
39 lines (31 loc) · 731 Bytes
/
shm.cc
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
#include "jaakkos.h"
void* shm_init(size_t bytes) {
key_t key;
int shmid;
char statnam[1024];
snprintf(statnam, 1024, "/proc/%d/stat", getpid());
/* make the key: */
if ((key = ftok(statnam, 'R')) == -1) {
perror("ftok");
exit(1);
}
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, bytes, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
/* attach to the segment to get a pointer to it: */
void *shm = shmat(shmid, (void *)0, 0);
if (shm == (void *)(-1)) {
perror("shmat");
exit(1);
}
return shm;
}
void shm_deinit(void *shm) {
/* detach from the segment: */
if (shmdt(shm) == -1) {
perror("shmdt");
exit(1);
}
}