Does the implementation of C libraries depend on the OS?

I'm just curious that there are different functions in different OSs, but they fulfill the same goal, or we can say that different OSs have different system programming languages ​​(for example, Windows and UNIX).

So, for example, since the C library consists of the implementation of functions, their implementation must call different functions (depending on the OS), implement the same thing. It's right? So, the libraries used by cygwin to compile a C program specially written for Windows and the gcc library, especially for Linux? I'm right? If not, why?

+5
source share
4 answers

Yes, that's right. Different operating systems have different functions that do the same. For example, on Windows you create a thread by calling CreateThread() , and on linux you call pthread_create() .

About C runtime, all OS implements them, but in different ways. On Windows, fopen() is the shell that calls CreateFile() , and on linux fopen() is the shell for open() .

Cygwin and the like add libraries to implement only Linux functions on Windows. For example, cygwin will implement pthread_create() on Windows by wrapping CreateThread() , as MS did for fopen() .

+4
source

Yes.

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

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

An important goal of the OS is that, from the point of view of the user process, there is a simple semantics of the procedure call. Complexity is hidden inside the kernel itself, another example of an operating system that provides a more abstract, that is, a simpler virtual machine for user processes.

enter image description here

As you know, these call examples are not similar to each other in different operating systems, such as Windows and Linux, but the names of the C wrapper functions are different, otherwise the pre-compiled language will differ in different systems.

Hope this helps!

+2
source

Yes, you get the point. I can not add.

But as far as I know, the OS serves libraries, and they are simply connected. The reason for this is that programmers who develop system-specific implementations better know their own system. The implementation of fopen() is not just asking for a hard drive for the strip. (you probably know)

You must respect the many circumstances of other implementations that work with file descriptors. And perhaps you have to rely on something that happens in the specific function of your OS, which is not required for the behavior of generals. But in your environment it all works.

That's why the C standard says that changing the source code of standard libraries leads to undefined behavior, even if the function that it itself still performs the same behavior (tried to find a quote for you, but couldn’t, sorry.)

Thus, with all its optimization. The generator implementation can be implemented, but since basically the entire OS is based on the thoose implementation, each OS is interested in making them work best for their own case.

(maybe not the only one, but I'm not as deep in OS development as I could call another)

+1
source

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

Helper functions, such as sprintf and atoi, will be the same for any implementation because they do not need OS system services.

Usually you will have an abstraction layer in your library. You may have a function like

  void * getBytesFromOS (unsigned int count) ; 

which allocates pages of memory. It will have different implementations for different systems. The malloc function using this interface can be 99% the same for all operating systems.

+1
source

Source: https://habr.com/ru/post/1209441/


All Articles