LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-02-2011, 08:55 AM   #1
sha_neb
Member
 
Registered: May 2007
Posts: 34

Rep: Reputation: 15
locks , mutex


Hi,

How read write locks, mutex,s , semaphore , spin lock etc , are implemented ?

compare and contrast them ?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-02-2011, 10:03 AM   #2
Julian Andrews
LQ Newbie
 
Registered: Jan 2011
Distribution: Ubuntu
Posts: 21

Rep: Reputation: 13
Hi sha_neb,

This question is a little vague since the particular implementation of these various objects might vary a lot by language - if you can give a better idea of what context you want to use them in, it might help.

Doing my best to answer your question though, a sempaphore is generally just a number which can be incremented or decremented via an atomic operation (that is an operation which cannot be interrupted by another process/thread). A thread wanting to access some limited resource will check if the value of the semaphore is positive, and if it is, will decrement the semaphore, and then access the resource - on completion the thread will increment the value of the semaphore. So the basic implementation of a semaphore has a very low overhead. A lock or a mutex is just a binary semaphore (or a semaphore with a starting value of 1).

The main differences between the various kinds of semaphores is in what a thread is expected to do if the semaphore's count is non-positive. In a spinlock, the thread simply continues to execute in a loop 'spinning,' until the resource becomes available. This allows rapid execution of the blocked operation, but can tie up system resources running a useless loop if the resource doesn't become available quickly. In most ordinary semaphores and locks the thread will reschedule itself to check for the resource's availability. This lets other threads run, but does involve the overhead of rescheduling. One other common approach is to have a mutex which cues up commands to be executed. This avoids both spinning and rescheduling if your threads don't need to wait for the results of the commands, but just need to get the commands executed eventually.

Generally, the spinlock model is good if you never expect a thread to have to wait long to acquire the resource since it prevents the thread from being interrupted. If the resources is often going to be in use, though, or could be in use for long periods of time a spinlock becomes incredibly inefficient, and an ordinary semaphore is preferable. If your threads don't need access to the results of the operations on the resource, then some sort of queue mutex might be best. This will generally involve some sort of list structure (a linked list or an array maybe? This will depend on the particular implementation), but the overhead of creating this list structure is likely to be small compared to 'spinning' or rescheduling.
 
2 members found this post helpful.
Old 02-02-2011, 10:20 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,200

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
Locking in general is generally implemented with the test and set instruction.

Instead of asking us to reproduce what's in Wikipedia and in your textbooks, why don't you just look them up?

Last edited by dugan; 02-02-2011 at 10:23 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Mutex v/s Semaphore jayadhanesh Linux - Software 3 12-07-2010 12:20 AM
pthread and mutex question Michael23 Programming 2 11-16-2009 03:53 AM
Why does pthread_cond_wait need a mutex? fuzzyBuzz Programming 4 06-01-2009 02:16 PM
Mutex new2lunix Programming 1 12-02-2008 08:12 PM
Mutex attributes? estratos Programming 12 12-24-2006 03:54 AM

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

All times are GMT -5. The time now is 08:29 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