不间断的读/写调用
在 Linux 上的 C 编程冒险过程中的某个时刻,我遇到了一些标志(可能是 ioctl/fcntl?),这些标志使文件描述符上的读写操作不间断。
不幸的是,我不记得如何做到这一点,也不记得我在哪里读过它。任何人都可以透露一些信息吗?
Update0
为了优化我的查询,我追求相同的阻塞并保证 fwrite()
和 fread()
提供,无用户空间缓冲。
At some point during my C programming adventures on Linux, I encountered flags (possibly ioctl
/fcntl
?), that make reads and writes on a file descriptor uninterruptible.
Unfortunately I cannot recall how to do this, or where I read it. Can anyone shed some light?
Update0
To refine my query, I'm after the same blocking and guarantees that fwrite()
and fread()
provide, sans userspace buffering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过确保所有信号处理程序都使用
SA_RESTART
安装来避免来自read()
和write()
的EINTR
>sigaction()
的标志。然而,这并不能保护您免受短读/写的影响。这只能通过将
read()
/write()
放入循环中来实现(除了必须已提供给的缓冲区之外,它不需要额外的缓冲区) code>read()
/write()
调用。)这样的循环如下所示:
You can avoid
EINTR
fromread()
andwrite()
by ensuring all your signal handlers are installed with theSA_RESTART
flag ofsigaction()
.However this does not protect you from short reads / writes. This is only possible by putting the
read()
/write()
into a loop (it does not require an additional buffer beyond the one that must already be supplied to theread()
/write()
call.)Such a loop would look like:
您是否希望在读/写时禁用中断,或者保证在您读/写文件时没有其他人会读/写该文件?
对于第二个,您可以使用
fcntl()
的F_GETLK、F_SETLK和F_SETLKW分别获取、释放和测试记录锁。然而,由于 POSIX 锁只是建议性的,Linux 并不强制执行它们——它只在协作进程之间有意义。第一个任务涉及深入研究零环并禁用本地处理器上的中断(或者全部,如果您使用的是 SMP 系统)。完成后请记住再次启用它们!
Do you wish to disable interrupts while reading/writing, or guarantee that nobody else will read/write the file while you are?
For the second, you can use
fcntl()
's F_GETLK, F_SETLK and F_SETLKW to acquire, release and test for record locks respectively. However, since POSIX locks are only advisory, Linux does not enforce them - it's only meaningful between cooperating processes.The first task involves diving into ring zero and disabling interrupts on your local processor (or all, if you're on an SMP system). Remember to enable them again when you're done!