检查write()/ send()可以处理整个缓冲区没有块,否则(没有部分写) [英] Check that write()/send() can process whole buffer without block, fail otherwise (no partial write)

查看:245
本文介绍了检查write()/ send()可以处理整个缓冲区没有块,否则(没有部分写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SOCK_SEQPACKET连接,对我来说,确保整个缓冲区通过单一write()/ send()调用发送是至关重要的。我也使用的设备驱动程序设计来处理一个完整的数据块与单个调用。同时,我想处理由于缓冲区溢出而导致的write()/ send()块的情况,即我想要一个反馈,当前实现是否在这里遇到瓶颈。我正在使用glibc,Linux 2.6。

I am using SOCK_SEQPACKET connection, and it is critical for me to ensure that whole buffer is sent with single write()/send() call. I am also operating with device driver that is designed to handle a complete block of data with single call. At the same time I want to handle the situation when write()/send() blocks due to buffer overflow, i.e. I want to have a feedback whether current implementation gets a bottleneck here. I'm working with glibc, Linux 2.6.

我需要实现一个接受缓冲区的方法,它要么完全发送缓冲区,要么表示由于阻塞而失败(例如系统缓冲区溢出)。

I need to implement a method that accepts a buffer, and it either sends a buffer completely or indicates a failure due to blocking (i.e. system buffer overflow).

看起来像使用send(...,MSG_DONTWAIT)/ fcntl(...,O_NONBLOCK)在报告EWOULDBLOCK / EAGAIN之前进行部分写入。是否有一种方法来检查输出缓冲区中是否有足够的空间或者是否有专门的写入完成或失败方法?

It looks like using send(..., MSG_DONTWAIT)/fcntl(..., O_NONBLOCK) is not a solution as they accept partial write prior to reporting EWOULDBLOCK/EAGAIN. Is there a way to check whether there is enough space in outgoing buffer or is there a dedicated write-complete-or-fail method?

或者,可以检测通过其他方式阻止?例如,timer +信号似乎是一个选项,但我不喜欢每次写入都设置它的想法。

Alternatively, is it possible to detect block by other means? For example, timer+signal seems to be an option, but I do not like the idea to set it for each write.

提前感谢。

推荐答案

实验显示我的设置没有发生部分发送(glibc 2.11.1,内核2.6.32-29),EAGAIN / EWOULDBLOCK

Experiment reveals that no partial send occurs on my setup (glibc 2.11.1, kernel 2.6.32-29) and that EAGAIN/EWOULDBLOCK is returned by send(...MSG_DONTWAIT) as expected for AF_UNIX+SOL_SEQPACKET socket.

无法确定行为是否是通用的。

Not sure whether behavior is universal.

这篇关于检查write()/ send()可以处理整个缓冲区没有块,否则(没有部分写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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