Does implementation of C libraries depend on OS?
Asked Answered
T

4

5

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?

Toluene answered 18/12, 2014 at 11:1 Comment(4)
This question might not be as precise as it should be, but I really don't know how to express it better. Kindly, forgive me.Toluene
Are you asking about the standard functions like printf() or malloc() or about thoose as CreateThread()?Lalonde
Libraries contain functions like printf(), etc. and they need system calls for the implementation of such functions.Toluene
Anyway, I don't get: Are you asking about thoose functions like printf which have to be served, but are implemented differen depending on enviroment? Or asking about functions which could but not have to behave the same way as they are not standarized?Lalonde
I
4

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().

Impart answered 18/12, 2014 at 11:11 Comment(5)
Thanks, I was hoping that it should be something like this, but I had very little knowledge about OSs.Toluene
"while on linux fopen() is the native function" You're wrong. Did you mean open()?Swarth
@black My bad, I forgot about open().Impart
pthread_create is more like _beginthreadex, actually: A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multithreaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.Underhung
@MaximYegorushkin You miss the point. _beginthreadex is also a wrapper for CreateThread. pthread_create equivalent in Windows is CreateThread.Impart
C
2

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!

Coacher answered 29/12, 2014 at 4:37 Comment(0)
L
1

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)

Lalonde answered 18/12, 2014 at 12:22 Comment(0)
M
1

Keep in mind that there are two types of library functions: utilities and system wrappers. Let's say you are a vendor trying to create a portable library.

Utility functions like sprintf and atoi are going to be the same on any implementation because they do not need OS system services.

Typically you would then have an abstraction layer in your library. You might have a function like

 void * getBytesFromOS (unsigned int count) ;

that allocates pages of memory. It would have different implementations for various systems. A malloc function using such an interface might be 99% the same across operating systems.

Mothy answered 19/12, 2014 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.