Thread: If C++ is king, why was the linux kernel written in C/ASM?

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    If C++ is king, why was the linux kernel written in C/ASM?

    I'm curious.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    C++ isn't King, its a general purpose language. ASM allows pretty much the lowest level of control so you pretty much need to use it to make a kernal (you dont HAVE to..) and C is more readable.
    "...since anyone who is anyone knows C..." -Peter Cellik

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, there are historical reasons, one being C came before C++. Also, back in the day, compiler technology wasn't what it is (and still isn't for some compilers) - meaning that compiled C++ can have alot of "overhead", in code space and execution time. This isn't neccessarly true for some compilers if you know what you are doing. But, by the time you've trimmed the fat from C++ code to get it running as fast as C, the biggest advantage you get in the end is type safety by using templates instead of macro's etc. The Linux kernel does utilize OO programming techniques, they just aren't enforced by the language. For instance, you can encapsulate data in a C struct and have functions which operate on the data in that struct. One could say that standared C file I/O is object-oriented, where a FILE* is the object and fopen(), fclose(), fread(), fwrite(), etc... are its methods.

    There are OS's out there that are implemented in C++, Symbian is one that comes to mind. http://www.symbian.com/index.html

    gg

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > C is more readable.

    Than C++?

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Maybe Torvalds didn't know C++...

  6. #6
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Hey, don't double post 2 one line posts. Use the edit button if you remeber something.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "Readability" is very subjective, especially when code is written using libraries/frameworks (which most code is). For example, if you knew C++ like the back of your hand but had never been exposed to STL - reading STL based code would be a nightmare.

    Browse the Linux source (http://lxr.linux.no/) and see how much is "readable" just from a C language perspective.
    Takes alot of reading before things get readable.

    gg

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Originally posted by Yawgmoth
    Hey, don't double post 2 one line posts. Use the edit button if you remeber something.
    Thanks - I'll be sure to ask you for help next time I post.

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Hey, don't double post 2 one line posts. Use the edit button if you remeber something.
    Hahahaha

    > Thanks - I'll be sure to ask you for help next time I post.
    Yeah man, if you ever need help signing in or something, let me know, I always love helping moderators.
    C++ isn't King, its a general purpose language. ASM allows pretty much the lowest level of control so you pretty much need to use it to make a kernal (you dont HAVE to..) and C is more readable.
    I agree. ASM allows for pin-point specification, and C/C++ is generally far easier for the programming beginniner to understand.

  10. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Why was Windows written in C?

  11. #11
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    >>you dont HAVE to..)

    if you want to write everything from scratch? Including the bootloader? Then yeah...you will...(IIRC from when I wrote my OS, anyhow)

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >if you want to write everything from scratch? Including the bootloader? Then yeah...you will...(IIRC from when I wrote my OS, anyhow)<

    Who said anything about writing something from scratch? You can probably get away with not using asm if you don't write the whole kernal (whatever that is) from scratch. If you wrote the whole thing from scratch then you'd be coding it all in assembler, or writing your own c/c++ compiler.
    Joe

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The reason why you use C to build most parts of the kernel is for the simple fact that C++ requires a runtime interface, believe it or not. Global variables aren't like those in C and have to be initialized before hand by the runtime (the class instances). The new and delete operators require memory mangement in part of the host OS. Error handling is also handled by the OS. Of course, you can turn all these features off if you're using a compiler like GNU gxx, but then, you're just really using a C++ compiler for C-like code. So, the main parts have to be written in C (or ASM) and then you can use C++. ASM is needed no matter what as the boot loader must be in real mode and 512 bytes long (if on a floppy), etc, etc....

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >Global variables aren't like those in C and have to be initialized before hand by the runtime

    Code has to be inserted to call constructors and destructors. The bog standard C runtime uses a similar technique to initialise stuff.

    >The new and delete operators require memory mangement in part of the host OS.

    Is this required by the C++ standard? Is it required by the C standard that malloc/free require memory management in part by the host OS?

    >Error handling is also handled by the OS.<

    Do you mean exception handling?

    >Of course, you can turn all these features off if you're using a compiler like GNU gxx, but then, you're just really using a C++ compiler for C-like code.<

    Rubbish.

    >So, the main parts have to be written in C (or ASM) and then you can use C++.<

    You can use C++ more or less where you use C. I suspect C is used more frequently for lower level suff due to historical reasons (ie as stated earlier C++ compilers are not always what they might be; and companies have built up large libraries/tool sets that assume the use of C).

    >ASM is needed no matter what as the boot loader must be in real mode and 512 bytes long (if on a floppy), etc, etc....<

    The bootloader is not the kernel.
    Last edited by JoeSixpack; 03-04-2003 at 06:34 PM.
    Joe

  15. #15
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Originally posted by Yawgmoth
    Hey, don't double post 2 one line posts. Use the edit button if you remeber something.
    LOL, shame shame gov't I know your name! Ah hahahhaa...mod gets scolded by MTG character lol

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux, doubles, and unions
    By paraglidersd in forum Linux Programming
    Replies: 14
    Last Post: 11-19-2008, 11:41 AM
  2. linux kernel 2.5
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-31-2002, 11:17 PM
  3. Linux? Windows Xp?
    By VooDoo in forum Linux Programming
    Replies: 15
    Last Post: 07-31-2002, 08:18 AM
  4. linux vs linux?
    By Dreamerv3 in forum Linux Programming
    Replies: 5
    Last Post: 01-22-2002, 09:39 AM
  5. Is Linux Source written in C or C++ ?
    By pritesh in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2001, 06:21 AM