tags:

views:

1148

answers:

11
+3  Q: 

Why use C?

Why is C used for writing drivers and OS codes?

Is there a size problem?

Are there any drivers written in other languages?

Which language were XP, Vista, and Solaris written in?

+9  A: 

"why we are using C language for writing drivers and OS codes.?"

So that programmers won't have to learn new syntax of each new assembly language for each new kind of machine.

"Is there any drivers written in any other languages?"

Historically, assembly languages. I don't remember if PL/S or BLISS could be used for drivers. Maybe B. In modern times, a few brave people use C++ but they have to be very careful. C++ can be used a bit more easily in user mode drivers in some situations.

Windows programmer
+1 for the mention of BLISS!
PTBNL
+9  A: 

Because C has the best combination of speed, low memory use, low-level access to the hardware, and popularity.

Most operating systems have a kernel written in C, and applications on top of that written in either C, C++, C# or Obj-C

cloudhead
To the best of my knowledge, at least some assembly is required for the lowest level of an OS kernel. Not being an OS writer though, I could be wrong about that.
Just the bootstrapper I think
John T
+5  A: 

C was one of the very first languages (that wasn't assembly) that was suitable for writing operating systems, so it caught on early. While other languages have appeared since that are also suitable for writing operating systems in, C has remained popular perhaps due to its long history and the familiarity programmers have with its structure and syntax.

Eddie
+16  A: 

C compiles down to machine code and doesn't require any runtime support for the language itself. This means that it's possible to write code that can run before things like filesystems, virtual memory, processes, and anything else but registers and RAM exist.

Theran
While this can be true to a certain extent, you have to be careful of the language features you use, because if I'm not mistaken, floating point operations, conversions at least, still need the C runtime library. In saying that though, unlike many other languages, the C runtime's code can be embedded within the application itself.The thing so many people have overlooked is that a loader essentially must be written in assembler, unless you have a C compiler that will compile a suitable boot image for a computer.
@webboy42, all you need is a couple of instructions sitting on the reset vector to jmp to the start of your C code if its already in memory, like on a microcontroller or in BIOS. Such a small amount of machine code can be built up by hand one bit at a time, no need for assembler :)
Theran
Modern architectures don't need runtime libs for floating point conversions either btw; it's a call to eg the fistp opcode.
Crashworks
There are more languages that compile to pure machine code. There is nothing stopping you from writing an OS in, for example, Pascal.
elmuerte
"C complies down to machine code and doesn't require any runtime support for the language itself." That's a feature of the compiler, not of the language.
Jason
@Jason for a question as basic as this, the distinction may not be so important.
Rex M
At this point, I'm not sure that matters. With cross compilers you can re-compile a system to work on any architecture. Once you consider that, compiling a runtime might actually be better/more reliable than porting and recompiling 1000 apps. I think it has more to do with inertia and the fact that most OS-centric engineers I know of aren't really open to changing something that big.
Bill K
There are numerous things preventing you from writing an OS in Pascal. To wit': http://www.lysator.liu.se/c/bwk-on-pascal.html
Crashworks
+6  A: 

C is by far the easiest language(other than assembly) to "get going" on bare bones hardware. With C, (assuming you have a 32bit bootloader such as GRUB to do the hard mode switching) all you must do is make a little crt0.asm file that sets up the stack and that's it(you get the language, not including libc). With C++ you must worry about dynamic casts, exceptions, global constructors, overriding new, etc etc.. With C# you must port the .Net runtime(which on it's own basically requires a kernel) and I'm not sure about Obj-C, but Im sure it has some requirements also...

C is simply the easiest language to use for drivers. Not only is it easy to get started with, but also it's easy to know exactly what happens at the machine level. Their is no operator overloading to confuse you and such. Sure it's handy in a "good" environment, but in Ring 0 where a bad pointer not only crashes your application, but usually results in a triple fault(reboot), blue screen, or kernel panic. You really like knowing what goes on in your machine..

Earlz
+7  A: 

Lisp machines had their operating systems written in Lisp, which shows that you don't have to use C or assembly. The Lisp machine business was destroyed by the availability of cheap PCs, whose operating systems were of course written in C and assembly.

Jouni K. Seppänen
A: 

As another note for machines that have drivers in other languages, there is the SunSpot robotics platform. Drivers for devices that are connected (sensors, motors, and everything else that can communicate via the I/O pins) are written in Java by the user.

Matthew Curry
+10  A: 

In safety-critical environments (think avionics, spacecraft, medical devices, transportation, control software for process control), systems (as well as drivers) are often written using Ada or even SPARK/Ada.

To clarify: C is usually understood to be fairly low level, and pretty much like a "macro language" for assembly itself, that's also where its power is coming from (speed, size, portability).

Ada, on the other, hand has been specifically designed for safety-critical applications with verifiability in mind, to quote Ada 2005 for Mission-Critical Systems:

Ada [9] is the language of choice for many critical systems due to its careful design and the existence of clear guidelines for building high integrity systems [10]

That's also where Ada's support for strong typing comes in, as well as a number of other important features (quoting design for safety):

Programming languages differ wildly in their appropriateness for use in safetyrelated systems. Carré et al. identified six factors that influence the suitability of a language for high-integrity applications [Carré 1990]. These are:

  • Logical soundness
  • Complexity of definition
  • Expressive power
  • Security
  • Verifiability
  • Bounded time and space constraints

No standard programming language performs well in all these areas although some (such as Pascal and Ada) perform much better than languages such as C or C++. In highly critical applications ‘verifiability’ is of great importance. Certain languages allow powerful software verification tools to be used to perform a wide range of static tests on the code to detect a range of programming errors. [...] An important issue in the selection of a programming language is the quality of the available compilers and other tools. For certain languages validated compilers are available. While not guaranteeing perfection, validation greatly increasing our confidence in a tool. Unfortunately, validated compilers are only available for a limited number of languages, such as Ada and Pascal. In addition to compilers, developers of critical systems will make use of a range of other tools such as static code analysis packages. The static tests that can be performed on a piece of code vary greatly depending on the language used. To aid this process it is common to restrict the features that are used within certain languages to a ‘safe subset’ of the language. Well structured and defined languages such as subsets of Ada, Pascal and Modula-2 allow a great many tests to be performed such as data flow analysis, data use analysis, information flow analysis and range checking. Unfortunately many of these tests cannot be performed on languages such as C and C++ .

It would be really beyond the scope of this question to go into even more detail, but you may want to check out some of the following pointers:

If anyone wants to look into Ada some more, check out this: Ada Programming (wikibooks)

There are even programming languages that are specifically developed for highly critical applications, such as JOVIAL or HAL/S, the latter of which is used by the space shuttle program.

Is there any drivers written in any other languages?

I have seen some Linux drivers for special hardware being written in Ada, don't know about other operating systems though. However, such drivers usually end up wrapping the the C API.

none
Care to explain why is Ada chosen over C for that? It sounds very interesting.
Milan Babuškov
I have reworked the answer to go into more detail and to provide further points, hope this helps
none
+2  A: 

Remember that C was originally developed for writing operating systems (in this case - Unix) and similar low-level stuff. It is wery close to the system architecture and does not contain any extra features that we want to control, how they exactly work. However, please note that the rest of the operating system, including the programming libraries, does not have to be written in the same language, as the kernel. The kernel functions are provided through a system of interrupts and in fact such programming libraries can be written in any language that supports assembler snippets.

The most popular operating system nowadays are written in C: Windows, Linux and many other Unix clones, however this is not the rule. There are some object-oriented operating systems, where both the kernel and the programming interface are written in an objective language, such as:

  • NeXTSTEP - Objective-C
  • BeOS - C++
  • Syllable - C++

See: Object-oriented operating system on Wikipedia

Note that in Linux, it is possible to write kernel drivers in the languages other than C (however, it is not recommended). Anyway, everything becomes a machine code when it comes to running it.

Zyx
+2  A: 

C is also a language that teaches a lot about memory management and is low-level enough to show the barrier between hardware and software. This is something that is rare among many methodologies today, that have grown more towards abstraction way above anything at the hardware level. I find C is a great way to learn these things, while being able to write speedy code at the same time.

mduvall
+1  A: 

"C compiles down to machine code and doesn't require any runtime support for the language itself."

This is the best feature of C

Arabcoder