简体   繁体   中英

Supervising C++ multithread process using Monit

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM