简体   繁体   English

Linux套接字编程:通过SIGIO读取数据

[英]Linux socket programming: read data by SIGIO

I coded a program which is reading and writing to some gpio pins and receiving some data through a socket by using datagram connection. 我编写了一个程序,该程序可以读写一些gpio引脚,并使用数据报连接通过套接字接收一些数据。 I wanted to try to read data from socket when a SIGIO signal is received. 我想尝试在收到SIGIO信号时从套接字读取数据。 But when I attempted to do that, reading / writing operations on gpio pins started to get failed. 但是,当我尝试这样做时,对gpio引脚的读/写操作开始失败。 Why is it happening? 为什么会这样呢?

If I do not activate gpio read/write operations to throw SIGIO signal, I am not supposed to receive SIGIO signal because of read/write operations on gpio pins. 如果我不激活gpio读/写操作以抛出SIGIO信号,则由于gpio引脚上的读/写操作,我不应该接收SIGIO信号。 Am I wrong? 我错了吗? Or do all IO operations start to get directed to my signal handler? 还是所有IO操作都开始定向到我的信号处理程序?

Here is a piece of my code where I activated SIGIO signal for socket. 这是我的一部分代码,其中我为套接字激活了SIGIO信号。

int fd = socket( AF_INET, SOCK_DGRAM, 0 );
...
struct sigaction sa;
memset( &sa, 0, sizeof(struct sigaction) );
sigemptyset( &sa.sa_mask );
sa.sa_sigaction = SignalHandler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction( SIGIO, &sa, NULL );
fcntl( fd, F_SETOWN, getpid() );
fcntl( fd, F_SETSIG, SIGIO );
fcntl( fd, F_SETFL, O_NONBLOCK | O_ASYNC );

Thanks. 谢谢。

Note: Code is very huge and it is written in OOP concept. 注意:代码非常庞大,并且是以OOP概念编写的。 It does not seem possible to copy all code to here. 似乎不可能将所有代码都复制到此处。 I just copied code related with my problem here. 我只是在这里复制了与我的问题有关的代码。 However, I will create an MCVE and copy here in a moment. 但是,我将创建一个MCVE并稍后在此处复制。 Also, I am trying to understand the signal concept of linux. 另外,我试图理解linux的信号概念。 Because it implies that I have a lack of understanding of signal concept or SIGIO behaviour. 因为这意味着我对信号概念或SIGIO行为缺乏了解。

SIGIO is one big bucket that can be caused from multiple different sources at the same time. SIGIO是一个大桶,可以同时从多个不同来源引起。

Using select()/poll() in main loop is a better approach than to rely SIGIO 在主循环中使用select()/ poll()是比依赖SIGIO更好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM