25 template <
class WORKER>
26 static void g_handler_exit(
int,
void* worker)
28 delete (WORKER*)worker;
31 static void g_handler_int(
int) { exit(0); }
34 Process() : m_pid(-1) {}
36 template<
class WORKER>
37 explicit Process(WORKER* worker,
bool detached =
true) : m_pid(fork())
40 signal(SIGINT, g_handler_int);
42 on_exit(g_handler_exit<WORKER>,
static_cast<void*
>(worker));
46 }
else if (detached) {
53 pid_t get_id()
const {
return m_pid; }
54 pid_t id()
const {
return m_pid; }
55 void set_id(pid_t
id) { m_pid = id; }
56 bool isAlive()
const {
return kill(0); }
57 bool kill(
int signo)
const
59 if (m_pid <= 0)
return false;
60 return ::kill(m_pid, signo) == 0;
62 bool wait(
int opt = 0)
64 if (m_pid < 0)
return false;
65 if ((m_waitpid_result = ::waitpid(m_pid, &m_waitpid_status, opt))) {
70 bool cancel() {
return kill(SIGINT); }
71 int waitpid_result()
const
73 return m_waitpid_result;
75 int waitpid_status()
const
77 return m_waitpid_status;
82 int m_waitpid_result = 0;
83 int m_waitpid_status = 0;