你怎么能刷新使用文件描述符写? [英] How can you flush a write using a file descriptor?

查看:312
本文介绍了你怎么能刷新使用文件描述符写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来开()与fopen()函数从在Linux 2.6.14内核在手腕上的越野车I2C驱动茎的这整个误解。向后移植工作有点殴打司机解决了,我想在这里解决问题的根本原因。

It turns out this whole misunderstanding of the open() versus fopen() stems from a buggy I2C driver in the Linux 2.6.14 kernel on an ARM. Backporting a working bit bashed driver solved the root cause of the problem I was trying to address here.

我想弄清楚的一个问题,在Linux操作系统(I2C)串行设备驱动程序。看来,通过增加写入之间定时OS暂停(睡),并读取工作的事情在设备上......(多)更好。

I'm trying to figure out an issue with a serial device driver in Linux (I2C). It appears that by adding timed OS pauses (sleeps) between writes and reads on the device things work ... (much) better.

旁白:的 I2C的本质是由主机读取或写入每个字节由设备承认在电线​​上(从)的另一端 - 改进的东西停顿鼓励我想到司机作为异步工作 - 这是我不能用公交车是如何工作的协调。安美居...

我要么喜欢为刷新写可以肯定(而不是使用固定的工期暂停), 不知何故测试该读/写交易都有完成在多线程友好的方式即可。

I'd either like to flush the write to be sure (rather than using fixed duration pause), or somehow test that the write/read transaction has completed in an multi-threaded friendly way.

使用麻烦的 fflush(FD); 是,它需要的fd是流指针(不是文件描述符)即

The trouble with using fflush(fd); is that it requires 'fd' to be stream pointer (not a file descriptor) i.e.

FILE * fd = fopen("filename","r+");
... // do read and writes
fflush(fd);

我的问题是我需要使用的ioctl(),它不使用流指针。即。

My problem is that I require the use of the ioctl(), which doesn't use a stream pointer. i.e.

int fd = open("filename",O_RDWR);
ioctl(fd,...);

建议?

推荐答案

您有两种选择:


  1. 使用的fileno()来获得与 STDIO 流指针有关的文件描述符

  1. Use fileno() to obtain the file descriptor associated with the stdio stream pointer

不要使用<&stdio.h中GT; 可言,这样你就不需要担心冲洗要么 - 所有的写操作会去该装置立即和字符设备的的write()通话甚至不会返回,直到下层IO已完成(理论上)。

Don't use <stdio.h> at all, that way you don't need to worry about flush either - all writes will go to the device immediately, and for character devices the write() call won't even return until the lower-level IO has completed (in theory).

有关设备级的IO我会说这是pretty不寻常的使用 STDIO 。我强烈建议使用较低级别的的open()阅读()写()函数,而不是(根据你以后的回复):

For device-level IO I'd say it's pretty unusual to use stdio. I'd strongly recommend using the lower-level open(), read() and write() functions instead (based on your later reply):

int fd = open("/dev/i2c", O_RDWR);
ioctl(fd, IOCTL_COMMAND, args);
write(fd, buf, length);

这篇关于你怎么能刷新使用文件描述符写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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