用于I/O的触发信号处理程序 [英] Triggering Signal Handler For I/O

查看:78
本文介绍了用于I/O的触发信号处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上使用C,每次使用write()函数将数据写入缓冲区时,如何触发信号处理程序.处理程序将在执行时读取所有写入缓冲区的数据.

Using C on Linux, how would I go about triggering a signal handler every time I write data to a buffer using the write() function. The handler will be reading all data written to the buffer at the time of execution.

推荐答案

套接字通过在套接字文件描述符上启用异步模式来支持此功能.在Linux上,这是使用fcntl调用完成的:

Sockets support this by enabling async mode on the socket file descriptor. On Linux this is done using fcntl calls:

/* set socket owner (the process that will receive signals) */
fcntl(fd, F_SETOWN, getpid());

/* optional if you want to receive a real-time signal instead of SIGIO */
fnctl(fd, F_SETSIG, signum);

/* turn on async mode -- this is the important part which enables signal delivery */
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_ASYNC);

这篇关于用于I/O的触发信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆