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.

Ask HN: If Unix is written in C, how did they run C before Unix?

53 pointsby v7engineabout 2 years ago
C is a programming language and Unix is an OS. So, how did they run C before Unix era?

20 comments

ksherlockabout 2 years ago
V1: (PDP-7 Unix) Kernel is written in assembly. C does not exist yet.<p>V2: (PDP-11 Unix) Kernel is written in assembly. C compiler is written in assembly.<p>V3: Kernel is written in assembly. C compiler is written in C.<p>V4: Kernel is written in C. C compiler is written in C.<p><a href="https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=PDP7-Unix" rel="nofollow">https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=PDP7-Unix</a><p><a href="https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V2" rel="nofollow">https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V2</a><p><a href="https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V3" rel="nofollow">https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V3</a><p><a href="https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V4" rel="nofollow">https:&#x2F;&#x2F;www.tuhs.org&#x2F;cgi-bin&#x2F;utree.pl?file=V4</a>
评论 #35443267 未加载
评论 #35442463 未加载
评论 #35442583 未加载
评论 #35443181 未加载
评论 #35442825 未加载
tethaabout 2 years ago
We had a few pretty cool courses in university touching this thing. It depends a bit on the work you want to do, but:<p>- Pick a language that&#x27;s simple enough. A subset of ML would be good, but if you want to complete it, I&#x27;d recommend a simple LISP. This is your new language. This is C.<p>- Use a language you know and like to implement a compiler for this language. This is your bootstrap language. Compile this for example into C--, ASM or LLVM, depending on what you know. This is the target language. As a recommendation, keep this compiler as simple as possible so you have a reference for the next step. For C, both the bootstrap and the target language were ASM.<p>- And now iterate on extending the stdlib your language has, until you can implement a compiler for your new language in your new language. Again, keep this compiler simple without optimization or passes, just generate the most trivial machine code possible. This usually takes a bit of back-and-forth. You&#x27;ll need some function evaluation first, some expression evaluation first (this is where a lisp can be an advantage, as those are the same), then you need function definitions, then you need filesystem interactions and so on. You kinda discover what you need as you implement.<p>- Once you have all of that, (i) compile the compiler for the new language in your bootstrap language and (ii) compile the compiler for the new language using the result of (i). If you want to verify the results, compile the compiler again with the output of (ii) and check if (ii) and (iii) are different.<p>- Your new language is now self-hosted.<p>This was fun, because it was accompanied with other courses like how processor microcode implements processor instructions, how different kinds of assembly is mapped onto processor instructions, and then how higher level languages are compiled down into assembly. All of this across 4-6 semesters resulted in a pretty good understanding how Java ends up being flip-flop operations.<p>EDIT - got target &amp; bootstrap mixed up in first part.
评论 #35443459 未加载
anon25783about 2 years ago
They didn&#x27;t. There was no C before Unix. The first Unix was written in Assembly, and was used to develop the first C compiler.
athoraxabout 2 years ago
Computerphile on youtube has a pretty good series on bootstrapping a compiler:<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=lJf2i87jgFA&amp;list=RDLVlJf2i87jgFA&amp;start_radio=1&amp;rv=lJf2i87jgFA&amp;t=2">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=lJf2i87jgFA&amp;list=RDLVlJf2i87...</a>
starkparkerabout 2 years ago
see Dennis Ritchie&#x27;s <i>The Development of the C Language</i>: <a href="https:&#x2F;&#x2F;www.bell-labs.com&#x2F;usr&#x2F;dmr&#x2F;www&#x2F;chist.html" rel="nofollow">https:&#x2F;&#x2F;www.bell-labs.com&#x2F;usr&#x2F;dmr&#x2F;www&#x2F;chist.html</a><p>pre-C Unix was written in assembly
评论 #35452267 未加载
AnimalMuppetabout 2 years ago
The start of C and the start of Unix are approximately at the same time. In that time, there was a stepwise process. The first Unix (IIRC, someone can correct me) was written in assembly. Also written at that time was a simple compiled language. That compiled language was used to write a more complicated compiler. There was another such cycle (I think) before there was a C compiler - and even then, it wasn&#x27;t the full C, but a subset. Unix was then re-written in C.
评论 #35442467 未加载
jzellisabout 2 years ago
Redneck dog meme: If we wuz descended from wolves, how are there still wolves?<p>(I&#x27;m just teasing, as you were.)
hawskiabout 2 years ago
Bootstrapping is a fun thing. You already have some good answers.<p>Now imagine how most things around you were made, how higher tech was made with lower tech. How they made high precision tools, when there were only lower precision tools available? For example: how to make a 0.001 mm precise caliper when all you have is a 0.1 mm one? There were a lot of challenges like that and we still get to new ones. I just wonder what general term is used for things like that.
abraxasabout 2 years ago
C does not depend on Unix in any way. Hell, 8 bit micros like Commodore 64 and ZX Spectrum had C compilers.
评论 #35442610 未加载
JohnFenabout 2 years ago
You don&#x27;t need an operating system to run a program. Nothing about C requires Unix (or any other OS).
评论 #35442689 未加载
MarkusWandelabout 2 years ago
Any self-hosting language compiler has to start from somewhere.<p>Generally it&#x27;s an incremental process where the compiler for an early&#x2F;subset version of the language is written in another, existing language (absent one, may be assembly code).<p>Once it&#x27;s possible to rewrite the compiler in its own subset language, it becomes self-hosting. Then you can add a feature to the language, and once it works, enhance the compiler to use it, and so on.<p>Eventually the language and compiler go hand-in hand: The only way to compile it is with a compiler, and the only way to compile the compiler is with itself. This leads to interesting thought experiments such as:<p><a href="https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~rdriley&#x2F;487&#x2F;papers&#x2F;Thompson_1984_ReflectionsonTrustingTrust.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~rdriley&#x2F;487&#x2F;papers&#x2F;Thompson_1984_Ref...</a>
评论 #35442813 未加载
js2about 2 years ago
As the question has already been answered, I&#x27;ll just add that the general principle is known as self-hosting:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Self-hosting_(compilers)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Self-hosting_(compilers)</a>
coliveiraabout 2 years ago
The question is malformed because there is no need for Unix to run C programs. In theory they could have created C without an OS. However, in practice C was written to rewrite Unix in a high level language (previously it was assembly).
snvzzabout 2 years ago
They ran it in UNICS.<p>UNIX is just an UNICS rewrite in C, and was done by the authors of UNICS and C.
HeckFeckabout 2 years ago
A related question: was an Operating System first known as a Time Sharing System? I&#x27;ve seen most of what I&#x27;ve known as an OS in this era termed that, also I read that the term OS was coined for CP&#x2F;M.
评论 #35443024 未加载
评论 #35443163 未加载
评论 #35443956 未加载
simonblackabout 2 years ago
Lots of C implementations don&#x27;t&#x2F;didn&#x27;t use Unix as the underlying OS.<p>There were Cs for MSDOS, Cs for CP&#x2F;M, even Cs for Windows, etc, etc.
remramabout 2 years ago
You don&#x27;t need an OS written in a language to use that language, as evidenced by Python&#x2F;JavaScript&#x2F;BASIC&#x2F;Go&#x2F;...
bitwizeabout 2 years ago
As I understand it, the first versions of Unix were written in PDP-7 assembly. Thompson and Ritchie&#x27;s C work took place on that.
moralestapiaabout 2 years ago
&gt;If UNIX is written ...<p>Doesn&#x27;t that imply already that C precludes UNIX? The question doesn&#x27;t make sense.
_dain_about 2 years ago
you&#x27;ve always been able to run C on bare metal without an OS
评论 #35442479 未加载