-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshared.c
More file actions
40 lines (33 loc) · 665 Bytes
/
shared.c
File metadata and controls
40 lines (33 loc) · 665 Bytes
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
#include "include.h"
extern int errno;
int shared_manage(void** restrict ptr, int * restrict id, key_t key, size_t size) {
int exist = 0;
*id = shmget(key, size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
if(*id == -1) {
if(errno == EEXIST)
exist=1;
else {
perror("shmget");
return -1;
}
}
if(exist) {
*id = shmget(key, size, IPC_CREAT | S_IRUSR | S_IWUSR);
if(*id == -1) {
perror("shmget");
return -1;
}
}
*ptr = shmat(*id, NULL, 0);
if(ptr == (void*) -1) {
perror("shmat");
return -1;
}
if(!exist)
memset(*ptr, 0, size);
return 0;
}
void shared_end(const void* shm) {
if(shmdt(shm) == -1)
perror("shmdt");
}