本文主要是介绍Linux alarm signal (SIGALRM) to detach process isAlive,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题记
最近做项目遇到的问题,程序跑了多个process,每个process都是相互独立的,为了解耦,类似于微服务的架构,我们要求系统可以detach 到 主线程跑飞,死循环等其他bug 问题,最初的设计方案是:每个process 都会给每一个monitor的process 去发送keep alive 消息,由monitor去收集每个module的keep alive消息,然后去判断是否process 跑飞等情况。但是这种方案,由于需要多一个monitor模块,在本来内存有限的嵌入式设备上,有点得不偿失,后来就想能否有linux 系统内部的 实现可以达到我们的要求,也就是 SIGALRM
1. Signal & Semaphore 区别
Signal: 是通过软中断信号通知进程发生了异步事件。进程之间可以通过系统调用kill 发送软中断信号,内核也可以因为内部事件而给进程发送信号,通知进程发生了某个事件。
Semaphore: 信号量是用来操作系统进程间同步访问共享资源。信号量在创建时需要设置一个初始值,表示同时可以有几个任务可以访问该信号量保护的共享资源,初始值为1就变成互斥锁(Mutex),即同时只能有一个任务可以访问信号量保护的共享资源。
2. SIGALRM 以及python code 实现
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
在alarm成功调用后,开始计时,超过该事件将触发SIGALARM信号,然后会调到handler 执行。如下 是python的例子,
import signal,time,sys,thread,tracebackclass Example:def __init__(self):self.handler_counter = 0self.retry_counter = 3passdef timout_handler(self, signum, frame):'''timeout handler when failed to send signal alarmthere is a retry to make sure main thread hung'''self.handler_counter += 1print "call timeout_handler counter: " + str(self.handler_counter)if self.handler_counter == self.retry_counter:print("Have retry %s, exit process", self.retry_counter)traceback.print_stack(frame) # print tracebacksys.exit()def monitor_alive(self, threadName, delay):'''monitor alive to send alarm message every (delay + 1) second, if after (delay + 1) doesn't receive response fromkernel, will interrupt timout_handler'''count = 0while True:time.sleep(delay)signal.alarm(delay + 1)print "sign_time count " + str(count)# below if logic to mock 3 time timeoutif count == 2:time.sleep(delay)if count == 4:time.sleep(delay)if count == 6:time.sleep(delay)count += 1print "%s: %s" % (threadName, time.ctime(time.time()))if __name__ == '__main__':example = Example()# register handler# only could set signal handler in main thread# https://stackoverflow.com/questions/44151888/why-only-main-thread-can-set-signal-handler-in-pythonsignal.signal(signal.SIGALRM, example.timout_handler)thread.start_new_thread(example.monitor_alive, ("Thread-1", 2,))while True:time.sleep(2)print('main thread ')
运行结果:
sign_time count 0
Thread-1: Sat Jun 9 10:52:43 2018
main thread
sign_time count 1
Thread-1: Sat Jun 9 10:52:45 2018
main thread
sign_time count 2
main thread
Thread-1: Sat Jun 9 10:52:49 2018
main thread
call timeout_handler counter: 1
main thread
sign_time count 3
Thread-1: Sat Jun 9 10:52:51 2018
main thread
sign_time count 4
main thread
Thread-1: Sat Jun 9 10:52:55 2018
main thread
call timeout_handler counter: 2
main thread
sign_time count 5
Thread-1: Sat Jun 9 10:52:57 2018
main thread
sign_time count 6
main thread
Thread-1: Sat Jun 9 10:53:01 2018
main thread
call timeout_handler counter: 3
('Have retry %s, exit process', 3)File "/home/odl/sereno/tests/singal.py", line 52, in <module>time.sleep(2)
这篇关于Linux alarm signal (SIGALRM) to detach process isAlive的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!