-
Notifications
You must be signed in to change notification settings - Fork 0
/
CShareMem.cpp
223 lines (194 loc) · 4.26 KB
/
CShareMem.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "CShareMem.h"
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <unistd.h>
#include <iostream>
using namespace std;
union semun
{
int value;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
CShareMem::CShareMem()
{
m_shmID = 0;
m_readSemID = 0;
m_shareMem = NULL;
}
CShareMem::~CShareMem()
{
//
}
int CShareMem::initShareMem(int key, bool isServer)
{
if (-1 == init_shm(key))
{
return -1;
}
if (-1 == load_shm(m_shmID, (void **)&m_shareMem))
{
return -1;
}
// 读信号量
if (-1 == init_sem(key + 1, &m_readSemID))
{
return -1;
}
if (isServer)
{
if (-1 == init_sem_value(m_readSemID, 1))
{
return -1;
}
}
return 0;
}
int CShareMem::initData()
{
memset(m_shareMem, 0, sizeof(My_MEM));
return 0;
}
int CShareMem::updateGetData(My_MEM *pMemData)
{
if (pMemData == NULL)
{
return -1;
}
if (-1 == sem_p(m_readSemID))
{
return -1;
}
m_shareMem->db3++;
memcpy(pMemData, m_shareMem, sizeof(My_MEM));
if (-1 == sem_v(m_readSemID))
{
return -1;
}
return 0;
}
int CShareMem::init_sem(int sem_key, int *sem_id)
{
*sem_id = semget((key_t)sem_key, 1, 0666);
if (*sem_id == -1)
{
*sem_id = semget((key_t)sem_key, 1, 0666 | IPC_CREAT);
if (*sem_id == -1)
{
cout << "信号量semget 获取失败" << endl;
return -1;
}
}
cout << "信号量 semget success." << endl;
return 0;
}
// 从系统中删除信号量
int CShareMem::del_sem(int sem_id)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
{
cout << "信号量 semctl 删除失败" << endl;
return -1;
}
cout << "信号量 semctl 删除成功" << endl;
return 0;
}
// 信号量设置初始值
int CShareMem::init_sem_value(int sem_id, int init_value)
{
union semun sem_union;
sem_union.value = init_value; // 设置初始值
if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
{
cout << "信号量 initialize sem value fail." << endl;
return -1;
}
else
{
cout << "信号量 initialize sem value success." << endl;
}
return 0;
}
/* P 操作函数 */
int CShareMem::sem_p(int sem_id)
{
struct sembuf sops;
sops.sem_num = 0; /* 单个信号量的编号应该为 0 */
sops.sem_op = -1; /* 表示 P 操作 */
sops.sem_flg = SEM_UNDO; /* 若进程退出,系统将还原信号量*/
if (semop(sem_id, &sops, 1) == -1)
{
perror("P operation");
return -1;
}
return 0;
}
/* V 操作函数*/
int CShareMem::sem_v(int sem_id)
{
struct sembuf sops;
sops.sem_num = 0; /* 单个信号量的编号应该为 0 */
sops.sem_op = 1; /* 表示 V 操作 */
sops.sem_flg = SEM_UNDO; /* 若进程退出,系统将还原信号量*/
if (semop(sem_id, &sops, 1) == -1)
{
perror("V operation");
return -1;
}
return 0;
}
// 初始化共享内存
int CShareMem::init_shm(int shm_key)
{
m_shmID = shmget((key_t)shm_key, sizeof(My_MEM), 0666);
if (m_shmID == -1)
{
m_shmID = shmget((key_t)shm_key, sizeof(My_MEM), IPC_CREAT | 0666);
if (m_shmID == -1)
{
cout << "shmget error." << endl;
return -1;
}
}
cout << "shmget success." << endl;
return 0;
}
// 删除共享内存
int CShareMem::del_shm(int shm_id)
{
int ret = shmctl(shm_id, IPC_RMID, NULL);
if (ret == -1)
{
cout << "shmctl 删除失败" << endl;
return -1;
}
cout << "shmctl 删除成功" << endl;
return 0;
}
// 挂载共享内存
int CShareMem::load_shm(int shm_id, void **mem)
{
*mem = shmat(shm_id, NULL, 0);
if (*mem == (void *)-1)
{
cerr << "shmat 挂载失败" << endl;
return -1;
}
cout << "shmat 挂载成功" << endl;
return 0;
}
// 分离共享内存
int CShareMem::unload_shm(int shm_id, void **mem)
{
int ret = shmdt(*mem);
if (ret == -1)
{
cout << "shmdt 分离失败" << endl;
return -1;
}
cout << "shmdt 分离成功" << endl;
return 0;
}