Chinaunix首页 | 论坛 | 博客
  • 博客访问: 433940
  • 博文数量: 103
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-15 22:17
文章分类

全部博文(103)

文章存档

2013年(4)

2012年(99)

我的朋友

分类:

2012-10-10 22:16:47

原文地址:linux为什么不用c++写? 作者:bough22

最近在看文件系统,里面有好多面向对象的封装,我就奇怪了,为什么不用面向对象语言c++,或其他高级语言呢?
想来想去就想到可能是c更适合于底层,可是心里还是没底,于是google了一下,哈哈!


在最近的一个关于LKML的讨论中,Linus给出了为什么不用C++来写Linux内核的理由:

"In fact, in Linux we did try C++ once already, back in 1992. It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.

“事实上,我们曾经尝试过用C++来写,是在1992年的时候。很糟糕。相信我--用C++来写内核代码是一个非常愚蠢的想法。”

"The fact is, C++ compilers are not trustworthy. They were even worse in 1992, but some fundamental facts haven't changed: 1) the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels. 2) any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel. 3) you can write object-oriented code (useful for filesystems etc) in C, _without_ the crap that is C++."

“实际上,C++编译器是很不可靠的。在1992年的时候情况就很差了,而且一些基础的东西到现在还没有改变:(1)整个C++对Exception的处理根本就是不完整的,特别是在写内核的时候。(2)任何喜欢把跟内存分配有关的功能匿藏起来的编译器或者程序语言对于编写内核来说都是错误的选择。(3)在C里面你也可以写面向对象的代码(写文件系统的时候很有用),是不需要C++的。

在附上一封linus的邮件:
Linux的回复:

Name: Linus Torvalds () 6/8/10

anon2 (anon@anons.com) on 6/8/10 wrote:
>
>But productivity is a difference thing when it comes to
>kernel code. Linux devs are working practically for free.
>So the same amount of budget can get you whole lot work
>done.

Actually, this is wrong.

People working for free still doesn't mean that it's fine to make the work take more effort - people still work for other compensation, and not feeling excessively  frustrated about the tools (including language) and getting productive work done is a big issue.

    So if a language change were to make people much more productive, that would be a good thing regardless of how  much people end up getting paid. It's definitely not about the money.

   But the thing is, "lines of code" isn't even remotely close to being a measure of productivity, or even the gating issue. The gating issue in any large project is pretty much all about

  (a) getting the top people and

  (b) communication.

  In the kernel, we have roughly a thousand people being attributed for each and every kernel release (at about three months apart). Now, there's a long tail, and the hundred (or even fifty) top contributors do most of the bulk work, but even then, the biggest issue that I end up worrying about is not even the code, but the "flow" of code and development.

  For example, I personally don't even write much code any more, and haven't for years. I mainly merge (and to a large degree - don't merge: a large portion of what I do is telling people "No, I won't take this, because of xyz". Even if rejection ends up being the rare case, it's actually the main reason for me existing. Anybody can say "yes". Somebody needs to say "no").

  And the best way to make things work is to not need to communicate at all. It's exactly the same issue as in parallel programming - any communication inevitably is the main bottleneck. And the best way to avoid communication is to have some "culture" - which is just another way to say "collection of
rules that don't even need to be written down/spoken, since people are aware of it". Sure, we obviously have a lot of documentation about how things are supposed to be done, but exactly as with any regular human culture, documentation is kind of secondary. (Put another way: there are lots of books about culture,
and you can get a PhD in anthropology and spend all your life just studying it - but for 99% of all people, you don't read a book about your culture, you learn it by being part of the community).

  And there is a very strong "culture" of C (and UNIX, for that matter). And this is also where it's so important for the language to be simple and unambiguous. One of the absolute worst features of C++ is how it makes a lot of things so context-dependent - which just means that when you look at the code, a local view simply seldom gives enough context to know what is going on.

  That is a huge problem for communication. It immediately makes it much harder to describe things, because you have to give a much bigger context. It's one big reason why I detest things like overloading - not only can you not grep for things, but it makes it much harder to see what a  snippet of code really does.

Put another way: when you communicate in fragments (think "patches"), it's always better to see "sctp_connect()" than to see just "connect()" where some unseen context is what makes the compiler know that it is in the sctp module.

   And you have to communicate in fragments in order to communicate efficiently. And I don't mean "efficiently" as in network bandwidth - I mean as in "in general". The reason we use patches instead of sending the whole project (or even just a couple of whole files) around is not because it's denser in email - it's because the only thing that matters is the change, not the end result.

   So that is a very fundamental reason for development to avoid ambiguity and context. And that, btw, has absolutely nothing to do particularly with "kernel programming". It's true in general in any sw project, but it's true in real life too: speaking or writing ambiguously is not good in normal human communication either.

  So a simple and clear language is a good thing. You don't want to be unnecessarily verbose (meaningless syntactic fluff is always bad), but at the same time you do not want to require too much context either.

  [ Lots of implicit context is fine if everybody is an expert on the subject. Which is why really esoteric scientific literature is basically unreadable unless you're an expert - it requires huge amounts of context to make sense at all. But that is simply not possible in a large project that has many different areas. For example, I know the VM and core kernel really well, but I still need to be able to read the code of various filesystems and networking code. So even for somebody like me, the code needs to be written without hidden context. ]

  And C is a largely context-free language. When you see a C expression, you know what it does. A function call does one thing, and one thing only - there will not be some subtle issue about "which version" of a function it calls.

  Of course, you can use the preprocessor and inline functions to do that, but even then you have to be pretty explicit: you can still grep for that preprocessor symbol, and it's all pretty "direct".

  Now, in other situations you do want more language support, and you do want the language to do memory allocation etc for you (ie GC - I'm not talking about that idiotic "new" keyword in C++, or other crap). In the kernel, we couldn't do that anyway. Similarly, in the kernel, we do really require very specialized locking and direct control over memory ordering etc, so a language that exposes some model of concurrency would almost certainly be too limited in that concurrency too.

  So there are particular reasons why I think C is "as simple as possible, but no simpler" for the particular case of an OS kernel, or system programming in particular. That's why I'm absolutely not saying that you should use C for all projects.

  But C++? I really don't think the "good features" of it are very good at all. If you leave C behind, do it properly and get some real features that matter. GC, some concurrency support, dynamic code generation, whatever.

   Linus

阅读(747) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~