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.

A Little Story About the `yes` Unix Command

452 pointsby mreover 7 years ago

23 comments

crehnover 7 years ago
Just for fun:<p>GNU true.c: <a href="https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;blob&#x2F;master&#x2F;src&#x2F;true.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;blob&#x2F;master&#x2F;src&#x2F;true....</a><p>OpenBSD true.c: <a href="http:&#x2F;&#x2F;cvsweb.openbsd.org&#x2F;cgi-bin&#x2F;cvsweb&#x2F;src&#x2F;usr.bin&#x2F;true&#x2F;true.c?rev=1.1&amp;content-type=text&#x2F;x-cvsweb-markup" rel="nofollow">http:&#x2F;&#x2F;cvsweb.openbsd.org&#x2F;cgi-bin&#x2F;cvsweb&#x2F;src&#x2F;usr.bin&#x2F;true&#x2F;tr...</a>
评论 #15455101 未加载
评论 #15455897 未加载
评论 #15458270 未加载
评论 #15455965 未加载
评论 #15455796 未加载
评论 #15456317 未加载
aledenover 7 years ago
This topic comes up every now and then. I thought this post was particularly insightful,<p>&quot;One thing to keep in mind when looking at GNU programs is that they&#x27;re often intentionally written in an odd style to remove all questions of Unix copyright infringement at the time that they were written.<p>The long-standing advice when writing GNU utilities used to be that if the program you were replacing was optimized for minimizing CPU use, write yours to minimize memory use, or vice-versa. Or in this case, if the program was optimized for simplicity, optimize for throughput.<p>It would have been very easy for the nascent GNU project to unintentionally produce a line-by-line equivalent of BSD yes.c, which would have potentially landed them in the 80&#x2F;90s equivalent of the Google v.s. Oracle case.&quot;<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14543640" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14543640</a>
dmitrygrover 7 years ago
That last rust example is less readable than modern template-metaprogramming variants of C++.<p>There is something elegant about being able to beat it out on speed with about 30 lines of C
评论 #15454793 未加载
评论 #15454800 未加载
评论 #15456480 未加载
评论 #15454648 未加载
评论 #15454700 未加载
pixelbeatover 7 years ago
The GNU variant was discussed recently at: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14542938" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14542938</a><p>The commit that sped up GNU yes has a summary of the perf measurements: <a href="https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;commit&#x2F;3521722" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;commit&#x2F;3521722</a><p>yes can be used to generate arbitrary repeated data for testing or whatever, so it is useful to be fast
dnelover 7 years ago
Interesting, but is there actually a practical benefit of having GigaBytes of the word yes being generated or is this all just optimisation porn?
评论 #15457977 未加载
评论 #15456732 未加载
评论 #15456659 未加载
kccqzyover 7 years ago
Really? You didn’t even mention reducing system calls? That’s basically what full_write does: try to output a whole buffer with one system call.<p>In your regular program, even with just a normal setvbuf call to set up block buffering would make a huge difference.
xurukefiover 7 years ago
<p><pre><code> import sys while True: sys.stdout.write(&quot;y\n&quot; * 2**16) </code></pre> gives me over 4GiB&#x2F;s
评论 #15460447 未加载
Myrmornisover 7 years ago
The author says &quot;no magic here&quot; for the C version:<p><pre><code> for (;;) printf(&quot;%s\n&quot;, argc&gt;1? argv[1]: &quot;y&quot;); </code></pre> but it&#x27;s not totally obvious to me whether the argument to printf would be evaluated on every iteration of the for loop or not. Does the compiler know that those don&#x27;t change, and is the answer to that question fairly basic C knowledge or not?
评论 #15455722 未加载
billsmithaustinover 7 years ago
Those performance improvements make the code more complicated too. How fast does the yes command actually need to be?
评论 #15455258 未加载
评论 #15455134 未加载
评论 #15454797 未加载
评论 #15454715 未加载
评论 #15454781 未加载
评论 #15455753 未加载
评论 #15454650 未加载
dretaover 7 years ago
Can anybody explain to me what&#x27;s this syntax? This is the first time i see anything like it, and i&#x27;ve been programming in C since i was a teenager.<p><pre><code> main(argc, argv) char **argv; { }</code></pre>
评论 #15456321 未加载
评论 #15457081 未加载
评论 #15457170 未加载
blibbleover 7 years ago
all the reddit users from the linked article missed the SIGPIPE trick!<p>you don&#x27;t need to check the return value from write() as your process will be terminated with SIGPIPE if it tries writing to a closed pipe.<p>saying that, none of them check the return code correctly: if the consumer only reads a byte at a time you could eventually get &#x27;yyyyyyyyyyyyyyyyyy&#x27; (without any newlines)<p>quite impressive that so many implementations of &quot;yes&quot; have the same bug :)
评论 #15455001 未加载
评论 #15455810 未加载
评论 #15454750 未加载
rurbanover 7 years ago
Not impressive at all. Basically he had to write a lot of manual buffering code to reach GNU yes throughput. I would suggest to use an infrastructure which already provides proper IO.<p>e.g. perl or clisp.<p><pre><code> $ perl -C0 -e&#x27;print &quot;y\n&quot; x (1024*8) while 1&#x27; | pv &gt; &#x2F;dev&#x2F;null ^C.4GiB 0:00:11 [6.17GiB&#x2F;s] [ &lt;=&gt; ] $ yes | pv &gt; &#x2F;dev&#x2F;null ^C.3GiB 0:00:07 [6.64GiB&#x2F;s] [ &lt;=&gt; </code></pre> And with linux-only vmsplice the record stands at 123GB&#x2F;s <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;unix&#x2F;comments&#x2F;6gxduc&#x2F;how_is_gnu_yes_so_fast&#x2F;diua761&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;unix&#x2F;comments&#x2F;6gxduc&#x2F;how_is_gnu_yes...</a>
13of40over 7 years ago
I guess a stretch goal would be to make a &quot;shouldi&quot; command that can consume more y&#x27;s per second than yes can produce. Of course at that point the shell itself would probably become the bottleneck.
评论 #15454705 未加载
评论 #15454709 未加载
评论 #15454994 未加载
nurettinover 7 years ago
Rust strike force is back, rewriting Unix tools in a sane language
nodesocketover 7 years ago
<p><pre><code> main(argc, argv) char **argv; { for (;;) printf(&quot;%s\n&quot;, argc&gt;1? argv[1]: &quot;y&quot;); } </code></pre> Is beautiful, readable, and minimal. The &quot;optimized&quot; Rust version is complicated and over 50 lines of code. At what point does performance optimization go too far?
评论 #15455905 未加载
评论 #15455552 未加载
评论 #15455862 未加载
cannikinover 7 years ago
Funny, I just learned about this command a couple of days ago as a simple way to max out your CPU. I was trying to drain the battery on my Macbook Pro and running 4 of these at the same time did the trick nicely. Redirected to &#x2F;dev&#x2F;null and run in the background: &quot;yes &gt; &#x2F;dev&#x2F;null &amp;&quot;
jwilkover 7 years ago
Curiously, yes(1) is not standardized by POSIX.<p>Are there any UNIX systems that don&#x27;t have it?
naranhaover 7 years ago
any idea why it is so much faster on fedora 26?<p>$ yes | pv -r &gt; &#x2F;dev&#x2F;null<p>[10.3GiB&#x2F;s]
评论 #15457496 未加载
snikchover 7 years ago
I use the yes command to defrost my lunch. I open up a couple of tabs running<p>yes &gt; &#x2F;dev&#x2F;null<p>Then place my frozen lunch on the back of my macbook. Give it an hour or so and boom, defrosted.
评论 #15455697 未加载
评论 #15454801 未加载
评论 #15454879 未加载
评论 #15454844 未加载
评论 #15455323 未加载
评论 #15455299 未加载
评论 #15456073 未加载
评论 #15455921 未加载
jlebrechover 7 years ago
maybe it&#x27;s supposed to be slow, wouldn&#x27;t a faster &#x27;yes&#x27; spam stdin much faster? you only need to hit yes occasionally and faster than a second
feelin_googleyover 7 years ago
k3<p><pre><code> while[1;`0:&quot;yes\n&quot;]</code></pre>
sabujpover 7 years ago
tldr; watched pootie tang
Annatarover 7 years ago
<p><pre><code> env::args().nth(1).unwrap_or(&quot;y&quot;.into()); </code></pre> this ridiculously complicated syntax to perform such a simple thing is why I will never accept Rust. What a clumsy, ugly language. I’ll just stick with learning ANSI common LISP, that pays immediate dividends.
评论 #15456784 未加载
评论 #15456434 未加载