-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockpair.cpp
More file actions
38 lines (35 loc) · 998 Bytes
/
sockpair.cpp
File metadata and controls
38 lines (35 loc) · 998 Bytes
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
#include <iostream>
#include <sys/socket.h>
#include <unistd.h>
using namespace std;
int main() {
int fd[2];
// socketpair用于父子进程或线程之间通信
socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
pid_t pid = fork();
if(pid == 0) {
// subprocess
int val = 0;
close(fd[0]);
while(true) {
sleep(1);
cout << endl;
cout << "subprocess sending data: " << val << endl;
write(fd[1], &val, sizeof(val));
read(fd[1], &val, sizeof(val));
cout << "subprocess receive data: " << val << endl;
}
}else {
// parent process
close(fd[1]);
int val;
while(true) {
read(fd[0], &val, sizeof(val));
cout << "parent process receive data: " << val << endl;
val++;
write(fd[0], &val, sizeof(val));
cout << "parent process sending data: " << val << endl;
}
}
return 0;
}