I've been curious about how programs are able to interact with operating systems even when they're written in different programming languages. For instance, do operating systems have some kind of hidden feature, like a foreign function interface, to facilitate this? Or is it more about how compilers handle these interactions? Thanks in advance for your insights!
5 Answers
At a fundamental level, all a program needs to do to work with the OS is set up the processor registers properly and execute system call instructions. This means you can use just about any programming language to interact with the OS, provided it can manage those low-level details.
When you're running a program, no matter the language, it gets compiled into machine code that the OS can execute. For instance, on Windows, your executable starts at a function called "WinMain". The compiler puts some starter code in place to ensure everything runs smoothly, whether from C++ or another language.
Virtually all modern operating systems provide an interface that programs can interact with using C. Many languages have foreign function interfaces that enable them to call C functions. While there are exceptions, most programming interactions funnel through C compatibility.
That's changing a bit as well, especially with more systems moving from C to Rust for improved safety and concurrency.
The concept you’re seeking is known as an Application Binary Interface (ABI). It defines how programs communicate at the machine level, similar to how an API functions. Most operating systems utilize an ABI defined by C, which makes it a kind of standard for interaction.
All programming languages eventually translate down to machine code, which is how they effectively communicate with the operating system. So, it's really about that underlying machine code enabling the interaction, regardless of the higher-level language.
Exactly! Plus, in Unix-like systems, there are also methods like sockets for inter-process communication, where programs can share data through files instead of just relying on code execution.

True! And to add on, language dependencies can also play a role. For example, regional settings might affect how numbers and dates are formatted when communicated between systems.