Linux File lock implementation

Source: Internet
Author: User
Linux File locks are the default recommended locks.

Two points are confirmed as follows:

1) exclusive read/write locks.

2) Lock contention when the file lock areas cross.

Filemutex. h:

class FileMutex {public:FileMutex(const string& filename);virtual ~FileMutex();int lockR(int offset,int len);int lockW(int offset,int len);int lockRWait(int offset,int len,int millseconds);int lockWWait(int offset,int len,int millseconds);int unlock(int offset,int len);private:int fd;};

Filemutex. cpp

#include "FileMutex.h"FileMutex::FileMutex(const string& filename) {fd = open(filename.c_str(),O_RDWR| O_CREAT, 0600);if(fd<0){perror("FileMutex init:");}cout << "fd:" << fd << endl;assert(fd);}FileMutex::~FileMutex() {if (fd > 0) {close(fd);}}int FileMutex::lockR(int offset, int len) {struct flock lock;lock.l_start = offset;lock.l_len = len;lock.l_type = F_RDLCK;lock.l_whence = SEEK_SET;int result=fcntl(fd, F_SETLK, &lock);if(result<0){perror("lockR:");}return result;}int FileMutex::lockW(int offset, int len) {struct flock lock;lock.l_start = offset;lock.l_len = len;lock.l_type = F_WRLCK;lock.l_whence = SEEK_SET;return fcntl(fd, F_SETLK, &lock);}int FileMutex::lockRWait(int offset, int len, int millseconds) {struct flock lock;lock.l_start = offset;lock.l_len = len;lock.l_type = F_RDLCK;lock.l_whence = SEEK_SET;return fcntl(fd, F_SETLKW, &lock);}int FileMutex::lockWWait(int offset, int len, int millseconds) {struct flock lock;lock.l_start = offset;lock.l_len = len;lock.l_type = F_WRLCK;lock.l_whence = SEEK_SET;return fcntl(fd, F_SETLKW, &lock);}int FileMutex::unlock(int offset, int len) {struct flock lock;lock.l_start = offset;lock.l_len = len;lock.l_type = F_UNLCK;lock.l_whence = SEEK_SET;return fcntl(fd, F_SETLK, &lock);}const option options[] = { { "file", required_argument, NULL, 'f' }, { "type", required_argument, NULL, 't' }, {"offset", required_argument, NULL, 'o' }, { "len", required_argument, NULL, 'l' }, { 0, 0, 0, 0 } };int main(int argc, char **argv) {char cFileName[128];int type;int offset;int len;int ch;while ((ch = getopt_long(argc, argv, ":f:t:o:l:", options, NULL)) != -1) {switch (ch) {case 'f':strcpy(cFileName, optarg);break;case 't':type = atoi(optarg);break;case 'o':offset = atoi(optarg);break;case 'l':len = atoi(optarg);break;default:break;}}cout << "FileName:" << cFileName << " type:" << type << " offset:" << offset << " len:" << len << endl;FileMutex mutex(cFileName);int lockResult;if (type == 0) {lockResult = mutex.lockR(offset, len);}if (type == 1) {lockResult = mutex.lockW(offset, len);}cout << lockResult << endl;pause();}

Run:

./Mutex -- file = lock1 -- type = 0 -- offset = 0 -- Len = 10

./Mutex -- file = lock1 -- type = 0 -- offset = 10 -- Len = 10

Two read locks can coexist.

./Mutex -- file = lock1 -- type = 0 -- offset = 0 -- Len = 10

./Mutex -- file = lock1 -- type = 1 -- offset = 10 -- Len = 10

The read/write locks and read/write locks must be in the same region.

./Mutex -- file = lock1 -- type = 0 -- offset = 0 -- Len = 10

./Mutex -- file = lock1 -- type = 1 -- offset = 11 -- Len = 10

Areas do not overlap and can coexist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.