统一事件源

版权声明:转载请注明出处. https://blog.csdn.net/laodaliubei/article/details/85266716

Contents

  • 信号是一种异步事件,信号处理函数和程序的主循环是两条不同的执行路线.
  • 信号处理期间,系统不会再次触发它,hence, 信号处理函数需要尽可能快地执行完毕,避免该信号遭到屏蔽太久.
    一种典型的方案是: 将信号处理的主要逻辑放在程序主循环当中,当信号处理函数被触发时,只是简单地通知主循环"信号来了,信号值是XXX,请注意查收"; 主循环根据信号值执行目标信号所对应的代码.
    Generally, Use “pipe”. 信号处理函数-> write sighal value into pipe -> 主循环read it .
    However, 主循环怎么知道什么时候该read 呢? polling 不靠谱, We use I/O 复用系统调用来监听管道的读端描述符上的可读事件. 这就是所谓的"统一事件源"
  • pipe创建和使用
  • #include <unistd.h> int pipe(int pipefd[2]);
  • 该函数如果调用成功,数组中将包含两个新的文件描述符—fd[0](read) 以及fd[1](write)来描述。
  • 信号是一种处理异步事件的方式,用于通知"接受进程"某个事件的发生; 既可以用于进程间的通信,也可以用于通知进程自身.
  • 在Linux上有signal() 和sigaction() 两种.

信号处理函数的执行期

进入内核态: interruption & System_Call.

  • 很多I/O框架库和后台处理器程序都统一处理信号&I/O event, e.g. Libevent I/O 框架库和xinetd
  • xinetd (Extended Internet Service Daemon) is an open-source super-server daemon, runs on many Unix-like systems and manages Internet-based connectivity.

xinetd offers a more secure alternative to the older inetd (“the Internet daemon”), which most modern Linux distributions have deprecated.

  • Libevent is a software library that provides asynchronous event notification. The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached.
  • libevent is meant to replace the event loop found in event-driven network servers. An application can just call event_dispatch() and then add or remove events dynamically without having to change the event loop
    As a result, libevent allows for portable application development and provides “the most scalable event notification mechanism available on an operating system”.
    Using callbacks on signals, libevent makes it possible to write “secure” signal handlers as none of the user supplied signal handling code runs in the signal’s context.

猜你喜欢

转载自blog.csdn.net/laodaliubei/article/details/85266716
今日推荐