1
$\begingroup$

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?

$\endgroup$

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
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ This Exchange Site is for Computer Science questions, please ask questions that are on-topic for this site. $\endgroup$ – Spencer Wieczorek Oct 13 '15 at 4:49
  • 3
    $\begingroup$ @SpencerWieczorek I think the question is close enough to being on-topic, since it doesn't seem very important that it's the C library rather than any other language. How libraries and operating systems interact in general is on-topic, I think. $\endgroup$ – David Richerby Oct 13 '15 at 7:20
  • 2
    $\begingroup$ Yes, it's the OS that interacts with hardware to do IO. But I can't work out what the rest of your question is asking. Could you try to expand it and make it clearer? $\endgroup$ – David Richerby Oct 13 '15 at 7:22
3
$\begingroup$

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 read(3) and 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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.