libev源码分析(三)---ev_signal

ev_signal结构体:

typedef struct ev_signal {

    int active;

    int pending;

    int priority;

    void* data;

    void (*cb)(struct ev_loop *loop, struct ev_signal *w,int revents);

    struct ev_watcher_list *next;

    int signum;//

} ev_signal;

ev_signal当接收到指定的信号时调用触发事件EV_SIGNAL

相关函数:

ev_init(ev,cb_);

ev_signal_set(ev,signum);

//等价于ev_signal_init(ev,cb,signum)

ev_signal_start(EV_P_ ev_signal *w) ;
ev_signal_stop(EV_P_ ev_signal *w) ;

参数说明:

前两个不用再说明了,同ev_io,ev_timer一样,第三个参数是代表触发的信号类型,一般是SIGINT(执行CTRL+C退出应用程序时触发)

 代码如下:

#include 
#include 
#include 
#include 
#include "ev.h"

void signal_action(struct ev_loop *main_loop,ev_signal *signal_w,int e)
{
    puts("In Signal action");
    ev_signal_stop(main_loop,signal_w);
    ev_break(main_loop,EVBREAK_ALL);
}

int main(int argc,char **argv)
{
    ev_signal signal_w;
    struct ev_loop *main_loop = ev_default_loop(0);

    ev_init(&signal_w,signal_action);
    ev_signal_set(&signal_w,SIGINT);

    ev_signal_start(main_loop,&signal_w);

    ev_run(main_loop,0);
    return 0;
}

代码功能:

        应用程序由ctrl+c等结束信号触发时,执行回调函数signal_action(); 

你可能感兴趣的:(嵌入式,网络编程)