Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Someone tells me that we can write OS with c#, is it possible? what is your opinion? if it can , how?
Posted
Comments
bbirajdar 28-Jul-12 6:49am    
Well...Bill Gates can answer your question certainly

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.
 
Share this answer
 
v4
Comments
bbirajdar 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#...
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.
 
Share this answer
 
Comments
bbirajdar 28-Jul-12 6:57am    
I completely agree.... +5 ....
bbirajdar 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.
bbirajdar 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#....
http://geekswithblogs.net/Podwysocki/archive/2008/03/11/120477.aspx
http://singularity.codeplex.com/

Also, tinyOS:
http://www.hanselman.com/blog/TinyOSBlastFromThePast.aspx
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...
 
Share this answer
 
v2
I completely agree, you can not, but you can write most of part on C#. Take a look on Singularity
 
Share this answer
 
v3
Comments
bbirajdar 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900