Reputation: 799
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?
Upvotes: 5
Views: 1803
Reputation: 3120
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.
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!
Upvotes: 2
Reputation: 21627
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.
Upvotes: 1
Reputation: 6555
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)
Upvotes: 1
Reputation: 6145
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()
.
Upvotes: 4