-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_shmringbuffer.cc
62 lines (56 loc) · 1.71 KB
/
test_shmringbuffer.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include "shmringbuffer.hh"
struct LogNode {
int ts; // timestamp
int len; // length
#define MAX_LOG_LEN 256
char log[MAX_LOG_LEN];
const std::string unparse() {
return "[" + std::to_string(ts) + "] " + std::string(&log[0]);
}
};
int main()
{
/* initialize random seed: */
srand (time(NULL));
const int CAPACITY = 20;
pid_t pid = fork();
if (pid == 0) {
// child process
usleep(500);
ShmRingBuffer<LogNode> buffer(CAPACITY, false);
int start = 1000;
LogNode log;
for (int i = start; i < start + 10*CAPACITY; ++i) {
snprintf(log.log, MAX_LOG_LEN, "%zu: %d", buffer.end(), i);
buffer.push_back(log);
std::cout << "child: insert " << i << ", index " << buffer.end() << std::endl; // FIXME
usleep(rand()%1000+500);
}
exit(0);
} else if (pid > 0) {
// parent process
ShmRingBuffer<LogNode> buffer(CAPACITY, true);
int start = 2000;
LogNode log;
for (int i = start; i < start + 10*CAPACITY; ++i) {
snprintf(log.log, MAX_LOG_LEN, "%zu: %d", buffer.end(), i);
buffer.push_back(log);
std::cout << "parent: insert " << i << ", index " << buffer.end() << std::endl; // FIXME
usleep(rand()%900+500);
}
usleep(5000); // wait for child process exit
std::cout << "Ring Buffer:" << std::endl;
std::cout << buffer.unparse() << std::endl;
} else {
// fork failed
std::cout << "fork() failed." << std::endl;
return 1;
}
return 0;
}