kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: ruben@mrbrklyn.com (Ruben)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Semaphore and Spinlock
Date: Mon, 27 Apr 2015 15:09:58 -0400	[thread overview]
Message-ID: <553E8986.1070005@mrbrklyn.com> (raw)
In-Reply-To: <4E5779AD88B2F040B8A7E83ECF544D1A5E3C10@SJCPEX01CL03.citrite.net>


On 04/27/2015 12:31 PM, Jeff Haran wrote:
>> -----Original Message-----
>> From: kernelnewbies-bounces+jharan=bytemobile.com at kernelnewbies.org
>> [mailto:kernelnewbies-
>> bounces+jharan=bytemobile.com at kernelnewbies.org] On Behalf Of
>> Abhishek Bist
>> Sent: Monday, April 27, 2015 6:19 AM
>> To: kernelnewbies at kernelnewbies.org
>> Subject: Semaphore and Spinlock
>>
>> [ Semaphores are a bit like spinlocks, except the holder of a semaphore is a
>> process, not a CPU. ] This is a very first line that is bein written on the
>> description of semaphore on kernel newbies.So what are the different
>> parameter that could justify this statement or the way it could be justified
>> and understood.
>>
>> It makes sense to me conceptually. Say you are running some kernel code on a

> multicore system and that code serializes access to some data structure via a spinlock.

>   If core A takes the spinlock, then core B comes along and tries to take it, core B will


No - that is completely confused.   A semaphore doesn't even imply a lock.
When a wait calls and the semaphore is less than zero, then the process 
that called
wait then is put on the wait queue and essentially blocks itself.

When a process signals a semaphore, it increments the semaphore and 
then, if the
value of the semaphore is <= 0 its goes and signals the wait queue for 
next waiting task
from the wait queue and puts in on the run queue for scheduling.




> spin until core A releases the spin lock, at which point core B will hold it. Conceptually, core B is prevented from doing anything else while core A holds the spinl
> Now say you are running some kernel code that is executed in the context of multiple processes and that code serializes access to some data structure via a semaphore. If process A takes the semaphore, then process B comes along and tries to take it, process B will block (the core B is running on will do a context switch to some other process) until process A releases the semaphore, at which point process B will hold it and once the scheduler on B's core allows process B will start running again. Conceptually, process B is prevented from doing anything else while process A holds the semaphore. A mutex here would also apply.
>
> Semaphore is to process like spinlock is to core, at least in this case where the semaphore is being used for mutual exclusion.
>
> Jeff Haran
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>

  reply	other threads:[~2015-04-27 19:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-27 13:18 Semaphore and Spinlock Abhishek Bist
2015-04-27 14:21 ` Ruben Safir
2015-04-27 16:31 ` Jeff Haran
2015-04-27 19:09   ` Ruben [this message]
2015-04-27 20:20     ` Jeff Haran

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=553E8986.1070005@mrbrklyn.com \
    --to=ruben@mrbrklyn.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).