Archived

This topic is now archived and is closed to further replies.

ehsen

c for operating system

Recommended Posts

Why C is used for operating system programing instead of C++. The difference between c and C++ is of one degree c++ has oop. I am learning c++(beginner). Please tell me what''s its reason.

Share this post


Link to post
Share on other sites
Legacy code.

Specious concerns about C++''s performance.

Legitimate -- though perfectly solvable -- concerns over such things as what does the exception-handling machinery look like and where do v-tables live in memory.

Share this post


Link to post
Share on other sites
quote:
Original post by ehsen
Why C is used for operating system programing instead of C++. The difference between c and C++ is of one degree c++ has oop.
I am learning c++(beginner). Please tell me what''s its reason.


Mainly because C is a very simple language, and perhaps even closer than C++ to the actual machine code.

You can compile flat C binaries in GCC by specifying some options and making your own C library (a few functions you have to code yourself). You can also make your own C compiler rather easily.



Looking for a serious game project?
www.xgameproject.com

Share this post


Link to post
Share on other sites
The main reason was that C++ was barely standardised until
recently and there were no fully compliant compilers (is there
a single fully compliant compiler today?).

Share this post


Link to post
Share on other sites
C has been around for over 30 years and there is good support for it on many platforms. Many other languages have support for interacting with C because it is so common.

So, information is often passed through a simple C interface to be able to communicate between different languages. What actually goes on behind that interface can change.

I'm not sure which operating system you are talking about and how you know what language it is written in but...

For example, the Microsoft Windows API (Application Programming Interface), is the interface defined so that you can write applications to use the windows platform. The API has been changing and being added to over the years, especially with new releases of the platform. The API is defined in C.

At the moment I would imagine that much of the internals of Windows are written in C++ but it is hidden behind a simple C API.

You can quite easily call the C API that defines Windows using Python, Java, Ruby, C++, VB etc. It is the lingua franca of programming.

Also, just to note, C++ isn't just about OOP (and it's hard to agree on a definition anyway) but it does support some OOP-like concepts.

[edited by - petewood on March 29, 2004 9:20:46 AM]

Share this post


Link to post
Share on other sites
Look on the Linux kernel mailing list, you will find various discussions.

One argument which is mentioned a lot is control over memory-management. OS writers want a lot of control over the way memory is allocated. C++ of course has a lot of things which do implicit memory allocation and moving, like copy constructors and such like.

OS designers (particularly in kernel mode code) want to know where every single allocation is, so that they can ensure that they don''t fall into traps.

I''m sure however, that with enough effort, and OS could be written in C++, even down to interrupt service routines.

The problem I believe is, a memory allocation could cause a page fault, and a page fault could cause the thread to sleep while it does some paging operation (for instance swapping out). If that was done while some kernel-mode lock was held, bad things would happen.

Also, exception handling and destructor-calling are apparently potentially unsafe inside OS code where you want more control over things.

Mark

Share this post


Link to post
Share on other sites
I see lots of arguments on both sides of this discussion, but its all pretty moot. C++ is used plenty in operating systems. However, if you are talking about in a kernel, then its because the maintainers are anal-retentive. C not require run-time? Then what the hell do you call the C Standard Library? I remember seeing something about a kernel written in C++, and google will probably find a link.

(http://www.ironfroggy.com/)
(http://www.ironfroggy.com/pinch)

Share this post


Link to post
Share on other sites
quote:
Original post by ironfroggy
I see lots of arguments on both sides of this discussion, but its all pretty moot. C++ is used plenty in operating systems. However, if you are talking about in a kernel, then its because the maintainers are anal-retentive. C not require run-time? Then what the hell do you call the C Standard Library? I remember seeing something about a kernel written in C++, and google will probably find a link.


Congratulations, you''ve just labeled yourself a fool.

C does not require a runtime library. For practical coding purposes, it''s useful to have one.

The real reasons for not using C++ are explained by markr. C++ has a lot of implicit memory work and stuff, and calling of various procedures, especially in classes, which can become a serious problem. A skilled C++ coder can avoid those issues, but in many cases it''s easier just to write in C. It''s also useful to do the lowest level of an OS in C for a couple reasons. There are versions of C that provide special interrupt routines, etc. which are extremely useful, especially if you don''t want to write assembly. And basically, everything C++ does in terms of OOP and all is essentially useless at the base level of the kernel.

Apart from all that, g++ is very slow at compiling C++. For large projects this gets very annoying (compare Gnome and KDE compile times, about a ratio of 2:9 on average). Since GCC is arguably the best cross-compiler in existence, it helps to use C and get your project compiled in a third of the time.

Share this post


Link to post
Share on other sites
I''m writing my own kernel in C++ and here is some info I have learned. Please note I''m just writing the kernel for experimental purposes so some issues like performance although I think about them, they are not top priority.

Like others have said. C requires no runtime. C++ does if you what RTTI, exceptions, new/new[] and delete/delete[].

Of course after you define your own new, etc.. you don''t have that problem. There is no standard way to implement exceptions and RTTI. Also with G++ you can disable the parts that require a runtime. Also with G++ you have to implement a function to use pure virtuals.. I don''t know if you have to do it with all compilers.

"Except for the new, delete, typeid, dynamic_cast, and throw operators and the try-block, individual C++ expressions and statements need no run-time support."(Supposedly from one of Bjarne Stroustrup''s books, taken from http://www.invalidsoftware.net/os/?the_id=11)

As for implicit memory copying and such. Yes, those are issues of course but I think if your writing a kernel in C++ then you should be grounded well enough in the language to be able to avoid those things when you want to.

Overall I think C++ is a good choice for kernel development but that''s for each kernel writer to decide for themselves.

Share this post


Link to post
Share on other sites
jyrix is correct.

It is entirely the runtime.

and also the fact that its not obvious what the machine code looks like from the code. virtual tables? what! that''s not in my struct! something like that is compiler dependent, and is just extra details, which make writing the kernel HARDER, not easier.

symbol mangling also sucks.

Share this post


Link to post
Share on other sites
C++ requires a run-time no more or less than C does.
Any decent compiler lets you disable RTTI and exceptions, and it''s neccessary to do so in more places than just a kernel or kernel-mode driver.

That said, when in Rome... I write my drivers in C.

Share this post


Link to post
Share on other sites
quote:
Original post by darookie
The main reason was that C++ was barely standardised until
recently and there were no fully compliant compilers (is there
a single fully compliant compiler today?).




I think Comeau C++ is. If it isn''t, it''s probably as close as you''ll get.

I also think this is a big reason. It''s much easier to write a C compiler than it is a C++ compiler, so you make a C compiler for your new platform. Now, you have a bunch of C code, and it''s just easier to stick with C.

Share this post


Link to post
Share on other sites