Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you flush a write using a file descriptor?

Tags:

c

linux

stdio

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.

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.

Aside: The nature of I2C is that each byte read or written by the master is acknowledged by the device on the other end of the wire (slave) - the pauses improving things encourage me to think of the driver as working asynchronously - something that I can't reconcile with how the bus works. Anyhoo ...

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.

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); 

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,...); 

Suggestions?

like image 681
Jamie Avatar asked Nov 03 '08 17:11

Jamie


People also ask

How do you flush a file in Python?

Python file method flush() flushes the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects. Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.

What does it mean to flush a file?

file. flush forces the data to be written out at that moment. This is hand when you know that it might be a while before you have more data to write out, but you want other processes to be able to view the data you've already written.

What is the use of flush () method in python?

The flush() method in Python file handling clears the internal buffer of the file. In Python, files are automatically flushed while closing them. However, a programmer can flush a file before closing it by using the flush() method.

Which method is used to clear the buffer and write contents in buffer to the file this is how programmers can forcefully write to the files as when required?

Python File flush() Method.


1 Answers

I think what you are looking for may be

int fsync(int fd); 

or

int fdatasync(int fd); 

fsync will flush the file from kernel buffer to the disk. fdatasync will also do except for the meta data.

like image 116
Danke Xie Avatar answered Sep 22 '22 19:09

Danke Xie