LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2010, 01:49 AM   #1
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
How to program critical section for reader-writer systems?


Hi,

Lets say, I have a reader-writer system where reader and writer are concurrently running. 'a' and 'b' are two shared variables, which are related to each other, so modification to them needs to be an atomic operation.

A reader-writer system can be of the following types:

1. rr
2. ww
3. r-w
4. r-ww
5. rr-w
6. rr-ww

where
[ r : single reader
rr: multiple reader
w : single writer
ww: multiple writer ]

Now, We can have a read method for a reader and a write method for a writer as follows. I have written them system type wise.

1. rr

read_method
{ read a; read b; }

2. ww

write_method
{ lock(m); write a; write b; unlock(m); }

3. r-w
4. r-ww
5. rr-w
6. rr-ww

read_method
{ lock(m); read a; read b; unlock(m); }

write_method
{ lock(m); write a; write b; unlock(m); }


For multiple reader system, shared variable access doesn't need to be atomic.

For multiple writer system, shared variable access need to be atomic, so locked with 'm'.

But, for system types 3 to 6, is my read_method and write_method correct? How can I improve?

Sincerely,
Srinivas Nayak
 
Old 05-20-2010, 08:12 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by sinu_nayak2001 View Post
3. r-w
4. r-ww
5. rr-w
6. rr-ww

read_method
{ lock(m); read a; read b; unlock(m); }

write_method
{ lock(m); write a; write b; unlock(m); }

But, for system types 3 to 6, is my read_method and write_method correct? How can I improve?
The only way to improve on this is to use read/write locks. They're similar to mutexes, except that there are two locking methods - a way to read_lock them and a way to write_lock them. Any number of threads can get a read_lock, but if a thread requests a write_lock, no other threads can have any type of lock on it.

Have a look at the man pages for pthread_rwlock_* (start with pthread_rwlock_init()) if you're using pthreads, but a lot of higher-level threading libraries provide their own implementation.

John G
 
Old 05-21-2010, 12:43 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Let's be clear:

1. You've got concurrency, or you don't.

2. If a global variable is being accessed concurrently, you ALWAYS need locking.
Code:
read_method
{ 
  // This is *never* valid in a multi-threaded environment!
  read a; read b; 
}
3. The distinction is this:
Quote:
http://www.digipedia.pl/man/doc/view/pthread.3/

pthread_rwlock_rdlock: permits multiple readers, no writers
pthread_rwlock_wrlock: permits exactly one reader/writer
standard semaphore or mutex: behaves like "wrlock"
Take a look at Beej's Guide to IPC. It's not about pthreads, but it gives some great times on concurrency issues:

http://beej.us/guide/bgipc/

'Hope that helps .. PSM

Last edited by paulsm4; 05-21-2010 at 12:47 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
any function for entering critical section? ebd Programming 12 09-03-2009 12:08 AM
any function for entering critical section? ebd Linux - Newbie 1 09-01-2009 12:54 PM
how to enter critical section in Linux Enviroment ebd Programming 2 09-01-2009 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration