5

I have been using C++ for a while now and I began to get interested in lower level system programming like drivers and stuff. Even some kind of primitive operating system could be very interesting project!

I have no clue where I could start. Are there any not-too-challenging things I could get started with and are there anything about C++ I should try to avoid like exceptions in performance critical code?

My current OS is Windows 7 if that matters much.

Henry H
  • 103
  • 1
  • 1
  • 8
  • 7
    If you are building device-drivers and operating systems, you should probably avoid C++ exceptions. period. – Marcelo Cantos Nov 22 '10 at 22:15
  • 3
    This resource may not be too useful for you since you are on Windows, but MacOS X implements its drivers in a C++ framework call IOKit. there is a lot of documentation on it at developer.apple.com – D.C. Nov 22 '10 at 22:24
  • ever think about writing hypervisor code? https://github.com/Bareflank/hypervisor – Rian Quinn May 24 '19 at 12:09

7 Answers7

15

Writing Windows device drivers in C++ isn't impossible, there are not many CRT functions that you could use to get you into trouble. The new operator is unusable for example, you don't have to fear a std::bad_alloc. Unless you replace it, that cuts out a rather large swath of standard C++ library classes.

But that's not really the point of a device driver, it is rather important that you make it as small as possible. C++ pays off when you write complex code. You explicitly do not want to write complex code in a device driver. Debugging it is redrum.

Linus really likes C in the kernel. There's a good reason for that.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

C++ doesn't provide quite all of the tools you will need to actually implement a full operating system in it. There are a few machine specific things that cannot be done in c++. These things are handling and raising interrupts, controlling the MMU, controlling access to supervisor cpu instructions, and a handful of other small odds and ends.

Fortunately, these things are few enough that they can be written in assembly language accessed from C++.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

Have a look at osdev.org (lots of questions that will pop into your mind when considering developing your own OS are answered here).

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
0

I would strongly suggest you start by hacking existing open source device-drivers and kernels, which you can really only do in Linux or *BSD. The experience will also give you a good sense of whether you're cut out for this kind of programming.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • I sure will. Most if not all of those are in C, right? I'll probably learn a lot from them anyways. – Henry H Nov 22 '10 at 22:25
0

I have heard the recently open sourced Symbian OS is written using C and C++. Not sure which parts of it are done with C++ as I have not read the code base. Consider looking into it.

Kerneltrap.org has some very good discussions about why the Linux kernel does not have C++ in its code base. Consider reading that as well.

vpit3833
  • 7,817
  • 2
  • 25
  • 25
0

Symbian OS is written in a variant of C++. Of course, there's assembly code for low-level things, but that is all wrapped up. You cannot use exceptions, and for real-time drivers you cannot do normal things like dynamic memory allocation, not even in C.

James
  • 9,064
  • 3
  • 31
  • 49
-2

I recommend C Programming Language and assembler. I'm not sure if it's possible to low-level much with C++.

RobertO
  • 2,655
  • 1
  • 20
  • 18
  • 3
    In what way is C++ less capable for low level things than C? – SingleNegationElimination Nov 22 '10 at 22:20
  • Most code samples I have seen and compilers I have used were for C + assembler. I just wanted to say that low-level programming was easer to learn with C. – RobertO Nov 22 '10 at 22:24
  • 2
    It's not that C++ is less capable, rather that it's less compatible. Lots of in-kernel APIs assume/support only C-style code, and don't support C++ niceties (such as exceptions, RTTI, etc). It's possible to do drivers and kernels in C++, but you have to really know what you're doing (and in particular what parts of C++ to avoid). And if you're knowledgable enough to do that, you're probably knowledgable enough to just code in straight C and avoid the hassle. – Jeremy Friesner Nov 22 '10 at 22:25
  • 2
    yay! some c++ lover doesn't like me :D – RobertO Nov 22 '10 at 22:47
  • 3
    @Jeremy, and yet the limited sub-set of C++ you could use *still* provides a lot of nice abstractions that are harder to implement or non-existent in C, such as data encapsulation, namespaces, and automatic initialization/destruction of objects. – Charles Salvia Nov 22 '10 at 23:12
  • 2
    @Charles, and almost my favorite, templates. – Henry H Nov 22 '10 at 23:19