• C

Many OS in C, Why?

I observed on fact that many popular OSs are coded in C laguage. They are,
UNIX, LINUX --- C
Windows 95, 98, 2000, NT --- VC
What does C offer which make the programmers of OS to use it but not Pascal and other languages?
LVL 3
sumant032199Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

serp_mageCommented:
C is a *much* more efficient language in a few ways:
1.  It is both a high level and low level programming language... high level meaning
the code is closer to actual thought-stuff allowing complex data structures, etc.
Low level meaning closer to the assembly code that will then be assembled into machine language.  
2. Since ANSI standard C is not an object oriented programming language like Java or C++, a lot of overhead is side-stepped; e.g.1, managing of inheritance & polymorphism thru virtual function tables, etc.  e.g.2, no need for garbage collection like Java has to make sure all unused memory on the heap is reclaimed.  
3. The Low-level-ness of C includes it's pointer idiom, a very powerfull tool which other languages avoid due to many programmers f***ing pointers up.  But within an OS, pointers are *extensively* used for things such as message passing / interprocess communication (IPC), memory management,  etc.  This can be seen if you grep thru the linux source code located in the /usr directory.  Some java programmers may argue that Java provides references, which are basically pointers, but the programmer doesn't have the same level of control that he/she does in C.  For e.g., in C you can do pointer arithmetic, which is often necessary in OSs, since the second most important thing is speed (w/ 1st being consistency and correctness).  

Another thing you should note is that no OS is written fully in a high level language such as C or Pascal.  Assembly code is (to my knowledge) always used for the most critical portions of an OS, such as clock interrupt handling, since this happens many times a second.

Hope I've been thorough enough w/o inundating you w/ OS concepts.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alexoCommented:
In order to understand the choice to use C, look at the alternatives:

* Writing the whole OS in assembly.
Disadvantages: hard to write, hard to understand, very hard to maintain, nearly impossible to port.

* Writing the OS in Pascal or similar high-level language (standard, not some proprietary offshoot).
Disadvantages: No (or very limited) direct access to memory and hardware (including control of registers), No (or very limited) interface to assembly routines for time-critical operations, less efficient.

C provides a good compromise between the abstraction of a "true" high-level language (Pascal, etc.) and the speed of a "true" low-level language (Assembly).  It also allows the programer to "break the rules" of the language when it is deemed beneficial (using casts, etc.)

Of course, C++ is a better language for this task (provided you know the costs of the features you plan to use and chose them accordingly) but it was not available/stable/standardized at the time [some of] these OSes were written.  As far as I know, BeOS is written mostly in C++.

Does this answer your question?
0
robert_marquardtCommented:
C was created to implement UNIX so its first target was OS design. No wonder it fits perfectly.
Then many programmers learned UNIX/C and began to write their own OSes. What do you think they used?
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

scrapdogCommented:
Nowadays Pascal and C are equal in efficiency (in at least one form).  Proof:  Borland C++ Builder and Delphi both use the same backend.

Turbo Pascal and Turbo C have always been equal as well.

As far as standard Pascal (i.e. non-proprietary), no one uses that anyway.  If Pascal is ever used, it is done using TP or Delphi, which are just as efficient as their C counterparts.

C, however, has a bit of an advantage in low-level programming (it is less cumbersome for this purpose).
0
scrapdogCommented:
sumant, just because others haven't written an OS in Pascal, there is no reason why you, or anyone else, can't...
0
alexoCommented:
>> Nowadays Pascal and C are equal in efficiency (in at least one form).
IFAIR, Pascal supposed to check array bounds on each access (you can turn this off in Borland/Inprise but that's a proprietary extension).

Also, in Pascal ("standard" Pascal) you cannot have a pointer point anywhere in memory as you can in C.

>> If Pascal is ever used, it is done using TP or Delphi.
You don't write an OS in a languaage that only produces Win32 executables.
0
KangaRooCommented:
Tradition plays a role too, Unix usually comes standard with a C compiler.
0
alexoCommented:
>> Tradition plays a role too, Unix usually comes standard with a C compiler.

Availability too.  Every platform that is capable of running a high level language compiler has a C compiler written for it.
0
3rsrichardCommented:
Also, C is widely taught in colleges.
So most of the programmers who would be
likely to write an OS already know C.

0
scrapdogCommented:
>You don't write an OS in a languaage that only produces Win32 executables.

Doesn't mean it can't be done in theory.
0
alexoCommented:
>> Doesn't mean it can't be done in theory.

Possibly.  But is it worth the trouble?
0
scrapdogCommented:
>But is it worth the trouble?

Writing an OS that relies on Windows can never be any better than Windows, so of course the answer is no.
0
alexoCommented:
Anyway, the fact is that many OSes were witten (at least partially) in C.

The reason was: C was designed as a language to write an OS (UNIX).

Also:

* C makes several low-level operations easy and fast.

* Due to the number of C compilers (and the resulting competition), they offered pretty decent optimizations and thus were (mostly) more efficient than other high-level languages.

* At the time of the OS'es writing, C was the most widely available language, with a published standard, cross-platform compatibility and "spoken" by lots of programers.
0
Ready4DisCommented:
C is the most popular language with the simlest Inline Assembly code.  Easy to make fast and quickly.  Much simpler to do interrupts.  Very profficient language especially when you put in inline asm code.  The loader, as it's called, is written in assembly.  The loader can be a maximum of 510 bytes.  This doesn't leave much room.  Try to write any program in C that compiles into a com file under 510 bytes.  Not very simple is it.  Basically all the loader has to do is take your main kernel and load it into memory, then jump to its  starting location.  The kernel is of course going to be written in a higher level language since it is not so limited.  C is very easy to use files with and C++ is even easier!  With on OS you are constantly dealing with files.  With Trubo Pascal, you can write an OS almost as easily as in C... but C is much more popular in markets, so most people tend to learn that.  You can write an OS in any language in theory.. even Quick Basic :).  But, I wouldn't recommend it.  Oh well, have fun.
0
alexoCommented:
>> You can write an OS in any language in theory..
not so.
0
Ready4DisCommented:
Ok, then which one can't you?  Dude, if you really wanted to, you could write an OS in a .bat file.  And run/compile it from there.  What language does not allow you to write an OS?  You can do it in VB, Java, C/C++, VC++, Basic, QBasic, Quick Basic, Delphi, Pascal, Assembly, Hex... what am I missing that you can't write it in?
0
daks2003Commented:
1. C is modular. See, Unix and Linux are OSs that is constantly updated. This will be very easy using a modular language only.

Pascal also is modular, so it can also be used. But as others have said there is some tradition also in it.(Initially Unix was writtern in C). Other Os devolopers also used it.

2. C can support high level and low level coding. So the Kernal part and the support commands can both be writtern using C. Instead of writing the Kernal in a low-level language and switching to high-level languages for applications.
0
Ready4DisCommented:
Heh... hey he just about took that whole answer from everyone else.  I don't see anything better in his answer than the original comments that where made.  I think the first person with the correct comment should get it.  In this case, Serp Mage was the first comment and said just about exactly what the guy trying to take credit said.  You should not accept his answer since he wasn't the first to say it.
0
alexoCommented:
> Ok, then which one can't you?
A lot of them.  I'll just give you several examples from your list.

VB - a VB program can only run on a windows platform (it needs the VB runtime DLLs) so, by definition it cannot be an OS since it depends on another OS to run (and to perform I/O, memory management, etc).

Basic - is in enterpreter.  'nuff said.

> hey he just about took that whole answer from everyone else.
daks2003 is a newbie here, he'll learn...
anyway, sumant seems to have abandoned the question.
0
Ready4DisCommented:
Actually... you can write an OS in basic first off.  Second off, you can write an OS in VB.  I wouldn't recommend it, but it can be done.  You can write a compiler in VB, which can compile to com files... with that, you can write your OS, and compile to the .com file, which will then be your OS.  In theory it is possible, just not recommended.  Same with QB.  I actually had a QB Interpreter that I used to write my OS loader which was a .com file.  With QB you can also directly access low-level stuff, Hex calls.  Using Call Absolute.  In theory an OS can be written by anything.  I made my QB program take some hex code, and output it into a new file in binary output, which could then be run.  But it was done using the interpreter.  Oh well, nuff said.  If you really have any questions on how this works, or any more input, let me know. :)
0
alexoCommented:
I happen to disagree with your conclusions.

Writing a wrapper for machine code in <language> to implement an OS does not constitute "writing an OS in <language>".

In order to qualify, I'd say that the OS source has to contain >90% pure <language> code.  It has been done in C, C++ and several other languages (FORTH?).  I doubt that it can be done in the likes of VB.

We seem to be drifting off-topic.  You're welcome to contact me by email to continue this discussion.
0
Ready4DisCommented:
Ok, my point was that it was possible to write an OS in any language, wether or not it's a good idea is a different story.  It's definetly possible, depends on your definition of writing.  You can put it all into one .bas file with no data files.  Which would mean that it is, in one way or another, completely QB code.  This also means that it was totally written in QB.  Oh well, I am done, either way you look at it, it can be correct, so this is really an endless battle.  Later.

One last thing.  Microsoft Used Visual C++ which was used to create windows apps to write windows 98... which means it can be done in that, and to some people... namely microsoft... it was worth it.  And I definetly think that the money they made off of it speaks for itself.  The entire OS is based on Internet Explorer which is a win32 app.
0
Ready4DisCommented:
>>  Writing an OS that relies on Windows can never be any better than Windows, so of course the answer is no.


Does this mean that writing an OS in Dos can never be better than dos?  Of course not, that would be a rediculous statement.  Besides, Just cause it's written in windows doesn't mean it relies on it.  You can re-write the entire windows kernel and have it perform much better and how you want it to, then the win32 kernel that was compiled in VC / VB could run better than windows itself.  Is it worth it... depends on how much time you have, and what kind of copyright laws you might infringe upon.
0
KangaRooCommented:
>> entire OS is based on Internet Explorer

Where did you get that from? The shell you are using to start applications is not the same as the OS it uses to do so.
0
Ready4DisCommented:
Ok... Lets see.  The Desktop is based on IE and is can even be setup to be viewed as a web-page.  I would like to see you get rid of IE on your computer and have your OS operate!  IE is completely integrated into the windows 98 OS and windows 98 is now dependednt on it.  It uses the basic functions of IE to do most of the displaying of forms and inputs now, since they where incorporated into web-pages a while ago, windows decided to use them in their OS.  Ex:  Input boxes, pulldown menus.  These 2 things are exactly the same in IE and windows now, so rather than write it twice, microsoft wrote it once and used it in both.  Microsoft now makes it... almost... impossible to run windows without IE.  It has been done, but only with re-programming of all the major IE functions.
0
daks2003Commented:
daks2003 changed the proposed answer to a comment
0
KangaRooCommented:
Nonsense. Try a  
  shell=c:\command.com /p
or
  shell=c:\windows\progman.exe

in system.ini¹ and I bet you'll see the OS is very much alive without IE, you just don't have the fancy desktop.
Seems you are confusing appearance with contents. There is much, much more to an OS then meets the eye (device IO, file and memory management).
But I don't doubt that the windows explorer (the desktop) shares a lot of functionality with IE and why not², they are both file browsers.

¹ I've done this with Windows 95.
² Actually I hate that web page look in Windoze 98, but you can completely remove it.
0
alexoCommented:
0
serp_mageCommented:
Not to be rude, but why are points offered when there appears to be no intention of awarding them?  Seems kinda pointless to me (no pun intended).
They have things called newsgroups for discussions.
0
Ready4DisCommented:
Ok, I specifically said that it didn't they didn't integrate IE until windows 98... so totally get rid of it in windows 95, sure.  Now, go to someones house with Win 98 and try to uninstall IE completely.  Suggestion, bring a Windows 98 CD with OEM code once you completely screwed everything up.  Do you really think I would sit here and make this stuff up?  Anyone here have a computer class with a teacher whom is really knowledgable about computers in general, and not just programming?  If so, go ask them if windows 98 is basically run by IE.  I asked my teacher to see what he said, and he agreed... and he has his masters and Microsoft Certifications for just about everything.  Oh well, buh-bye for now.  That 98Lite thing is the thing I was talking about that people wrote so that you could run windows without IE.  Like I said before, it has been done but "only by re-writing the major IE functions".  Oh well.
0
alexoCommented:
I'm outta here
0
KangaRooCommented:
We are talking about the OS here, nor the shell that enables the user to run programs...
0
Ready4DisCommented:
Actually... we where talking about everything.  Windows is based on the whole IE file structure now too.. so it does have to do with how it loads/looks at files and folders.  Which is part, a major part I may add, of an OS.  And it happens to be the part that Microsoft decided to integrate with IE.  Forget it, we are fighting a lost cause.

Things we agree on.. and leave it at that:

IE is part of the Win98 OS.
Win98 OS isn't completely dependent on IE, since it's not the entire OS.
IE is used for most of the Graphical User Interfacing (Windows main purpose)
Win98 Can be run without IE with special software.
IE is integrated, but nobody can truley say to what extent unless...

You helped write/have access to the Win98 code.
0
KangaRooCommented:
Seems like a fine conclusion to me. Thanx
0
SunBowCommented:
Windows wasn't C to begin with, wasn't it originally Pascal? How about PL/I?
C does best at low-level handling (forget the plus) which makes for better speed.  Usually parts of good OS remain in machine code (why not assembly?).
C is better at portability and scalability. but less portable than you might think.
There was an enhanced Pascal for building OSs, what became of it I do not know. It was so slick, maybe innefficient.  What happened to ADA?

I believe that early on, mfr's held closer to standard for C than the alternatives.  I recall many portability issues moving many apps from one platform to another - (to semi-colon, or not to semi-colon, why is that still a question?)

If sumant is not absent, my answer of day shall be:

1) Cross-platform compatibility.

There are other advantages, but since C had fewer corrections/adjustments needed when moving it around, that's my bet for popularity. whilte slower than machine code, easier to keep up and maintain. Faster than the pack for the most part.
0
Lab_RatCommented:
You can emulate an OS from almost any language...
0
sumant032199Author Commented:
Ohh! I am sorry. It is late.
But I got very valuable information during this discussion.
And you can see I was receiving comments on 17th Feb also.
The discussion inbetween is out of focus. Hence I am granting points
to serp_mage. Sorry serp_mage but I was reading comments regularly,
but I did not want to interrupt.

Anyway thanks a lot experts. You guys can continue discussion.
0
sumant032199Author Commented:
Ohh! I am sorry. It is late.
But I got very much valuable information during this discussion.
And you can see I was receiving comments on 17th Feb also.
The discussion inbetween is out of focus. Hence I am granting points
to serp_mage. Sorry serp_mage but I was reading comments regularly,
but I did not want to interrupt.

Anyway thanks a lot experts. You guys can continue discussion.

I am getting unexpected error message when I clicked on "accept comment as answer."
Hence please serp_mage give an answer.
Then I can give points.
0
sumant032199Author Commented:
Neglect last comment, it is done.
0
serp_mageCommented:
Thank you sumant.  Sorry for being impatient.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.