+ 7

Which C language is best (for building OS)?

Hello, I want to build my own OS in the future, and I'm wondering, which C language should I learn. So which C language is best? (overall and for buidling OS).

25th Oct 2020, 9:05 PM
Jakub Horák
Jakub Horák - avatar
20 ответов
+ 16
Typically I would say this question isn't very helpful, but I will make a few points. When you say, "building OS" you are actually speaking VERY generally. What people count towards being part of an OS is often suggestive to the OS in question. Do you mean writing the kernel? the drivers? the boot loader? trademark userspace apps always included? the DE? all of the above? Depending on your answer I might say that C/C++ would be better than C# because it has more of a track record and allows programming closer to the hardware, yet C# can make `unsafe` calls as well and is even able to be compiled to machine code. Not to mention that C# would be great for writing applications above the kernel and nitty gritty stuff. I would be interested to hear about an actual use case for C# in the core of an OS. Then again, I would not in my right mind suggest an unsafe language like C/C++ for a new OS and I will instead suggest Rust. There is even Redox OS actively being developed in Rust and some Linux software in Rust
25th Oct 2020, 10:00 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 9
cont: I'm aware you have to write "unsafe" code to even be capable of making an OS, but the argument for using a safe language like Rust or C# is that your unsafe calls are sectioned off and easily identifiable. This allows for easier code review and bug discovery of unsafe code usage. This non unsafe code also eliminates numerous classes of memory safety bugs and so on.
25th Oct 2020, 10:03 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 8
Jakub Horák you can read my comments but I'll summarize some pros cons Kernel, boot loader, drivers: C/C++ (You could separate these and have slightly different pros/cons but they fit roughly the same category): Pros: used in all of the main OS, fast, close to hardware, C is simple and has some good design Cons: can be hard to debug ("Segfault"), unsafe memory management (C++ has some helper classes tho), C++ is huge, slow compile times C#: Pros: fast, good language design, safe, explicit unsafe statements Cons: garbage collected by default, not widely used for OS, not compiled to native code by default Rust: Pros: memory safety, thread safety, type safety, enforced ownership rules, fast, explicit unsafe statements Cons: slow compile speed, new in the OS field, many people struggle with ownership rules I personally suggest Rust and C# when you want to make something. If I miss something or misspoke please correct me
25th Oct 2020, 10:51 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 6
User space apps and integrated apps like the desktop and network manager: Anything you want for user space apps. For integrated apps, it's often helpful to use the same language you used for the underlying software, but anything fast works.
25th Oct 2020, 10:52 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 6
tl;dr Rust and C#
25th Oct 2020, 10:55 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 5
Jakub Horák Yes, in that case, the answer is less clear, and you can honestly use a combination of several languages for different parts.
25th Oct 2020, 10:38 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 4
Aditya rout Your concept of C's performance is oversimplified. Languages like Rust and C++ can achieve this same closeness to the machine, and as for performance, this is then highly dependent upon compiler optimization. C is not guaranteed to be faster than Rust or C++ or a similar candidate, the performance is relatively the same.
26th Oct 2020, 12:51 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 4
EMMA_NUEL📚 🇺🇿 Actually, C# does not have to operate with byte code on a VM and can also be compiled directly to machine code. Theoretically, there are only a few things keeping C# from achieving performance that approaches C/C++/Rust.
28th Oct 2020, 6:55 AM
Maxwell Anderson
Maxwell Anderson - avatar
+ 4
Ishan Shah 1. The kernel does not NEED to be written in C, but it's not a bad idea. 2. While C++ might be bloated in number of features, you can achieve the same performance with it as you can in C. For OS dev, you will have to refrain from some runtime only features of C++, but you don't have to write C++ like it's C. 3. Your analogy makes no sense and doesn't improve your argument. Keep in mind that a large portion of Windows, and this incluedes the kernel, is written in C++ (putting aside my aversion to this OS). 4. I would be careful saying the Linux kernel is simple. It has over 20 million source lines of code and is setup as a monolith which only inhibits its simplicity. 5. I recommend that YOU try to read the Linux source code. I would recommend visiting wiki.osdev.org for some surface level information.
29th Oct 2020, 8:54 AM
Maxwell Anderson
Maxwell Anderson - avatar
+ 3
Aditya rout Assembly language is indeed required, yet C as a specific language is NOT required. Learning systems programming as a general skill in an appropriate language is what is required.
26th Oct 2020, 3:59 AM
Maxwell Anderson
Maxwell Anderson - avatar
+ 2
Holy C is the one and only.
25th Oct 2020, 9:33 PM
Marcel Weber
Marcel Weber - avatar
+ 2
I'm talking about building a completly new OS with my own Kernel, boot loader etc.
25th Oct 2020, 10:22 PM
Jakub Horák
Jakub Horák - avatar
+ 2
C and C++ are a great choice because they are mainly core programming language s but i think that C is a better choice because its the one better for creating the core of most operating systems like android and IOS. So i would say C or C++ because C# is so slow and it just runs on top of the Kernel and just operates on a Virtual Machine so t ain't so precise
27th Oct 2020, 2:27 PM
Programmado
Programmado - avatar
+ 1
And the ideal languages for it is?
25th Oct 2020, 10:40 PM
Jakub Horák
Jakub Horák - avatar
+ 1
normal c is worst c++ was made for a lot of desktop applications and software (frostbite minecraftbedrock windows microsoft unreal engine etc) c# is good but idk
26th Oct 2020, 7:45 PM
Matteo Rocco Micò
Matteo Rocco Micò - avatar
27th Oct 2020, 10:23 AM
narayanaprasad
narayanaprasad - avatar
0
Writing an OS now a days requires extensive knowledge of hardware implementation,computer architecture and skill in C and ASSEMBLY LANGUAGE.So rather writing an OS create any other
26th Oct 2020, 2:48 AM
Aditya rout
Aditya rout - avatar
0
Maxwell Anderson since 'c' is the lowest human readable language and it's easy concepts,I mentioned this language. The interaction duration between 'c' and the hardware is the smallest among other programming languages,so faster processing time.
26th Oct 2020, 4:10 AM
Aditya rout
Aditya rout - avatar
0
Maxwell Anderson what i wrote is very clear to a beginer.
26th Oct 2020, 1:10 PM
Aditya rout
Aditya rout - avatar
- 2
Hello, As per the my opinion The kernel really need to be written in C and Assembly. It is required that the boot and some parts of the kernel are written in Assembly because you need that fine control which is unobtainable elsewhere. C is used for the rest because it's actually standardized as compared to Assembly. You will completely rewrite Assembly when moving from 80x86 to ARM. C++ typically is impractical for use in kernel programming. It's footprint is too large; it's too slow. It's like calling a giant freight truck to pull a toy car. It's got everything required, but it way too much for the job! It's inefficient. You rarely need to use OOP in kernel programming. When writing a kernel, you want to write it as simple as possible! Try reading the Linux kernel source! It's super easy! Linux is so efficient, fast and robust because of it's simplicity. Even if you removed all comments and docs from the Linux code, it would be understandable for a beginner kernel programmer. Typically, you only want to use C++ for anything after the kernel. The kernel is that red zone where you need to make sure nothing goes wrong! Not just that, but you don't have any library support anywhere! I would pick C/C++ because they are close to the metal, and in general, the core of most OS out there are programmed using Assembly and C/C++. I have never tried to do such task myself, I think that you need to be aware and have knowledge about a lot of things in order to create an OS I hope this will help you
29th Oct 2020, 4:01 AM
Ishan Shah
Ishan Shah - avatar