-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathprocess.cpp
48 lines (40 loc) · 1.18 KB
/
process.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
#include "test_process.h"
using swoole::test::Process;
using swoole::UnixSocket;
Process::Process(std::function<void(Process *)> fn, int pipe_type) : handler(fn) {
if (pipe_type > 0) {
auto pipe = new UnixSocket(true, SOCK_DGRAM);
worker.pipe_master = pipe->get_socket(true);
worker.pipe_worker = pipe->get_socket(false);
worker.pipe_object = pipe;
worker.pipe_current = worker.pipe_master;
}
}
Process::~Process() {
if (worker.pipe_object) {
delete worker.pipe_object;
}
}
pid_t Process::start() {
// std::system("ls /proc/self/task");
pid_t pid = swoole_fork(0);
if (pid < 0) {
printf("[Worker] Fatal Error: fork() failed");
exit(1);
} else if (pid == 0) {
worker.child_process = 1;
worker.pipe_current = worker.pipe_worker;
handler(this);
exit(0);
} else {
worker.pid = pid;
worker.child_process = 0;
return pid;
}
}
ssize_t Process::write(const void *__buf, size_t __n) {
return worker.pipe_current->write(__buf, __n);
}
ssize_t Process::read(void *__buf, size_t __nbytes) {
return worker.pipe_current->read(__buf, __nbytes);
}