如何阻止 C 语言中的文件描述符溢出
我正在向 88kb 的文件描述符写入一条消息。我的linux系统上的文件描述符只能容纳64kb。一旦数据被放到文件描述符上,它就会被读取并通过管道传送到 TCP 连接中。
我如何知道是否可以向文件描述符写入更多数据,即文件描述符为空?我需要一个函数,该函数会阻塞直到文件描述符为空,或者至少可以返回一个值,让我知道如何将数据安全地写入文件描述符。
I am writing a message to a file descriptor that is 88kb. The file descriptor on my linux system only can hold 64kb. Once the data gets put on the file descriptor it gets read and piped into a tcp connection.
How do i know if I can write more data to the file descriptor, meaning the file descriptor is empty? I need a function that blocks until the file descriptor is empty or at least can return a value letting me know how data I can safely write to the file descriptor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 select() 可能就是你想要的。有了正确的论据,你就可以安排
让它阻塞,直到你的文件描述符准备好进行 I/O 操作。
不过,你的问题措辞有点奇怪,因为我们通常不指
文件描述符具有容量或“空”。听起来也许
您正在谈论代表管道一个端点的文件描述符,
它是管道(不是文件描述符),容量为 64 KB。
I think select() is probably what you want. With the right arguments, you can arrange
for it to block until your file descriptor is ready for your I/O operations.
Your question is worded a little strangely, though, since we don't generally refer to
file descriptors as having a capacity, or being "empty". It sounds like maybe
you're talking about a file descriptor that represents one endpoint of a pipe,
and it's the pipe (not the file descriptor) that has a capacity of 64 KB.
对我来说,听起来更像是OP正在尝试发送大于配置的最大数据包大小的数据报(AF_UNIX或AF_INET/UDP)。其他可能性是管道和 FIFO,它们具有最大原子写入大小;这里的修复方法是进行多次写入,连续的写入将被阻塞,直到管道/FIFO 中有足够的空间。
Sounds to me more like the OP is trying to send a datagram (AF_UNIX or AF_INET/UDP) larger than the configured maximum packet size. Other possibilities are pipes and FIFOs, which have maximum atomic write sizes; the fix here is to do multiple writes, and successive writes will block until there is room for them in the pipe/FIFO.