TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Linus Torvalds on semaphores (1999)

349 pointsby rainbowgardenover 10 years ago

21 comments

Animatsover 10 years ago
Here&#x27;s Dijkstra&#x27;s original paper on P and V (in Dutch), from about 1963.<p><a href="http://www.cs.utexas.edu/users/EWD/transcriptions/EWD00xx/EWD35.html" rel="nofollow">http:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;users&#x2F;EWD&#x2F;transcriptions&#x2F;EWD00xx&#x2F;EW...</a><p>Here is a implementation of P and V, the original counted semaphore primitives, from 1972.<p><a href="http://www.fourmilab.ch/documents/univac/fang/" rel="nofollow">http:&#x2F;&#x2F;www.fourmilab.ch&#x2F;documents&#x2F;univac&#x2F;fang&#x2F;</a><p>This is UNIVAC 1108 assembly code. Along with P and V is the code for bounded buffers, with the operations &quot;PUT&quot; and &quot;GET&quot;. Bounded buffers are what Go calls &quot;channels&quot;. Note how simple they are if you have P and V. That code even works on multiprocessors. There&#x27;s one semaphore for &quot;queue full&quot; and one for &quot;queue empty&quot;. PUT does a P on &quot;queue full&quot;, puts on an item, and does a V on &quot;queue empty&quot;. GET does a P on &quot;queue empty&quot;, takes off an item, and does a V on &quot;queue full&quot;. It&#x27;s very simple. That&#x27;s the real use case for P and V. Linus&#x27; note indicates that in 1999 he didn&#x27;t know this.<p>(I didn&#x27;t write those primitives, but I&#x27;ve used that code, and once ported it to a Pascal compiler I adapted to handle concurrency.)<p>This stuff was all well understood four decades ago. Much of it was forgotten outside the mainframe world, because threads and multiprocessors didn&#x27;t make it to microprocessors for several more decades. UNIX, for a long time, had very primitive synchronization primitives. Early UNIX didn&#x27;t have threads, and even after it got threads, it took years before the locking primitives settled down. The DOS&#x2F;Windows world didn&#x27;t get them until Windows NT, circa 1993.<p>It&#x27;s been amusing to me to see bounded buffers resurface in Go. They&#x27;re quite useful, and I&#x27;ve been using them in concurrent programs for many years.
评论 #8761654 未加载
评论 #8761824 未加载
评论 #8763361 未加载
评论 #8761788 未加载
robert_tweedover 10 years ago
<i>&quot;Dijkstra was probably a bit heavy on drugs or something (I think the official explanation is that P and V are the first letters in some Dutch words, but I personally find the drug overdose story much more believable).&quot;</i><p>Quotes like this are why I always read what Linus has to say, regardless of whether the subject is relevant to my life in the slightest.<p>Edit: Yes people, those Dutch words exist! I get it! I&#x27;m sure Linus was well aware of that when he made this remark. However, the point Linus was making (in a humorous way) was that like most computer scientists &#x2F; mathematicians, Dijkstra was overly fond of obscure, single-letter names, which nobody ever intuitively understands. Whereas the names &quot;up&quot; and &quot;down&quot; (see the rest of Linus&#x27; quote) are far more intuitive. Personally, I would argue that &quot;hold&quot; and &quot;release&quot; convey the intent in a clearer, more abstract way.
评论 #8761758 未加载
评论 #8762056 未加载
评论 #8761873 未加载
评论 #8761731 未加载
评论 #8761993 未加载
WallWextraover 10 years ago
Semaphores are basically unused in the kernel these days, abandoned in favor of mutexes. I really recommend reading kernel&#x2F;locking&#x2F;mutex.c. You can learn a lot about the details of low-level synchronization, and it&#x27;s also just a really impressive piece of ruthlessly-optimised code. Tricks like reading the lock&#x27;s &#x27;owner&#x27; field without any sort of synchronization, to decide whether to spin on the lock or to sleep.
评论 #8762259 未加载
wazari972over 10 years ago
it&#x27;s surprising that Linus answers peacefully and pedagogically ! and thus it&#x27;s a nice read to refresh the definition of semaphores, spinlocks and mutexes.<p>Maybe you can edit the title and add a little [199] :-)
评论 #8761928 未加载
评论 #8762141 未加载
评论 #8761612 未加载
评论 #8761606 未加载
PhantomGremlinover 10 years ago
Link is to a collection of Linus&#x27;s comments, from between 1999 and 2008, about how to efficiently implement mutex and semaphores in the Linux kernel.<p>Linus is the BDFL of the Linux kernel, and he obviously needs to think about the big picture. And yet in these comments he gets into nitty-gritty assembly language.<p>I love it when a big picture guy also sweats the details.
PinguTSover 10 years ago
Nice read, but still some misconceptions and misunderstandings.<p>If I am in a multiprocessor design, where each processor runs completely independent from each other and there is no central organizational unit like a central OS, like in many embedded designs, there is no such thing as a spinlock. A semaphore is also not used for sending processes to sleep.<p>Linus explanation makes sense, speaking only about a single OS, which may is comprised of multiple processors. But makes no sense at all for real multi-processor designs. I don&#x27;t blame him for his narrow view, more his professors that he never experienced real embedded design.<p>I have worked in the past with multi-processors designs, where one processor was using one OS, like OS9, and the other processor had no OS at all running. Both processors where exchanging data via a dual ported RAM. So basically both processes where competing for the same resource. But even than, using a semaphore does not prevent race-conditions. Even then it is very tricky to use them. Because dual ported RAM allowed real concurrent access to the same resource.<p>In summary, the semaphore definition is the more broad definition where 2 (or more) processes are competing for a single resource. The naming convention within Linux is more specific for this requirements. The &quot;invented&quot; spinlock is just a renamed version for a fast semaphore, AFAIK. Even a mutex is just a special case of a semaphore.
评论 #8763326 未加载
评论 #8763545 未加载
herfover 10 years ago
The &quot;benchmarks game&quot; has a thread ring benchmark which can compare conditions&#x2F;mutexes&#x2F;semaphores, because for some applications they&#x27;re interchangeable:<p>4-thread x64:<p>mutex (480s): <a href="http://benchmarksgame.alioth.debian.org/u64q/program.php?test=threadring&amp;lang=gcc&amp;id=1" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u64q&#x2F;program.php?tes...</a><p>sem_wait (476s): <a href="http://benchmarksgame.alioth.debian.org/u64q/program.php?test=threadring&amp;lang=gcc&amp;id=2" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u64q&#x2F;program.php?tes...</a><p>cond_wait (271s - runs single-threaded?) <a href="http://benchmarksgame.alioth.debian.org/u64q/program.php?test=threadring&amp;lang=gcc&amp;id=3" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u64q&#x2F;program.php?tes...</a><p>1-thread x86:<p>mutex (136s): <a href="http://benchmarksgame.alioth.debian.org/u32/program.php?test=threadring&amp;lang=gcc&amp;id=1" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u32&#x2F;program.php?test...</a><p>sem_wait (131s): <a href="http://benchmarksgame.alioth.debian.org/u32/program.php?test=threadring&amp;lang=gcc&amp;id=2" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u32&#x2F;program.php?test...</a><p>cond_wait (156s): <a href="http://benchmarksgame.alioth.debian.org/u32/program.php?test=threadring&amp;lang=gcc&amp;id=3" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;u32&#x2F;program.php?test...</a>
gsgover 10 years ago
If you haven&#x27;t already, follow the &#x27;index&#x27; link (to <a href="http://yarchive.net/comp/index.html" rel="nofollow">http:&#x2F;&#x2F;yarchive.net&#x2F;comp&#x2F;index.html</a>) and bookmark that. Some very worthwhile reading there.
评论 #8763660 未加载
cafover 10 years ago
The possible future improvement that Linus mentions here:<p><pre><code> For example, the per-VM memory management semaphore could very usefully be a blocking read-write lock, but without heavy thread contention a mutex semaphore is basically equivalent. </code></pre> has actually come to pass: the mm_struct is now protected by struct rw_semaphore mmap_sem.
fleitzover 10 years ago
This is why I <i>LOVE</i> reading what Linus has to say, it&#x27;s all very practical, and pretty much ignores theory, because he has actual evidence to support his claims.
jgrahamcover 10 years ago
For those that care why Dijskstra used P() and V(): P for Passering; V for Vrijgave (release)
评论 #8761680 未加载
xyzzyzover 10 years ago
Ah, semaphores. I recall in my operating systems course we had to implement some simple concurrency patterns using semaphores as an exercise -- for instance synchronize a group of processes so that idle processes gather into groups of N, and when a group is ready, N-1 threads of the group does TaskX(), and 1 thread does TaskY(), and when they&#x27;re done, they go back to the pool. Then we implemented the same using usual mutexes and condition variables. I encourage everyone to do that, to see how one of the approaches is much more complicated than the other. Linus says that almost all practical uses of semaphores is when they are just used as mutexes, and rightly so -- implementing concurrency patterns based only on semaphores is a total pain.
评论 #8762181 未加载
michaelsbradleyover 10 years ago
There&#x27;s a free book available on the subject of semaphores:<p><i>The Little Book of Semaphores</i> by Allen Downey<p><a href="http://greenteapress.com/semaphores/" rel="nofollow">http:&#x2F;&#x2F;greenteapress.com&#x2F;semaphores&#x2F;</a>
asimpletuneover 10 years ago
Semaphores make a lot more sense if you know their literal translation of the Spanish word &quot;semaforo&quot;, which is a &quot;signal&quot;. It&#x27;s also used in Spanish to describe a traffic light.
评论 #8765510 未加载
ww520over 10 years ago
Semaphore and spinlock sure are different. People rarely use spinlock in user mode programs since there are better synchronizing mechanisms in user mode than busy waiting with spinlock. However, in kernel mode program spinlock is invaluable since it&#x27;s the only synchronizing mechanism that works in any interrupt level.
darylteoover 10 years ago
Wow I remember some of this class from my OS course 10 years after this was written.<p>We were just told about mutexes, but I never knew that it stood for Mutual Exclusion. (I did correctly intuit that a mutex was simply a specific use case of semaphore though so I&#x27;m happy and managed to pass the course in the end :D)
knutsonbradacnlover 10 years ago
Coming from embedded systems and MCU RTOS development, mutexes and semaphores are the name of the game.
esaymover 10 years ago
Man every time I read linux kernel stuff I want to join the dev team. Kernel development sounds awesome&#x2F;fun. But I can never seem to make time.
pwelchover 10 years ago
That was a pretty interesting read.<p>Random question, does anyone know what some more active newsgroups lists are today or has it all slowed down?
CmonDevover 10 years ago
Love vintage concurrency techniques :).
评论 #8761806 未加载
评论 #8763788 未加载
haileyrover 10 years ago
nice