LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 03-04-2013, 06:13 AM   #1
ram619
LQ Newbie
 
Registered: Jun 2012
Location: India
Posts: 10

Rep: Reputation: Disabled
In depth explanation of Spinlock, Semaphore, Mutex


Seeking in depth explanation for Spinlock, Semaphore, Mutex.
I tried Google but everywhere I am just getting basic working of these things. I need more explanation.

like,
1. what to use when and why.
2. what is system's status while using one of these
3. Concrete differences between these


Thanks a lot!
 
Old 03-04-2013, 06:41 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,641

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
here you can find some information, just google: http://stackoverflow.com/questions/6...hore-and-mutex and http://stackoverflow.com/questions/5...stead-of-mutex

Last edited by pan64; 03-04-2013 at 06:42 AM.
 
Old 03-04-2013, 07:43 AM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,354

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
Hoping this isn't homework ...

spinlock:
Used to synchronize between multiple CPUs or cores, this is an explicit time-waster that consists of using an atomic machine instruction to manipulate or to test the value of a known memory location ... "spinning one's wheels" until the operation succeeds. (An "atomic" machine instruction uses the hardware to prevent conflicting simultaneous activity by any CPU other than the one issuing the instruction.)

semaphore:
The red flag on a mailbox or, in its day, beside the railroad tracks.

mutex:
The lock on the bathroom-stall door.
 
Old 04-01-2013, 02:11 PM   #4
ram619
LQ Newbie
 
Registered: Jun 2012
Location: India
Posts: 10

Original Poster
Rep: Reputation: Disabled
I read all the above mentioned links and few other links that I found on above pages but still I am a bit confused, please verify following

Mutex:-
1. Recursive locking allowed or not ?
Answer- As I think its operating system dependent.
2. holder can sleep or not ?
Answer- Can Sleep, even while holding lock.
3, is scope limited within the process ??? that is it can't be used between two process ?
Answer- Can be used between processes

4. It can't be acquired by interrupt handler or bottom half

5. to be used when u want long lock hold time ?????

6 its used for locking

Semaphore:-

1. Recursive locking allowed or not ?
Answer- not allowed ?
2. can sleep or not ?
Answer- Can Sleep.
3, is scope limited within the process ??? that is it can't be used between two process ?
Answer- Can be used between processes

4. its for synchronization or can be taken as signaling mechanism.
 
Old 04-03-2013, 08:01 AM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,354

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
Chase your homework down yourself! Good grief ...
 
Old 04-08-2013, 01:44 PM   #6
ram619
LQ Newbie
 
Registered: Jun 2012
Location: India
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
Chase your homework down yourself! Good grief ...
This is NOT homework.
I am trying to learn Linux Kernel. Please help
 
Old 04-08-2013, 05:10 PM   #7
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,354

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
All right, then ...

A spinlock is quite-literally a tiny, two-instruction loop which uses an "atomic" instruction to attempt to set a particular memory-location to a particular value, "spinning in place" until it succeeds. "Atomic" means that the CPU which is issuing the instruction prevents any other CPU from modifying the same location. This construct is used only on multi-CPU builds and is a no-op (i.e. "no code is generated") on single-CPU builds.

A mutex is a "mutual-exclusion" construct, similar to the lock on a bathroom-stall door. It is used for cooperation among processes/threads: only one can successfully "obtain" the mutex at one time; any other attempts are forced to sleep and thus wait their turn.

A semaphore is used when there is a number of resources to be shared ... such as the (say, 5) elements in a connection-pool. In this case, "up to 5" threads/processes would be allowed to enter, and the rest would be excluded (forced to sleep) for the time being.

Categorically, you should not attempt to obtain any of these "recursively."
 
1 members found this post helpful.
Old 09-23-2013, 05:13 AM   #8
trukna
LQ Newbie
 
Registered: Sep 2013
Posts: 8

Rep: Reputation: 0
@ram619
If you are still struggling to find answer and seriously want to understand linux, then i recommend you to read this
http://linuxburps.blogspot.in/2013/0...rimitives.html
 
  


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
Kernel programming - how to get an array of mutex or spinlock? Fratty Programming 2 12-24-2011 05:00 PM
mutex vs semaphore vishnu anand Programming 1 12-19-2011 03:41 PM
Mutex v/s Semaphore jayadhanesh Linux - Software 3 12-07-2010 12:20 AM
spinlock semaphore question 2.6.32 mac80211 GurgleSpuge Linux - Kernel 4 10-04-2010 09:07 PM
difference mutex vs. semaphore? Thinking Programming 1 11-16-2005 05:26 AM


All times are GMT -5. The time now is 06:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration