LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (http://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   In depth explanation of Spinlock, Semaphore, Mutex (http://www.linuxquestions.org/questions/linux-kernel-70/in-depth-explanation-of-spinlock-semaphore-mutex-4175452627/)

ram619 03-04-2013 06:13 AM

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!

pan64 03-04-2013 06:41 AM

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

sundialsvcs 03-04-2013 07:43 AM

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.

ram619 04-01-2013 02:11 PM

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.

sundialsvcs 04-03-2013 08:01 AM

Chase your homework down yourself! Good grief ...

ram619 04-08-2013 01:44 PM

Quote:

Originally Posted by sundialsvcs (Post 4924265)
Chase your homework down yourself! Good grief ...

This is NOT homework.
I am trying to learn Linux Kernel. Please help

sundialsvcs 04-08-2013 05:10 PM

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."

trukna 09-23-2013 05:13 AM

@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


All times are GMT -5. The time now is 02:04 PM.