I wonder, how is that operating systems make use of libraries as stdio.h if they themselves are the services? I am right? Is the operating system that interacts with the hardware to operations of I/O? How you write to a file system with the C library, for example?
closed as off-topic by Luke Mathieson, vonbrand, Ran G., Evil, Thomas Klimpel Oct 22 '15 at 7:31
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question does not appear to be about computer science, within the scope defined in the help center." – Luke Mathieson, Evil, Thomas Klimpel
The kernel of the system doesn't use the C library (at least not it's I/O facilities).
The kernel does offer system calls like
write(3) in Unix, and translates them into whatever magic the underlying hardware requires to do this. For example, as a very rough description of how this goes, to write to a serial port, the kernel copies the individual bytes to the serial port's register, signals that a new character is ready to transmit, and waits for it to be shipped off before placing the next one there. To write into a file the kernel keeps track of where the writing to the file has to take place, asks for space in the underlying device if the file will grow, and writes into the allocated space. Each filessystem has its own (rather complex) data structures on disk and in memory to keep track of free and allocated space, and so on. To manipulate it (and the underlying blocks on disk) usually another complex layer talking to the hardware is interposed.
Thus one way of looking at an operating system kernel is as a huge library offering nifty functions (system calls) that allow the programmer to access complex, even under the hood radically different functionalities, by a simple interface. That the programmer uses the convenience of the standard C library, some other language's library, or even chooses to write the system calls directly in assembler, is irrelevant.
The kernel does write messages on occasion. For this it has internal functions (in the case of e.g. Linux it is
printk that mimics
printf(3)) to do this by tickling the underlying hardware the right way.
The C language standard has provisions for hosted and freestanding runtime environments and makes clear which language features are required for each. Hosted environments are required to provide the standard C library, freestanding environments are not. Operating system kernels and embedded systems are typically built for freestanding environments and so do not use the standard libraries at all.
Note that the hosted vs. freestanding division relates primarily to libraries. As mentioned in this stackoverflow question macros defined in some header files are still usable in freestanding implementations.