-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_test.cpp
71 lines (59 loc) · 1.6 KB
/
pipe_test.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
#include "Loop.h"
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <functional>
using namespace std;
using namespace std::placeholders;
/* int p1[2], p2[2], p3[2]; */
int p[3][2];
Loop loop;
int counter = 0;
void print_events(int i, uint32_t events) {
cout << "events for " << p[i][0] << ": ";
if (events & EPOLLIN) cout << "EPOLLIN ";
if (events & EPOLLOUT) cout << "EPOLLOUT ";
if (events & EPOLLRDHUP) cout << "EPOLLRDHUP ";
if (events & EPOLLPRI) cout << "EPOLLPRI ";
if (events & EPOLLERR) cout << "EPOLLERR ";
if (events & EPOLLHUP) cout << "EPOLLHUP ";
cout << endl;
counter++;
close(p[i][1]);
if (counter > 10) exit(0);
}
void del_other_pipes(int index, uint32_t events) {
for (int i = 0; i <= 2; i++) {
if (i != index) {
loop.del_fd(p[i][0]);
close(p[i][0]);
}
}
int val;
if ((std::size_t) read(p[index][0], &val, sizeof(val)) < sizeof(val)) {
cout << "Short read" << endl;
exit(1);
}
cout << "Pipe: " << index << ", val: " << val << endl;
if (val == 0) loop.exit_loop();
}
int main() {
//Run a simple test with some pipes
pipe(p[0]);
pipe(p[1]);
pipe(p[2]);
for (int i = 0; i <= 2; i++) {
loop.add_fd(p[i][0], EPOLLIN, bind(print_events, i, _1));
}
int val = 1;
for (int i = 0; i <= 2; i++) {
write(p[i][1], &val, sizeof(val));
//close(p[i][1]);
}
/* val = 0; */
/* for (int i = 0; i <= 2; i++) { */
/* write(p[i][1], &val, sizeof(val)); */
/* } */
loop.handle_events();
return 0;
}