首页 > 后端开发 > php教程 > 为什么linux的内核用c不用c++呢?

为什么linux的内核用c不用c++呢?

WBOY
发布: 2016-06-17 08:30:49
原创
1948 人浏览过

java虚拟机以及windows都是c++写的,为什么linux的内核用c不用c++呢?php解释器和python解释器为什么用c不用c++呢?是不是说明php、python比起java虚拟机更简单,linux比起windows更简单呢?

回复内容:

从技术角度猜测一下:

从对编译的机器代码的把握度,c比c++好多了。系统程序员几乎看着c就没知道编译器里出来的汇编代码的样子。而c++编译器出来的就复杂多了,一个对象this指针,再加上各种虚表,当然还有各种拷贝构造等等。要完全把控确实不容易。(虽然我没有写过操作系统内核,但我总觉得操作系统内核就应该把控到汇编,用c只是为了提高开发效率。)

从c++的高级语言特性来说,模板,多态等特性,到底在系统开发中用还是不用呢?这是一个很现实的问题。除非能够很好的把控这些高级特性,否则轻导致编译出来的代码膨胀,重则导致奇怪的问题。而且内核调试和应用程序调试完全不是一回事啊…所以bochs虽然都转成c++了,但开发说明里明确要求不允许使用模板。

最后还有一点不能忽略,linux内核是跨平台的,所有的平台都有对应的c语言编译器,而且应该都优化的不错。而不是所有的平台都有c++编译器,即使有也不一定优化的效率很高。

当然还有其他因素,比如linus的个人好恶… 1. Windows内核也是C的。这几年才开始有一点点C++进去,但基本上只用了C with class。
2. Linus个人非常讨厌C++。
3. Linux kernel 1991年开搞的,C++到1998才定型。
4. 语言和写的东西是否简单无关。 操作系统内核是少数几个需要明确知道电脑到底干了什么的情形,用 C 是为数不多的选择。 来自linus的官方回答(虽然原问题是问为什么git是用c而不是c++写的,但根据其中列的原因,也适用于题主这个问题):
<code class="language-text">On Wed, 5 Sep 2007, Dmitry Kakurin wrote:
> 
> When I first looked at Git source code two things struck me as odd:
> 1. Pure C as opposed to C++. No idea why. Please don't talk about portability,
> it's BS.

*YOU* are full of bullshit.

C++ is a horrible language. It's made more horrible by the fact that a lot 
of substandard programmers use it, to the point where it's much much 
easier to generate total and utter crap with it. Quite frankly, even if 
the choice of C were to do *nothing* but keep the C++ programmers out, 
that in itself would be a huge reason to use C.

In other words: the choice of C is the only sane choice. I know Miles 
Bader jokingly said "to piss you off", but it's actually true. I've come 
to the conclusion that any programmer that would prefer the project to be 
in C++ over C is likely a programmer that I really *would* prefer to piss 
off, so that he doesn't come and screw up any project I'm involved with.

C++ leads to really really bad design choices. You invariably start using 
the "nice" library features of the language like STL and Boost and other 
total and utter crap, that may "help" you program, but causes:

 - infinite amounts of pain when they don't work (and anybody who tells me 
   that STL and especially Boost are stable and portable is just so full 
   of BS that it's not even funny)

 - inefficient abstracted programming models where two years down the road 
   you notice that some abstraction wasn't very efficient, but now all 
   your code depends on all the nice object models around it, and you 
   cannot fix it without rewriting your app.

In other words, the only way to do good, efficient, and system-level and 
portable C++ ends up to limit yourself to all the things that are 
basically available in C. And limiting your project to C means that people 
don't screw that up, and also means that you get a lot of programmers that 
do actually understand low-level issues and don't screw things up with any 
idiotic "object model" crap.

So I'm sorry, but for something like git, where efficiency was a primary 
objective, the "advantages" of C++ is just a huge mistake. The fact that 
we also piss off people who cannot see that is just a big additional 
advantage.

If you want a VCS that is written in C++, go play with Monotone. Really. 
They use a "real database". They use "nice object-oriented libraries". 
They use "nice C++ abstractions". And quite frankly, as a result of all 
these design decisions that sound so appealing to some CS people, the end 
result is a horrible and unmaintainable mess.

But I'm sure you'd like it more than git.

			Linus
</code>
登录后复制
首先,我想用两段"很长的代码"来做一个开始。
下面这段代码叫 min.c:
<code class="language-c"><span class="cp">#include <stdio.h></stdio.h></span>

<span class="cm">/*</span>
<span class="cm"> * min()/max() macros that also do</span>
<span class="cm"> * strict type-checking.. See the</span>
<span class="cm"> * "unnecessary" pointer comparison.</span>
<span class="cm"> */</span>
<span class="cp">#define min(x,y) ({ \</span>
<span class="cp">        typeof(x) _x = (x);     \</span>
<span class="cp">        typeof(y) _y = (y);     \</span>
<span class="cp">        (void) (&_x == &_y);    \</span>
<span class="cp">        _x 

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
        <span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
        <span class="kt">double</span> <span class="n">d1</span> <span class="o">=</span> <span class="mf">3.14</span><span class="p">,</span> <span class="n">d2</span> <span class="o">=</span> <span class="mf">2.718</span><span class="p">;</span>

        <span class="n">printf</span><span class="p">(</span><span class="s">"min of a & b: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">min</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">));</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">"min of d1 & d2: %lf</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">min</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span> <span class="n">d2</span><span class="p">));</span>

        <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</span></code>
登录后复制
这个问题也是当时困扰我的,为什么Linux kernel没有引入C++. 各甚用C++重写,收藏的一个回答。

<code class="language-text">The reasons specifically for Linux are explained in great detail as
part of the Linux Kernel Mailing List (LKML) Frequently Asked
Questions (FAQ) at:
  http://www.tux.org/lkml/
Scroll down to section 15 (titled Programming Religion) and read the
explanation. A summary is:
 - Linus started with an 386 computer, Minix, and gcc (no g++ available)
 - C is used instead of Assembly for a LOT of reasons (maintenance,
readability, efficiency, ...)
 - it is not clear how an object oriented language helps in OS design
 - MANY years ago, they tried using g++ to compile the Linux kernel
and found it ran slower than when compiled with gcc (many thought it
should have been the same); they are not willing to do this again
 - Linus makes the final decision and he's decided to stick with C
There is also another good explanation of some of the issues at
  http://kerneltrap.org/node/2067
which describes one person's attempt to build adapt some C++ code to
incorporate into a kernel module.

For a historical perspective, Thompson and Ritchie developed Unix
using some of the concepts from Multics and CTSS
  http://www.multicians.org/unix.html
including implementation of most operations in a higher order language
(Unix - C, Multics - PL/I), common naming of commands, etc. I also
refer you to
  http://cm.bell-labs.com/cm/cs/who/dmr/hist.html
which describes the EARLY development of Unix including the conversion
from Assembly on the PDP-7 to C on the PDP-11 (near the end of the
paper).

Building from that basis, the Unix from AT&T that was widely
distributed and worked on by thousands of people was a C based
operating system. In the 1970's and 1980's, an alternative to AT&T's
distribution was developed at UC Berkeley, see
  http://en.wikipedia.org/wiki/Berkeley_Software_Distribution
for a relatively short explanation of the development of BSD. The BSD
version eventually did not use ANY of the AT&T code base, but was also
implemented in C. At the end of the article is a list of several BSD
descendants including SunOS (now Solaris).

That is not to say that some applications are better implemented in an
OO language such as C++. There are several Graphical User Interface
(GUI) toolkits such as KDE which are implemented in C++. However,
there are applications that are better suited for a more procedural
language, and at this point operating systems are in that category.

If this answer is unclear or somehow incomplete, please make a
clarification request.
Good luck with your work.

  --Maniac
</code>
登录后复制
Linus不喜欢C++ 更底层,更可靠,更稳定。 问Linus吧,看看他会不会喷你一脸(▭-▭)✧ c++太自由了。写操作系统内核这东西,你放心么?
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板