cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : UNIX/Linux Programming : Implementing shared/exclusive locking (r...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

question  Implementing shared/exclusive locking (readers/writer) lock

sabya
Question Implementing shared/exclusive locking (readers/writer) lock


Hi,
I am using semaphore to implement Single Writer Multiple Readers (Reader / writer) lock.
The problem is that semaphores are critical resources in linux systems, and there is a limit on the number of semaphores

that you can create.
So, after creating some semaphores, sometimes my application exceeds this limit, and I get an error like 'No space left on

device.'.

I have to reboot the system sometimes, to release the semaphores, if my application is terminated abruptly!

How should I implement readers/writers lock using something else than semaphore, which is scalable?

I don't need reader/writer locks across multiple processes. I just need to use them in multiple threads of my application,

which is a single process.
But I want a very scalable solution, say I want 500 locks through out my application daemon's lifetime.

I read in the link below that postgres also suffered the same problem, but there it is recommended that the limit should be

increased, which I don't want users to do.
http://www2.units.it/~nircdc/doc/postgres/kernel-resources.html

Sabyasachi.
|
jsmith
Slightly non-portable are pthread read-write locks (man pthread_rwlock_init) as it requires you to #define __USE_UNIX98.

Alternatively, you could use a pthread_mutex_t (man pthread_mutex_init). You'll need a couple of them plus some ancillary data to create your own reader/writer lock using pthread_mutexes.

Also, you might want to man 8 ipcrm ... ipcrm is a command-line tool that allows you to delete kernel semaphores without having to reboot.
|
Zaita
Wouldn't you benefit from having more concise exception handling to ensure if something does go wrong, all of your semaphores are released?
|
Duoas
The following threads are about preventing multiple instances of an application from coexisting, but the concepts are the same and should prove you useful:
http://www.linuxquestions.org/questions/programming-9/creating-single-instance-linux-application-256693/
http://www.linuxquestions.org/questions/programming-9/restricting-multiple-instance-of-a-program-242069/

Hope this helps.
|

Registered users can reply in this forum.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us