I’d like to write a small C program that reads from a file while it is actively being written to. Any ideas?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you have control over the writing process you should use
mmap()withMAP_SHAREDin both reader and writer. This way the reader will see the changes done by the writer practically immediately.Also, note that Linux does not make any snapshot of the data in the file when you open the file, so you should see the changes that are being made in the file even if you just use
read()andlseek().In order to determine whether a file was modified/opened/accessed/etc in Linux you can use
inotifyAPI (see inotify manpage). This allows you to make your process wait for an event you’re interested in until it occurs (as opposed to polling it regularly). You can also useepoll()or more traditionalselect()to accomplish similar result.