Read from a file in C while it is written externally

I would like to write a small C program that reads from a file while it is actively writing. Any ideas?

+4
source share
2 answers

If you have control over the writing process, you should use mmap() with MAP_SHARED for both reading and writing. Thus, the reader will see the changes made by the author almost immediately.

Also note that when you open a file, Linux does not take snapshots of the data in the file, so you should see the changes that are made to the file, even if you just use read() and lseek() .

To determine if a file has been modified / opened / accessible / on Linux, you can use the inotify API (see inotify manpage ). This allows you to make the process wait for an event that interests you until it happens (as opposed to regular polling). You can also use epoll() or the more traditional select() to achieve a similar result.

+4
source

I think tail -f is exactly what you want, right? Take a look at the source code: http://www.gnu.org/s/coreutils/

Or this one (not sure if it is updated): http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c

+2
source

Source: https://habr.com/ru/post/1379989/


All Articles