Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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.

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

    Few things that I am Confused about

    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


    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.

    Thanks a lot!

  2. #2
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location literally, this state is hell..
    Kind of low on time, so I can't adress each particular bullet, but I can say a bit about what I know the most to hopefully get you SOME info and to bump your thread .

    For Semaphore, #4 is right, it is used for sync between threads or processes (any shared resource) as to when something else (thread, process, etc.) can use that resource. I'm not sure what you mean by recursive locking, but if I'm understanding it correctly, no - well not necessarily, anyway. A semaphore can be implemented with a simple int, and can be used such that you can only have 4 threads doing work in a specific area at a given time (say they were guaranteed not to overwrite each other's memory or something, but you wanted them limited for some other reason), so a semaphore can just be used as a counter, basically, to make sure less than the max are used - but other than the number (multiple vs single), it's pretty much the same as a mutex. A mutex is just one, one critical region or shared resource or something, only one thread can use it, it ends there.

    These are all just controls / locks. The concept is to lock a resource to control access to it.

    A spinlock would be, a simple sleep, wake, check loop until a resource is free, or if you didn't care about clock cycles, just a simple forever check loop until the resource is free. This thread (or process, w.e) would check the semaphore to see how many threads are using it, or a mutex to see if the memory or webcam or buffer or whatever is locked.

    Sorry I don't have time to write more, but hopefully that made a little bit of sense and in the rush that I typed it in and you understand what it meant.

    I know you said you googled this, but mutex and semaphore are pretty similar, so if you google "mutex vs semaphore" you're bound to get a bunch of hits. Try that. Spinlock is just a way a thread that is waiting on a mutex or semaphore can chill out and wait for it to be ready. Another way to do this is for the thread that's waiting to just go to sleep, and something else will send the program a signal when the resource is ready. I'm pretty sure this is how Java's monitoring system works, though I'm not 100% positive.

    Best of luck, OS stuff is fun once you get the hang of it .

  3. #3
    Just Joined! krokoziabla's Avatar
    Join Date
    Sep 2012
    Well, my answer is not so detailed and doesn't relate directly to Linux but I would recommend the book "Foundations of Multithreaded, Parallel, and Distributed Programming" written by Gregory R. Andrews. Among other things it explains the theory behind the things you are interested in as well as the examples of their realisation. You even can find there a manual for writing your own primitive OS which provides threads with IPC services.

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts