简体   繁体   中英

Linux: something between FIFO file and device driver

I have one linux daemon, that is generating some data. From time to time that daemon is updating that data. For simplicity lets say that we have just one byte. Now I want other processes to have access to that data and the easiest way (at least for me) to do that is to have some kind of file mediator.

The problem is that I want to read the data asynchronous from multiple processes (or none) and the daemon to update the content of that file.

Is possible to have one file, opened from one process for writing and from one or more processes for reading? And is there some special file type, that is made for that purpose?

pp I was reading about FIFO files and device drivers, but I am not sure, that should use them.

What? Your requirements are not sufficiently stated.

First off, what prevents your readers from seeing data as you update it?

One possible thing to employ is advisory file locking (which support shared and exclusive modes). Read up about flock.

The other standard solution is to create a new file and then rename(2) it it to the old one. The operation is atomic.

Readers who managed to open the old file of course will read your old data, but you have not provided any synchronization so it was inherently racy anyway. (And in fact is likely sufficiently racy even with locks.) Reading processes open + read + close the file each time they want to get new data.

One can also have a hack where the file is fstated to see the link count on the inode. If it is 0, you know the file is removed and there is an update. A problem arises if a joker hardlinks the old file as that will make you miss updates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM