Thread: Waiting for file availability

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Waiting for file availability

    Is there any portable way to wait until a file becomes available (assuming it exists)? For example, I thought of this:
    Code:
    //assuming some other app is using "file"...
    //wait until "file" is available, then delete it
    while( !std::fstream("file") );
    std::system("del file");
    Of course, speaking of portability, the contents of system() are not. Still, then I realized how inefficient my method waswas. Something more along the lines of this was in order:
    Code:
    while( !std::fstream("file") )   //check every 5 seconds
    {
       Sleep(5000);
    }
    std::system("del file");
    But then Sleep() is not portable! Am I out of luck? Is this os-specific?
    Last edited by CodeMonkey; 01-07-2007 at 11:25 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    well, there's always something like
    Code:
    namespace my_util
    {
       //with <ctime>
       inline const std::clock_t sleep(int duration,bool in_seconds = true)
       { 
          std::clock_t start = std::clock(NULL);
          while( std::clock(NULL) < start+duration*(in_seconds ? std::CLOCKS_PER_SEC : 1) );
          return std::CLOCKS_PER_SEC;
       }
    }
    But what about deleting a file?
    Last edited by CodeMonkey; 01-08-2007 at 12:09 AM. Reason: return type
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by CodeMonkey
    well, there's always something like
    Code:
    namespace my_util
    {
       //with <ctime>
       inline const std::clock_t sleep(int duration,bool in_seconds = true)
       { 
          std::clock_t start = std::clock(NULL);
          while( std::clock(NULL) < start+duration*(in_seconds ? std::CLOCKS_PER_SEC : 1) );
          return std::CLOCKS_PER_SEC;
       }
    }
    Have you measured the CPU usage in your solution?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    It's, well..... absolutely gigantic. BTW, thanks Desolation.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'd put something like
    Code:
    #ifdef WIN32
       Sleep(duration);
    #elif _UNIX
       sleep(duration/1000);
    #else
       ...
    #endif
    in my code to support different OSes when to go with the solution that eats my CPU time even if it is universal
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Small variation on vart's code, just in case you want to use the timer more than once.

    Code:
    #ifdef WIN32
        #define SLEEP_MS(a) Sleep(a)
    #elif _UNIX
        #define SLEEP_MS(a) sleep( (a) / 1000 )
    #elif
        ...
    #endif

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Desolation
    Small variation on vart's code, just in case you want to use the timer more than once.

    Code:
    #ifdef WIN32
        #define SLEEP_MS(a) Sleep(a)
    #elif _UNIX
        #define SLEEP_MS(a) sleep( (a) / 1000 )
    #elif
        ...
    #endif
    I prefer to create function named SLEEP_MS than macro.
    Didn't mean that my code should go in the place where we need to call Sleep,
    Instead it should be wrapped in some utility function and place in the file containing all OS adapters.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes, that's the solution I've been finding online. Thank you.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here http://cboard.cprogramming.com/showp...66&postcount=9
    CornedBee suggested even better solution that uses the same idea
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    However, the idea of this thread is not portable in the first place. Unless the application that currently has the file open locked it explicitly, you can simply delete it in Linux (and probably most Unices). The immediate and strong locking that Windows does isn't the situation everywhere. In other words, deleting the file would succeed immediately, leaving the other application with a phantom file. Strictly speaking, you're only removing the symbolic name of the file from the directory - hence the term "unlink". The actual file, identified by its inode number, is still there, and stays there as long as anything holds a reference. And the open file descriptor of that other application is a reference. The file will be deleted from disk the moment the other application closes its file descriptor.
    The waiting is thus only necessary in Windows in the first place, which suggests implementing a platform-specific proper waiting method there. I have to admit, though, that I'm not sure what the right method is. LockFile(Ex) require an open handle, and I'm not sure if a statistics handle (the only that can be opened on an already locked file) is enough.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. signal sending and waiting
    By eva69 in forum C Programming
    Replies: 1
    Last Post: 10-03-2008, 12:03 PM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  4. pclose without waiting for child
    By Largo in forum C Programming
    Replies: 1
    Last Post: 11-26-2005, 05:13 AM
  5. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 02:18 AM