GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Feb-2005, 12:54
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Simple file lock works on unices, not on Windows


I am using a basic file-locking mechanism that works fine on Unix/Linux platform, but fails on Windows 2000 platform. The problem on Windows is that when a process is finished with the data file, it is unable to delete the lock file it created. Apparently, upon closing the lock file, the read-only permission is set for some reason. Some code below (methods inline for simplicity):

CPP / C++ / C Code:
class FileLock

{

public:

  static bool acquire(std::string const& filename)

  {

    int nAttempt(0), fd(open(filename.c_str(), O_WRONLY | O_CREAT | O_EXCL));

    while (fd < 0)

    {

      ++nAttempt;

      if (errno == EEXIST && nAttempt <= MAX_ATTEMPTS) {

#ifdef WIN32

        Sleep(10);

#else // assume POSIX-compliant platform

        usleep(10);

#endif

        fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_EXCL);

      } else {

        return false; // unable to acquire lock

      }

    }

    close(fd);

    FILE_LOCK._locks.insert(filename);

    return true;

  }


  static bool release(std::string const& filename)

  {

    if (unlink(filename.c_str()) == 0)

    {

      FILE_LOCK._locks.erase(filename);

      return true;

    }

    return false;

  }


  ~FileLock()

  {

    for (std::set<std::string>::iterator i(_locks.begin()); i != _locks.end(); ++i)

    {

      release(*i);

    }

  }

private:

  static size_t const MAX_ATTEMPTS;

  static FileLock FILE_LOCK;

  std::set<std::string> _locks;

  FileLock() : _locks() {}

};

size_t const FileLock::MAX_ATTEMPTS = 1000000; // attempt at most 1M aqcuires

FileLock FileLock::FILE_LOCK = FileLock();


Any help is greatly appreciated.

P.S. Thanks to the mod/whoever edited my post. I was just coming back to fix the code tags after finding out the proper tag for C/C++ code.
  #2  
Old 03-Feb-2005, 07:25
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Problem solved.


Thanks to anyone who had viewed my posted and devited any time to thinking about my problem. It turns out there was a silly oversight on my part.

I had overlooked the third, optional parameter to the POSIX open function, namely the permissions mode. Apparently, the security policy on Windows NT/2000/XP makes read-only the default mode, while the Unix/Linux test boxes I used did not. The only change required to fix this is in my acquire method:

CPP / C++ / C Code:
  static bool acquire(std::string const& filename)
  {
    int nAttempt(0), fd(open(filename.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IWRITE));

    while (fd < 0) {
      ++nAttempt;

      if (errno == EEXIST && nAttempt <= MAX_ATTEMPTS) {

#ifdef WIN32
        Sleep(10);
#else // assume POSIX-compliant platform
        usleep(10);
#endif

        fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IWRITE);

      } else {
        return false; // unable to acquire lock
      }
    }
    close(fd);

    FILE_LOCK._locks.insert(filename);

    return true;
  }
Note: The sleep times are not what I intended in my first code sample, so I didn't correct them here because I wanted the only change to be the relevant fix. It should read Sleep(1) for Windows and usleep(1000) otherwise; i.e., 1 millisecond.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 111 17-Apr-2008 15:58
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.