Belle II Software development
Fork Class Reference

Public Member Functions

template<class WORKER >
 Fork (WORKER *worker, bool detached=true)
 
pid_t get_id () const
 
void set_id (pid_t id)
 
bool isAlive () const
 
bool kill (int signo) const
 
bool wait (int opt=0)
 
bool cancel ()
 

Static Public Member Functions

template<class WORKER >
static void g_handler_exit (int, void *worker)
 
static void g_handler_int (int)
 

Private Attributes

pid_t m_pid
 

Detailed Description

Definition at line 22 of file Fork.h.

Constructor & Destructor Documentation

◆ Fork() [1/2]

Fork ( )
inline

Definition at line 34 of file Fork.h.

34: m_pid(-1) {}

◆ Fork() [2/2]

Fork ( WORKER *  worker,
bool  detached = true 
)
inline

Definition at line 37 of file Fork.h.

38 {
39 m_pid = fork();
40 if (m_pid == 0) {
41 signal(SIGINT, g_handler_int);
42 if (detached) {
43 on_exit(g_handler_exit<WORKER>, (void*)worker);
44 }
45 worker->run();
46 exit(0);
47 } else if (detached) {
48 delete worker;
49 }
50 }

◆ ~Fork()

~Fork ( )
inline

Definition at line 51 of file Fork.h.

51{}

Member Function Documentation

◆ cancel()

bool cancel ( )
inline

Definition at line 70 of file Fork.h.

70{ return kill(SIGINT); }

◆ g_handler_exit()

static void g_handler_exit ( int  ,
void *  worker 
)
inlinestatic

Definition at line 26 of file Fork.h.

27 {
28 delete(WORKER*)worker;
29 exit(0);
30 }

◆ g_handler_int()

static void g_handler_int ( int  )
inlinestatic

Definition at line 31 of file Fork.h.

31{ exit(0); }

◆ get_id()

pid_t get_id ( ) const
inline

Definition at line 54 of file Fork.h.

54{ return m_pid; }

◆ isAlive()

bool isAlive ( ) const
inline

Definition at line 56 of file Fork.h.

56{ return kill(0); }

◆ kill()

bool kill ( int  signo) const
inline

Definition at line 57 of file Fork.h.

58 {
59 if (m_pid < 0) return false;
60 return ::kill(m_pid, signo) == 0;
61 }

◆ set_id()

void set_id ( pid_t  id)
inline

Definition at line 55 of file Fork.h.

55{ m_pid = id; }

◆ wait()

bool wait ( int  opt = 0)
inline

Definition at line 62 of file Fork.h.

63 {
64 if (m_pid < 0) return false;
65 if (::waitpid(m_pid, NULL, opt)) {
66 m_pid = -1;
67 }
68 return true;
69 }

Member Data Documentation

◆ m_pid

pid_t m_pid
private

Definition at line 73 of file Fork.h.


The documentation for this class was generated from the following file: