Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:58:34+00:00 2026-05-29T17:58:34+00:00

When tailing multiple files in C/C++ using inotify, is there a risk of a

  • 0

When tailing multiple files in C/C++ using inotify, is there a risk of a race condition when you read to the end of the file, then the file is written to before you start to poll?

The relevant piece of code starts like:

while (true) {
  struct pollfd pfd = { fd, POLLIN, 0 };
  int ret = poll(&pfd, 1, 30000);  // timeout 30s
  if (ret > 0) {
    size_t len = read(fd, buf, sizeof(buf));

    for (size_t e = 0; e < len; ) {
      inotify_event *ev = reinterpret_cast<inotify_event*>(&buf[e]);

      int i = 0;
      while (wds[i] != ev->wd) {
        ++i;
      }

      if (ev->mask & IN_MODIFY) {
        FILE* f = ff[i];
        fseek(f, pos[i], SEEK_SET);

        while (fgets(line[i]+offsets[i], MAX_LINE_LENGTH, f)) {

Does the poll function only return when the file is modified? So what happens if the following sequence happens:

  1. poll returns signalling file has been added to
  2. I read until the end of the file
  3. then the file gets added to
  4. then I start to poll

will I be stuck until the file is added to once again? Since the inotify_add_watch function only takes file names, it has no idea where I “left” off?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:58:34+00:00Added an answer on May 29, 2026 at 5:58 pm

    You must read to the end of file after you create the notification. Otherwise, you have exactly this race condition. When you get a notification, you must re-arm the notification system before you read the file to ensure that you get notified of any change that occurred after you read the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's easy to copy multiple files to a folder that doesn't exists and let
i need to read files that look like this into a datatable: A02 BLANK031
I'm having trouble both tailing and scp-ing big files with msysgit (version 1.7.1.msysgit.0). This
I'm using a log tailing app (BareTailPro) that highlights rows which match regular expressions.
I've got a program that is tailing a growing file. I'm trying to avoid
This is my .htaccess file: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # Remove multiple
Using the following function: // remove multiple, leading or trailing spaces function trim(s) {
I am opening a file with read access and allowing subsequent read|write|delete file share
I want to trim trailing whitespace at the end of all XHTML paragraphs. I
I am getting 'trailing whitespace' errors trying to commit some files in Git. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.