4 ms·
Why C? I'm just curious if another language can be used. (c++, go, rust).
by acomjean 13y ago
Why C?
I'm just curious if another language can be used. (c++, go, rust).
- DSMan195276 13y agoC is really easy for this sort of thing. I don't know the specifics for using C++, Go, Rust, etc. (Disclamer, I'm a fan of C, and I know some C++ and Rust but little Go). The kernel also doesn't get run-time help, so a lot of features of C++, Rust, and Go would be gone right off the bat (Ex. You can't be spawning threads in Rust or throwing exceptions in C++ without first setting up your environment, which can be a non-trivial task). Using a subset of any of those languages should work, provided the compiler can provide straight ELF binaries that you could boot (I know g++ and rustc can do this, Go is the only one I wonder about.). If you get to that point, then you have the fact that the C ABI is fairly standard, extremely easy to use, and well-known, so it makes it easy to intermingle assembly and C. If you did it in C++, you'd definitely want to extern "C" any symbol asm is going to call so it isn't mangled, Rust should be the same, no idea on Go (You'd really also want to extern "C" any user-space system calls, because the name mangling might get in the way.). For the last point, you have to be able to use pointers and write to arbitrary memory locations, which is easy for C and C++, possible in Rust, but I don't know about Go. IMO, the code displayed in this example is really hardly a kernel (It is, but it doesn't really do anything). For a more complex kernel, you may get a benefit from using a language other then C, but for something this simple C lowers the complexity to get a working example with minimal issues going.
- alexanderblom 13y agoGo doesn't really work because it needs a runtime (as far as I know). Rust however is quite useable. There's a couple of kernels, including one by me: https://github.com/lexs/rust-os https://github.com/lexs/rust-os
- rdc12 13y agoHere is someone else who has done something similar in Rust, http://jvns.ca/blog/2014/03/12/the-rust-os-story/ http://jvns.ca/blog/2014/03/12/the-rust-os-story/ (she has other posts on Rust kernel iirc)
- e12e 13y agoC is (pretty) easy to use as a structured, portable assembler (eg: you get loops, easy-to-use variables, which you don't have in assembly) -- and easy to interface with assembly. Pascal might be another good alternative -- as it also doesn't require a run-time. I don't think go will ever (officially) support this kind of thing, rust most likely will. For an example of something that's not C, have a look at Marte OS, implemented in ADA: http://marte.unican.es/ http://marte.unican.es/