#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    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?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    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?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Originally Posted by salem
    > 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.
    I knew most of Linux is written in C leaving some 1000 lines of code written in assembly. I was just making sure.

    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?

    Thank you!
  10. #6
  11. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,958
    Rep Power
    4035
    What code cannot be written in C(Example)?
    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
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    859
    Rep Power
    388
    Originally Posted by Avichal
    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?
    Easy, follow these instructions.
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > 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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    859
    Rep Power
    388
    Originally Posted by salem
    > 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).
    In other words, It's called the "Kernel"

IMN logo majestic logo threadwatch logo seochat tools logo