SIGPOLL (SIGIO) 问题:执行处理程序时中断
我正在实现一个客户端和服务器,它们使用sendto() 和recvfrom() 通过UDP 进行通信。我想在从服务器接收数据时为我的客户端使用 SIGPOLL 。我的问题是,在处理程序中,另一个信号到达并且它丢失了。我读到有一个内核变量(一个标志)被设置,我可以在退出处理程序之前检查它,但我似乎无法找出那是哪个标志。
void my_receive_server_data(int sig_num)
{
/* execute recvfrom() */
}
在main中:
setup_action.sa_handler = my_receive_server_data;
if (sigaction(SIGPOLL, &setup_action, NULL) == -1)
perror("Sigaction");
if (fcntl(sock, F_SETOWN, getpid()) < 0) {
perror("fcntl");
}
if (fcntl(sock, F_SETFL, O_RDONLY | FASYNC) < 0) {
perror("fcntl");
}
目前,如果我不在服务器中的sendto()之后放置sleep(),则只有第一个sendto()会被客户端执行(在处理程序中,作为recvfrom())。使用 sleep() 可以解决问题,所以我高度相信这是因为处理程序只运行一次(因为它在仍在执行 my_receive_server_data 的同时获取数据)。
我想知道在从 my_receive_server_data 返回之前应该检查什么标志,以检查在该处理程序执行时是否有任何输入到达。
非常感谢。
I am implementing a client and server that communicate over UDP using sendto() and recvfrom(). I wanted to use SIGPOLL for my client when receiving data from the server. My issue is that while in the handler, another signal arrives and it gets lost. I've read there is a kernel variable (a flag) that gets set that I could check before exiting the handler, but I can't seem to find out which flag that is.
void my_receive_server_data(int sig_num)
{
/* execute recvfrom() */
}
And in main:
setup_action.sa_handler = my_receive_server_data;
if (sigaction(SIGPOLL, &setup_action, NULL) == -1)
perror("Sigaction");
if (fcntl(sock, F_SETOWN, getpid()) < 0) {
perror("fcntl");
}
if (fcntl(sock, F_SETFL, O_RDONLY | FASYNC) < 0) {
perror("fcntl");
}
Currently, if I do not put a sleep() after sendto() in the server, only the first sendto() gets executed by the client (in the handler, as a recvfrom()). Putting sleep() fixes the problem, so I highly believe it is because the handler only gets run once (because it is getting the data while still executing my_receive_server_data).
I would like to please know what flag I should check before returning from my_receive_server_data to check if any input has arrived while that handler was executing please.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照您尝试的方式创建一个可靠的系统非常困难。学习使用阻塞调用模型以及通过
select()
或poll()
进行 I/O 复用,而不是使用信号。如果您打算无论如何使用信号,请尝试将信号处理程序中的代码减少到最少——也许只是通过管道发送一个字节来唤醒休眠的线程或进程,或者其他什么。
It's very hard to create a solid system the way you're trying to. Instead of signals, learn to use blocking call models, and I/O multiplexing with
select()
orpoll()
.If you intend to use signals no matter what, try to reduce the code in the signal handler to a minimum -- maybe just sending a byte down a pipe to wake up a sleeping thread or process, or whatever.
在为
SIGPOLL
定义处理程序时,SA_NODEFER
标志会传递给 sigaction。The flag is
SA_NODEFER
passed to sigaction when defining a handler forSIGPOLL
.