-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathSharedMemoryWrapper.c
executable file
·86 lines (64 loc) · 1.33 KB
/
SharedMemoryWrapper.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "SharedMemoryWrapper.h"
int sharedMemoryGet(key_t key, int size)
{
int shmid;
if ((shmid = shmget(key, size, 0666)) == -1)
return -1;
return shmid;
}
int sharedMemoryCreate(key_t key, int size)
{
int shmid;
if ((shmid = shmget(key, size, IPC_CREAT | 0666)) == -1)
return -1;
return shmid;
}
int sharedMemoryCreateOrGet(key_t key, int size)
{
int shmid;
/* If memory has already been created.. then just get it: */
if ((shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0666)) == -1)
{
if( errno != EEXIST )
return -1;
if ((shmid = shmget(key, size, 0666)) == -1)
return -1;
}
return shmid;
}
int sharedMemoryCreateIfGone(key_t key, int size)
{
int shmid;
/* If memory has already been created.. then just get it: */
if ((shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0666)) == -1)
return errno;
return shmid;
}
void* sharedMemoryAttach(int shmid)
{
return shmat(shmid, 0, 0);
}
int sharedMemoryDetatch(const void* shmaddr)
{
if( shmdt(shmaddr) == -1 )
return errno;
return 0;
}
int sharedMemoryDelete(int shmid)
{
if( shmctl(shmid, IPC_RMID, 0) == -1 )
return errno;
return 0;
}
int sharedMemoryLock(int shmid)
{
if( shmctl(shmid, SHM_LOCK, 0) == -1 )
return errno;
return 0;
}
int sharedMemoryUnlock(int shmid)
{
if( shmctl(shmid, SHM_UNLOCK, 0) == -1 )
return errno;
return 0;
}