0

I have a question about Monit.

i've wrote a multithread C++ process. The main and some of the thread that it invoke, ar critical for my application and needs to live for indefinite time.

include <thread.h>
.......
std::thread Thread_1( );
Thread_1.detach();  
std::thread Thread_2();
Thread_2.detach(); 
std::thread Thread_3();
Thread_3.detach(); 

How can I create a .pid file in order to configuring Monit?

Do i need to supervise all thread PID separately or it is sufficient to monitor only the main? if I type in my shell

pidof main

I can get the PID of main (not the pid of the other threads), but it changes after reboot

Many thanks for your patience

user3450036
  • 85
  • 1
  • 2
  • 10

1 Answers1

0

Monit the main process and let the main process monitor the threads. (easiest way)

You can use pidfile (man 3 pidfile) to manage pidfiles under Linux/BSD. You can use getpid() to get the PID of the current process and write it into a file. Monitor this file with Monit. You can implement watchdog mechanisms for your threads and/or use signal-handlers (inside your program).

A watchdog mechanism can be a variable incremented by the thread in a given intervall. The main process checks if the variable was incremented in that interval or not. (requires locking, possible deadlock, if thread hangs while incrementing).

If you want to check, if a std::thread is running, check out the answer here: How to check if a std::thread is still running?

Community
  • 1
  • 1
gj13
  • 1,314
  • 9
  • 23
  • Thank you for toyr reply. So I: 1) use cron job to start my c++ Process at boot time 2) at the begin of my c++ code, I write the result of getpid(); in main.pid file 3) I configure Monit to work with main.pid is it correct? But I haven't understood yet how the main process can monitor the threads after Thread_x.detach(). Is the first time i need to use multithreading, so if you can suggest me the better way between watchdog or signal-handlers I'll study the mechanism – user3450036 Dec 21 '14 at 22:29