August 29th, 2013, 01:35 PM
How operating systems are written?
This is a general Operating System doubt.
Operating system provides us with a set of instructions called system calls to communicate with it and inturn the OS communicates with the hardware.
So when we write a C program it first converts everything to system call and then OS converts it to assembly and machine language, right?
But then how is OS written? Since C is mostly used to write Operating Systems and the C compiler just understands system calls (I think I'm mistaken here) how is it possible?
August 29th, 2013, 02:17 PM
August 29th, 2013, 09:45 PM
I don't want to write an OS.
My doubt is - Since C and other languages are just made to communicate with the OS, how can OS be written in theses languages? Are OS written in assembly or higher languages?
August 30th, 2013, 12:34 AM
> Since C and other languages are just made to communicate with the OS
Well you're starting with a false premise.
The standard C library provides a standardised interface to the platform (which may be an OS). But you're completely free to ignore all that and write code for a bare machine.
> Are OS written in assembly or higher languages?
If you'd done any reading at all, you would have soon learnt (for example) that the vast majority of Linux is written in C, with a few special cases (such as early bootstrap) written in assembler.
August 30th, 2013, 03:09 AM
I knew most of Linux is written in C leaving some 1000 lines of code written in assembly. I was just making sure.
Originally Posted by salem
So you can write code for bare machine too? That clears most of my doubt, certainly I was in a misconception.
But in a paper I was reading that some part of OS is written in assembly because that cannot be written in C. What code cannot be written in C(Example)?
Also I want to check if I'm right on the following thing : -
Since user can write code for the bare machine, it can cause some damage to the machine if user does something wrong. For this reason I suppose, we have a mode bit i.e. who is running the process - kernel or user. Am I right?
August 30th, 2013, 03:59 AM
I may be incorrect on this point, but I believe typically it's not a case C being unable to perform a task, but rather it can be done more efficiently using direct assembler code.
Parts of a program which must be either extremely small, or extremely fast are written in assembler by hand rather than letting the compiler translate the C code.
Recycle your old CD's, don't just trash them
If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
August 30th, 2013, 12:21 PM
Easy, follow these instructions.
Originally Posted by Avichal
August 30th, 2013, 01:24 PM
> But in a paper I was reading that some part of OS is written in assembly because that cannot be written in C. What code cannot be written in C(Example)?
On modern processors (those that support protected operating systems like Linux), there are typically 2 classes of instructions.
Normal instructions can be used by anyone.
Privileged instructions can only be executed by code running in some kind of 'supervisor' mode. This is normally your operating system.
Examples of such instructions are those which communicate with I/O addresses (on an x86), those that deal with hardware interrupts, those that deal with setting up virtual memory pages, those that switch between user and supervisor mode, etc).
Accessing these instructions involves some assembler code, since the compiler is unlikely to be able to generate these instructions directly. To keep things nice and tidy, these are typically written as very short 'C' functions (which could be inlined) to maintain readability of the code (and performance through inlining).
August 30th, 2013, 03:59 PM
In other words, It's called the "Kernel"
Originally Posted by salem