Doubt in Semaphore or a Spinlock in a SMP machine. - Linux

This is a discussion on Doubt in Semaphore or a Spinlock in a SMP machine. - Linux ; I have a doubt in how the kernel handles this situation: Spinlock: Thread 1 - Thread 2 running in two diff processors under an SMP system. Now they are trying to acquire a spinlock S1 at the same time(even at ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Doubt in Semaphore or a Spinlock in a SMP machine.

  1. Doubt in Semaphore or a Spinlock in a SMP machine.

    I have a doubt in how the kernel handles this situation:

    Spinlock:
    Thread 1 - Thread 2 running in two diff processors under an SMP
    system. Now they are trying to acquire a spinlock S1 at the same
    time(even at machine code level they are trying to get the lock). How
    the kernel handles this? which one do you think will get the lock
    first?

    Semaphore:

    The same thing holds good for semaphore. Thread 1 - Thread 2 runing
    in two diff processors under an SMP system. So when they try to get
    the semaphore in sync that is at mechine code level both are trying to
    access/acquire the semaphore lock. How the kernel handles this?

    Can somebody explain this to me?

    p.s: Sorry if this is not the right place for this question.

  2. Re: Doubt in Semaphore or a Spinlock in a SMP machine.

    On Oct 20, 8:15*pm, Mohan_Gunasekaran
    wrote:
    > I have a doubt in how *the kernel handles this situation:
    >
    > Spinlock:
    > Thread 1 - Thread 2 *running in two diff processors under an SMP
    > system. Now they are trying to acquire a spinlock S1 at the same
    > time(even at machine code level they are trying to get the lock). How
    > the kernel handles this?


    One gets the spinlock, the other spins.

    > which one do you think will get the lock
    > first?


    It's arbitrary. If you care, you shouldn't be using spinlocks.

    > Semaphore:
    >
    > The same thing holds good for semaphore. Thread 1 - *Thread 2 runing
    > in two diff processors under an SMP system. So when they try to get
    > the semaphore in sync that is at mechine code level both are trying to
    > access/acquire the semaphore lock. How the kernel handles this?


    An arbitrary thread gets the semaphore, the other atomically places
    itself on the semaphore's wakeup list and goes to sleep.

    > Can somebody explain this to me?
    >
    > p.s: Sorry if this is not the right place for this question.


    CPUs have special serializing instructions. If two CPUs run them
    concurrently, one will succeed and the other will fail. Which does
    which is arbitrary. Some CPUs are random, some have a "preferred CPU",
    some enforce strict alternation. Generally it doesn't matter because
    perfect races are incredibly rare. One CPU need only be a cycle ahead
    of the other to ensure it wins the race.

    DS

  3. Re: Doubt in Semaphore or a Spinlock in a SMP machine.

    David Schwartz writes:
    > On Oct 20, 8:15*pm, Mohan_Gunasekaran
    > wrote:
    >> I have a doubt in how *the kernel handles this situation:
    >>
    >> Spinlock:
    >> Thread 1 - Thread 2 *running in two diff processors under an SMP
    >> system. Now they are trying to acquire a spinlock S1 at the same
    >> time(even at machine code level they are trying to get the lock). How
    >> the kernel handles this?

    >
    > One gets the spinlock, the other spins.
    >
    >> which one do you think will get the lock
    >> first?

    >
    > It's arbitrary. If you care, you shouldn't be using spinlocks.


    A 'spin lock' is the most simple possible mutual exclusion
    mechanism. The question 'which gets it first' is completely
    meaningless: One gets it, the other doesn't. Both must be capable of
    dealing with the possibility that the lock is presently locked, hence,
    it does not matter 'which gets it first'.

+ Reply to Thread