This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.hpp
208 lines (163 loc) · 5.92 KB
/
process.hpp
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
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013-2017 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#ifndef PGM_PROCESS_HPP
#define PGM_PROCESS_HPP
////////////////////////////////////////////////////////////////////////////////
#include <chrono>
#include <csignal>
#include <fstream>
#include <functional>
#include <memory>
#include <ostream>
#include <thread>
#include <utility>
////////////////////////////////////////////////////////////////////////////////
namespace pgm
{
////////////////////////////////////////////////////////////////////////////////
class ofilebuf;
class ifilebuf;
enum state
{
not_started,
running,
stopped,
exited,
signaled,
};
////////////////////////////////////////////////////////////////////////////////
// Creates and manages child process.
//
// Modeled after std::thread.
// Provides C++-style streams (cin, cout, cerr) attached
// to the process' stdin, stdout and stderr streams.
//
class process
{
public:
////////////////////
using native_handle_type = pid_t;
// process id
struct id
{
////////////////////
id() noexcept = default;
explicit id(native_handle_type h) noexcept : handle_(h) { }
private:
////////////////////
native_handle_type handle_ { };
friend class process;
friend bool operator==(id x, id y) noexcept { return x.handle_==y.handle_; }
friend bool operator< (id x, id y) noexcept { return x.handle_< y.handle_; }
template<typename CharT, typename Traits>
friend std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, process::id id)
{ return os << id.handle_; }
friend struct std::hash<process::id>;
};
////////////////////
process();
process(const process&) = delete;
process(process&&) noexcept;
template<typename Fn, typename... Args>
explicit process(Fn&&, Args&&...);
~process() noexcept;
process& operator=(const process&) = delete;
process& operator=(process&&) noexcept;
void swap(process&) noexcept;
////////////////////
bool joinable() const noexcept { return !(id_ == id()); }
explicit operator bool() const noexcept { return joinable(); }
// get process id
id get_id() const noexcept { return id_; }
// get pid
auto native_handle() const noexcept { return id_.handle_; }
// get process state, exit code & signal
pgm::state state();
int code() const noexcept { return code_; }
int signal() const noexcept { return signal_; }
// detach process
void detach() noexcept;
// wait for process to finish execution
void join();
template<typename Rep, typename Period>
bool try_join_for(const std::chrono::duration<Rep, Period>&);
template<typename Clock, typename Duration>
bool try_join_until(const std::chrono::time_point<Clock, Duration>&);
////////////////////
// send signal to process
void raise(int);
void terminate() { raise(SIGTERM); }
void kill() { raise(SIGKILL); }
////////////////////
std::ofstream cin;
std::ifstream cout, cerr;
private:
////////////////////
process(std::function<int()>&&);
////////////////////
id id_;
enum state state_ = not_started;
int code_ = -1;
int signal_ = -1;
void update(int status);
using nsec = std::chrono::nanoseconds;
bool try_join_for_(const nsec&);
////////////////////
std::unique_ptr<ofilebuf> fbi_;
std::unique_ptr<ifilebuf> fbo_, fbe_;
};
////////////////////////////////////////////////////////////////////////////////
inline bool operator!=(process::id x, process::id y) noexcept { return !(x==y); }
inline bool operator> (process::id x, process::id y) noexcept { return (y< x); }
inline bool operator<=(process::id x, process::id y) noexcept { return !(y< x); }
inline bool operator>=(process::id x, process::id y) noexcept { return !(x< y); }
////////////////////////////////////////////////////////////////////////////////
template<typename Fn, typename... Args>
process::process(Fn&& fn, Args&&... args) :
process(std::function<int()>(
std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...))
)
{ }
////////////////////////////////////////////////////////////////////////////////
template<typename Rep, typename Period>
inline bool
process::try_join_for(const std::chrono::duration<Rep, Period>& time)
{ return try_join_for_(std::chrono::duration_cast<nsec>(time)); }
////////////////////////////////////////////////////////////////////////////////
template<typename Clock, typename Duration>
bool process::try_join_until(const std::chrono::time_point<Clock, Duration>& tp)
{
auto now = Clock::now();
return try_join_for(tp - (tp < now ? tp : now));
}
////////////////////////////////////////////////////////////////////////////////
inline void swap(process& lhs, process& rhs) noexcept { lhs.swap(rhs); }
////////////////////////////////////////////////////////////////////////////////
namespace this_process
{
////////////////////////////////////////////////////////////////////////////////
process::id get_id() noexcept;
process::id parent_id() noexcept;
using std::this_thread::sleep_for;
using std::this_thread::sleep_until;
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
namespace std
{
template<>
struct hash<pgm::process::id>
{
auto operator()(pgm::process::id id) const noexcept
{ return hash<decltype(id.handle_)>{}(id.handle_); }
};
}
////////////////////////////////////////////////////////////////////////////////
#endif