Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does implementation of C libraries depend on OS?

I'm just wondering that there are different functions in different OSs, but they serve the same purpose, or it can be said that different OSs have different system programming languages (like that of Windows vs that of UNIX).

So, for example, as C library consists implementation of functions, their implementation must call distinct functions (depending on OS), to implement the same thing. Is this correct? So, are libraries used in cygwin for compiling C program specially written for Windows, and that of gcc, especially for Linux? Am I correct? If not, why then?

like image 426
PalashV Avatar asked Dec 18 '14 11:12

PalashV


People also ask

How does C library work?

C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

Why we Cannot use C standard library function in kernel module?

Because the GNU C Library which you are familiar with is implemented for user mode, not kernel mode. The kernel cannot access a userspace API (which might invoke a syscall to the Linux kernel).

What is the purpose of libraries in C?

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

Does the kernel use glibc?

The kernel does not use glibc ever.


3 Answers

Yes that is correct. Different OS have different functions that do the same thing. For example, on Windows you create a thread by calling CreateThread(), while on linux you call pthread_create().

About the C runtime, all OS implement them, but differently. On Windows, fopen() is a wrapper that will call CreateFile(), while on linux fopen() is a wrapper for open().

Cygwin and the likes add libraries to implement linux-only function on Windows. For example, cygwin will implement pthread_create() on Windows, by wrapping CreateThread(), like MS did for fopen().

like image 50
ElderBug Avatar answered Oct 03 '22 20:10

ElderBug


Yes.

To add on to ElderBug's answer, the C libraries that act as wrappers for different types of system calls vary across systems. System calls, such as the following (from NYU's Operating Systems, Lecture #4), shift the process from user mode to supervisor/kernel mode.

Note the goal of differentiating between the user-mode (the wrappers) and the kernel mode (the OS' implementation), from the lecture:

An important OS objective is that simple procedure call semantics are observed from a user process viewpoint. The complexity is hidden inside the kernel itself, yet another example of the operating system providing a more abstract, i.e., simpler, virtual machine to the user processes.

enter image description here

As you know, these sample calls are not similar across different operating systems such as Windows and Linux, but the names of the C wrapper functions are --otherwise, the pre-compiled language itself would differ across systems.

Hope that helps!

like image 40
aralar Avatar answered Oct 03 '22 20:10

aralar


Yeah, you got it. There isn't much I can add.

But as far as I know the OS is serving the libraries and they get just linked. The reason for this is, the programmers who develop the system specific implementations know their own system best. Implementing an fopen() isn't just asking the Hard Disk for a lane to its stuff. (you probably know)

You have to respect many circumstances of other implementations which are working with File descriptors. And maybe you have to rely on something happening in a specific function on your OS what for the generell behaving isn't needed. But in your enviroment this keeps it all running.

Thats also why the C standard says changing the source code of standard librarys results in undefined behavior even if the function it self still serves the same behavior (tried to find the cite for you but wasn't able, sorry.)

So at all its a optimisation thing. There could be generell implementation but as mostly the whole OS is based on thoose implementations, every OS is interested in making them run the best for their own Case.

(probably not the only one but I'm not that deep in OS development as I could name another)

like image 33
dhein Avatar answered Oct 03 '22 19:10

dhein