Click here to Skip to main content
13,003,370 members (56,396 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Someone tells me that we can write OS with c#, is it possible? what is your opinion? if it can , how?
Posted 28-Jul-12 0:47am
aspnet_regiis 28-Jul-12 6:49am
Well...Bill Gates can answer your question certainly
Rate this: bad
Please Sign up or sign in to vote.

Solution 8

Yes, it is possible to build an operating system in C#. However, authoring performant kernels in managed languages is an open and 'unsolved' research area, with many challenges. In addition, the standard microsoft C# .NET compiler tools are not sufficient for such a task.

Microsoft built the Singularity kernel using a variant of C#. MOSA and Cosmos are hobby projects building operating system kernels in C#.

C# can be "ahead of time" compiled to machine code, just like C/C++. However, one would need to author a custom compiler to control the output format and runtime dependencies of this code. MOSA and Cosmos both have custom MSIL to native code compilers for this purpose. The open source Mono compiler also has AOT capabilities and could be modified for such a purpose.

The C# runtime is more extensive than the C/C++ runtime, but both require runtime support. One of the biggest differences and complexities in a C# runtime is the garbage collector. The vast majority of kernels do not use garbage collectors. Garbage collection "stop the world" pauses are a significant challenge to overcome for an OS kernel. The Azul C4 Pauseless Collector may be a possible solution, but is still very new technology.

So yes, you can write an operating system (kernel) in C#, and yes, it's already been done before. However, these are research systems, with tradeoffs, compromises, and experimentation. It is not clear whether or not you could write, for example, a version of a traditional macrokernel like Linux or Windows-kernel in C# and get similar performance results -- it has never been done before.
aspnet_regiis 28-Jul-12 16:57pm
+5 for the research... Good one
pasztorpisti 28-Jul-12 20:25pm
+5 Currently C++ vs C# seems to me like a balance between performance and safety/less bugs. Maybe this will change in the future for the benefit of C#...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Not strictly, no. You can write an operating system, but C# depends on the .NET framework in a way that C and C++ do not. And the .NET framework relies on Windows (or Linux/Android in the case of Mono) in order to work.

So the real answer is no, you can't write a "true" operating system directly in C#. You can write something that sits on top of an existing operating system, but not the "Full Monty" OS itself.
aspnet_regiis 28-Jul-12 6:57am
I completely agree.... +5 ....
aspnet_regiis 28-Jul-12 6:58am
If C# can be compiled to assembly language of the processor, then we can write a OS in C#.. But currently C# can be compiled to MSIL which can be understood by the .NET framework only ( which sits on the top of OS)..

OriginalGriff...Correct me if I am wrong...
OriginalGriff 28-Jul-12 7:23am
Yes - and the MSIL is part of the .NET framework (along with a gazillion other things).
To be honest, if you are compiling C# to native, then you either need to compile the whole .NET framework to native as well, and provide all the OS stuff that it expects, or you are much, much better off using C or C++ in the first place! C# is a very good language, but it relies on so much support stuff that it isn't really practical to use it without the infrastructure. For example, C# just won't work without the Garbage Collector, because it has no other way to dispose of items when you are finished with them. C and C++ have malloc and free for this.
aspnet_regiis 28-Jul-12 7:31am
Very clear explanation ..!!!
ErnestoNet 28-Jul-12 10:47am
Right. +5.

I don't know if singularity is written in C#....

Also, tinyOS:
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Current layering:
1. Platform (hardware, bios, ...)
2. OS (windows or linux written in C/C++ and in minimal amount of assembly)
3. .Net (probably written in C/C++)
4. Your .Net code that is a windows program

I can imagine something like:
1. Platform (hardware, bios, ...)
2. .Net (lets say you write it in C/C++ plus some platform dependent assembly)
3. OS written in .Net
4. Programs for your OS in whatever format written in whatever language

The .Net platform is currently implemented based on the OS. It would be totally possible to build up a .Net environment on top of the hardware/bios functionality as a base for your OS code but it is not a small piece of work. I think the reason for not doing this is performance related, C and C++ give you much more freedom to manage your resources, these languages are "high level assembly" languages. :D Still, could write and start a second OS (inside windows) on top of the current .Net implementation (the same way you would do it on .Net that is built on top of the hardware) as well If you insist on this, it could load your own executable format etc... It can be fun if you have a lot of time to kill :D

You can put arbitrary layering on top of the hardware, what you can not change is just the hardware and its assembly language. You can find out your own languages, your own OS, your own compilers and your own executable formats...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I completely agree, you can not, but you can write most of part on C#. Take a look on Singularity
aspnet_regiis 28-Jul-12 7:15am
Please correct the link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170626.1 | Last Updated 28 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100