The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Mutex in Perl superuser84 Shell Programming and Scripting 6 11-12-2007 09:55 PM
what is diff b/w semaphore and mutex amitpansuria UNIX for Dummies Questions & Answers 1 08-20-2007 10:45 PM
how to lock keyboard without using lock command dianayun UNIX for Dummies Questions & Answers 7 06-21-2002 03:05 PM
mutex sagar UNIX for Dummies Questions & Answers 2 02-04-2002 03:00 PM
Threads and Mutex vthasan High Level Programming 2 11-02-2001 04:34 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-20-2006
Registered User
 

Join Date: Dec 2005
Posts: 4
How to handle mutex lock?

Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively.
Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time.
ie. two Reads can happen simultaneously, but Write can't happen at Read is going on.

I can do this by using Mutex lock which can lock the file at the start of Read and release after completion of Read, But by doing this the second Read will also won't get access to the file.

Can anybody have idea, how to handle this?

Thanks in advance.

Cheers
Reply With Quote
Forum Sponsor
  #2  
Old 03-20-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
should the Read be allowed when a write is going on ?
Reply With Quote
  #3  
Old 03-20-2006
Registered User
 

Join Date: Dec 2005
Posts: 4
No, it shouldn't.
Normally we can allow this also, but needs the synchronizing mechcanism.

For a moment, letus say it shouldn't.
Reply With Quote
  #4  
Old 03-20-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
I dont have much experience in this but may be you can use two locks ,
1) Read Lock. I am not sure but there is somthing as recursive lock which you can lock multiple times. It maintains a type of counter which increments everytime you lock it and decrements everytime you unlock it. If the value is say zero then read lock is unlocked and if > 0 then it is locked.
2) Write Lock
so,
At the start of Read the check fo the write lock, if it is not locked, read lock the file.

At the start of Write check for both Read and write lock and if both are free then write lock the file and start writing.

Last edited by gauravgoel; 03-20-2006 at 12:51 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0