forked from detailyang/ipc_benchmark
/
shm.c
217 lines (196 loc) · 4.68 KB
/
shm.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/time.h>
typedef union {
int val;
struct semid_ds *buf;
unsigned short *array ;
#ifndef __VMS
struct seminfo *__buf;
void *__pad ;
#endif
} semun;
void sem_init(int sem_id, int sem_num, int init_valve)
{
semun sem_union;
sem_union.val = init_valve;
if (semctl(sem_id, sem_num, SETVAL, sem_union))
{
perror("semctl");
exit(-1);
}
}
void sem_release(int sem_id, int sem_num)
{
struct sembuf sem_b;
sem_b.sem_num = sem_num;
sem_b.sem_op = 1;
sem_b.sem_flg = 0;
if (semop(sem_id, &sem_b, 1) == -1)
{
perror("sem_release");
exit(-1);
}
}
void sem_reserve(int sem_id, int sem_num)
{
struct sembuf sem_b;
sem_b.sem_num = sem_num;
sem_b.sem_op = -1;
sem_b.sem_flg = 0;
if (semop(sem_id, &sem_b, 1) == -1)
{
perror("sem_reserve");
exit(-1);
}
}
double getdetlatimeofday(struct timeval *begin, struct timeval *end)
{
return (end->tv_sec + end->tv_usec * 1.0 / 1000000) -
(begin->tv_sec + begin->tv_usec * 1.0 / 1000000);
}
int main(int argc, char *argv[])
{
#define SHM_KEY 0x1234
#define SEM_KEY 0x5678
#define WRITE_SEM 0
#define READ_SEM 1
pid_t pid;
int sem_id;
int shm_id;
long long count, i, size;
struct timeval begin, end;
#ifdef __VMS
static char* child_flag = "----";
int is_child = 0;
if (argc == 4 && !strcmp(argv[3], child_flag)) {
argc--;
is_child = 1;
}
#endif
if (argc != 3)
{
printf("usage: ./shm <size> <count>\n");
return 1;
}
size = atoi(argv[1]);
count = atoi(argv[2]);
unsigned char *buf = malloc(size);
#ifdef __VMS
if (is_child)
#else
pid = fork();
if (pid == -1)
{
perror("fork");
return -1;
}
else if (pid == 0) // child
#endif
{
sem_id = semget(SEM_KEY, 2, 0600 | IPC_CREAT);
if (sem_id == -1)
{
perror("child: semget");
return -1;
}
sem_init(sem_id, WRITE_SEM, 1);
sem_init(sem_id, READ_SEM, 0);
shm_id = shmget(SHM_KEY, size, IPC_CREAT | 0600);
if (shm_id == -1)
{
perror("child: shmget");
return -1;
}
void *addr = shmat(shm_id, NULL, 0);
if (addr == (void *)-1)
{
perror("child: shmat");
return -1;
}
for (i = 0; i < count; i++)
{
sem_reserve(sem_id, READ_SEM);
memcpy(buf, addr, size);
// printf(">>>>>>>>%d\n", *(int*)buf);
sem_release(sem_id, WRITE_SEM);
}
if (shmdt(addr) == -1)
{
perror("child: shmdt");
return -1;
}
}
else // parent
{
#ifdef __VMS
char *argv_copy[5];
int sleep_time = 5;
if (vfork() == 0) {
/* exec myself with child_flag as the 3rd argument */
for (i = 0; i < 3; i++) {
argv_copy[i] = argv[i];
}
argv_copy[3] = child_flag;
argv_copy[4] = NULL;
if (execvp(argv[0], argv_copy)) {
perror("execvp");
return 1;
}
}
/* continue as the parent */
while (sleep_time > 0) {
sleep_time = sleep(sleep_time);
}
#else
sleep(1);
#endif
sem_id = semget(SEM_KEY, 0, 0);
if (sem_id == -1)
{
perror("parent: semget");
return -1;
}
shm_id = shmget(SHM_KEY, 0, 0);
if (shm_id == -1)
{
perror("parent: shmget");
return -1;
}
void *addr = (int *)shmat(shm_id, NULL, 0);
if (addr == (void *)-1)
{
perror("parent: shmat");
return -1;
}
gettimeofday(&begin, NULL);
for (i = 0; i < count; i++)
{
sem_reserve(sem_id, WRITE_SEM);
// *(int*)buf = i;
memcpy(addr, buf, size);
sem_release(sem_id, READ_SEM);
}
gettimeofday(&end, NULL);
double tm = getdetlatimeofday(&begin, &end);
printf("%.0fMB/s %.0fmsg/s\n",
count * size * 1.0 / (tm * 1024 * 1024),
count * 1.0 / tm);
sem_reserve(sem_id, WRITE_SEM);
semun dummy = {0};
semctl(sem_id, 0, IPC_RMID, dummy);
if (shmdt(addr) == -1)
{
perror("parent: shmdt");
return -1;
}
shmctl(shm_id, IPC_RMID, 0);
}
return 0;
}