统一事件源

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值