LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2016, 08:58 AM   #1
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Rep: Reputation: Disabled
Most efficient method to `monitor' for additions to a file ... in real-time


The environment is Linux; language is C/C++; solar powered system so power is at a premium...

I have a file that is added to (as in, something is appended to the tail end) every 1 second. I need to identify, in a separate process, as soon data is added to the end of the file so that I can take action on it. The entire purpose of separate process is to monitor this file so the solution can be blocking.

What is the right way to do this?

The `dumb' ways I can think of involve infinite loops, sleep commands, and repeatedly trying to read ... but if I'm out of sync on my loop it can be nearly another second before I identify that data is available ... and the whole method just seems inefficient.

Is there a graceful way to identify exactly when new data is added to a file .. when that data is added once every ~1 second ... and I'm going to be monitoring this file 24/7?

Thanks for any suggestions!
 
Old 01-16-2016, 09:37 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
The Linux kernel provides the inotify subsystem exactly for this purpose. Short overview: http://linux.die.net/man/7/inotify
 
Old 01-16-2016, 10:12 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Write a program, in which you open the file and read from it. Don't stop at eof, sleep a little then try reading again. Repeat infinitely.
 
Old 01-17-2016, 09:54 AM   #4
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thanks, TobiSGD! That's a great feature added to the Linux kernel ... it wasn't around last time I was coding on this platform. Very useful.
 
Old 01-17-2016, 10:21 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Consider using libuv, which supports monitoring filesystem events and which was used for both nodejs and neovim.

https://github.com/libuv/libuv
 
Old 01-17-2016, 10:53 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,672
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Quote:
Originally Posted by NevemTeve View Post
Write a program, in which you open the file and read from it. Don't stop at eof, sleep a little then try reading again. Repeat infinitely.
I would suggest combining both of these approaches: instead of "busy waiting" to see if the file has been extended (which, actually, can be problematic if the file is "remote"), use some appropriate notification mechanism to allow the program to efficiently "sleep" until there might be more data available in the file.

Obviously, the cleanest approach would be to write the data to a pipe that is being read by a process which eventually writes the data to the permanent destination.
 
Old 01-18-2016, 09:21 PM   #7
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
I think the method I have settled on is using inotify if the input file is a regular file, using O_ASYNC and sigaction() to set up SIGIO for the inotify file-descriptor, and pause() while I wait for signals. If the input file is actually a pipe, I am simply setting up SIGIO to trigger ``I/O possible'' for the pipe, and again, using pause() while I wait.

I realize I could open the file in blocking mode and have read() block while I wait for input ... but there is also a network socket I am listening to ... so I don't want either the file or the socket blocking.

I saw some post somewhere where an `expert' said, ``if signals are your answer then you're asking the wrong question''... but I don't see how anything would be more appropriate for my application than pause() and SIGIO.

Thanks for the input, everyone.
 
Old 01-20-2016, 07:20 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by Scraph View Post
I realize I could open the file in blocking mode and have read() block while I wait for input ... but there is also a network socket I am listening to ... so I don't want either the file or the socket blocking.
Sounds like a job for select(3) (or poll(3)).
EDIT: nevermind, won't work on files, as NevemTeve points out.

Although if you already have something working maybe "if it ain't broken don't fix it" applies...

Last edited by ntubski; 01-20-2016 at 09:51 AM. Reason: oops
 
Old 01-20-2016, 07:36 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Yes, using select/poll is the usual method, except they don't work on disk-files (or so I was told).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Real-time traffic monitor Last Attacker Linux - Software 11 12-29-2008 09:27 AM
LXer: Red Hat announces real-time additions to Linux LXer Syndicated Linux News 0 12-05-2007 12:00 PM
Real Time Monitor for proxy server kepkin Linux - Server 0 08-30-2007 07:57 AM
Monitor directory size in real-time billygoat32 Linux - Software 2 10-28-2005 09:21 AM
Graphical real-time bandwidth monitor ??? khermans Linux - Software 5 05-10-2004 09:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration