how does an operating system that can run on many different processors execute the program?
when we make an app for certain OS, it gets compiled into machine code for that specific OS, but since an OS can run on different processors, does it mean the OS further compiles it down to the machine language that the installed processor understands?
7/21/2021 3:18:47 PMAndy_Roid
13 AnswersNew Answer
No the OS doesn't really have a role in compilation. Most modern "mainline/mainstream" CPUs can understand the same instructions which is why you can install different operating systems without swapping the CPU. For more obscure or "different use" (like chips for electronics/appliances) chips, different compilers exist and should be used Now the same compiler, but made for different OSs, is another story since different OSs are built differently and handle executables differently (from threads to security to gui libs and runtimes and even different I/O handling). It's a pretty big topic if you're interested but too big for answering this question
You're welcome! Sololearn was offline for me sorry for late response Well I'm not an embedded systems professional sorry So you're sending a program to the MC's memory? If I understood that correctly it should already be compiled but yes the executable should be compiled in a way that the MC's hardware will understand which would require a compiler specific to that architecture I think most MC's don't have vms since the vm itself requires a lot of resources and most MC's don't have much to just spare. Let's say one does have a vm. It could depend on the manufacturer as well but generally, language vms just act as a basic sandboxed computer. You can imagine this being a hefty task which is the reason why languages with VMs are said to be slower than others and why they shouldn't be used with cheap embedded systems projects.
Programs that compile to native binary format are processor and os specific. A program compiled for x64 Linux will not execute on x64 Windows (different os). Similarly a program compiled for x64 Linux will not execute on ARM-Linux (different processor). Compilers are designed to target a specific processor and os combination. A compiler that generates programs for the same system it is running on is known as a "native compiler", or simply a "compiler". A compiler that generates programs for a different system than the compiler is running on is known as a "cross compiler". The compiler used to generate Arduino programs is a cross compiler since it runs on a Linux or Windows system (the host system) but generates code for AVR microcontrollers without an operating system (the target system). The Java compiler (javac) creates programs that run on the Java Virtual Machine (JVM). Such programs will run on any system with a compatible JVM.
I think you have a wrong understanding of what role the OS has in a computer system. It doesn't play ANY role in compiling code into machine language, other than supporting the compiler than runs on top of it. An OS"s basic function is to simply manage the resources available to the system: the cpu, memory, I/O devices, and external and internal drives. What does the compiling is the software that resides on top of the OS, namely the compiler and other programs that support it, like the scheduler routines and such in the kernel. Compilers know, more or less, what they're compiling for, or at the very least, have settings that allow you to change what system they are compiling for. The most important thing they need to know is whether the machine code they produce will run on the hardware the code will be loaded into, which is largely dependent on the Instruction Set Architecture (ISA) of a system. Since many silicon companies have standardized how instructions are formatted, this isn't as common a problem as it once was. ARM and Intel's x86 and x64 architectures are the most common now. The OS does have a role in running the compiled machine code, which is why Windows Software doesn't usually run on linux, and vice versa. This isn't a ISA conflict, however, rather an internal difference between the OS's that can fubar how it reads the instructions. Hope that clears up some stuff for you.
If you compile a program for a microcontroller the program is compiled to native machine code. There are virtual machines available for microcontrollers, such as the Java Micro Edition (Java ME), though they typically require more resources and execute slower than native code. See: https://en.m.wikipedia.org/wiki/Java_Platform,_Micro_Edition Executable file formats of programs for operating systems also differ; the hex, bin, com, exe, coff, elf, or whatever, file format must comply with the application binary interface (ABI) of the system. See: https://en.m.wikipedia.org/wiki/Executable
Embedded systems vary a great deal. They can be as simple as a tiny microcontroller with only 2KB of Flash, 128 Bytes of RAM, and with a clock speed measured in KHz or they can be complex, powerful, GHz clocked, multi cored RISC processor powered system on chip single board computers running a real time operating system. How you go about writing a program and getting it into the system is going to depend on the design of the system.
Thanks a lot Martin Taylor i really appreciate all the infos provided by u😊
「ＨＡＰＰＹ ＴＯ ＨＥＬＰ」 thanks for the answer, this things are really confusing. i want to learn embedded system, i have many questions regarding it. if we download a program in a microcontroller, do we directly compile it into a machine language understandable by the microcontroller? how do languages that require a virtual machine to run work, run on small microcontrollers?
Well you see, languages like c/c++ are cpu dependent, meaning if you want to run programs written in c for other cpus,you have to get the respective compiler for that cpu but with java for instance, when source code is compiled,its converted into bytecode that is not cpu dependant but needs an interpreter or JVM to run,so any machine with an interpreter can run source code without compiling
Hacker029 what if the user somehow dont have JVM, do we send the JVM package along with the app?
Hacker029, who told you a JVM is crucial for a system? They most certainly are not crucial.
LoneWolf, If a user does not have a JVM installed then that user is required to install the Java Runtime Edition (JRE) for their system. The JRE not only contains the JVM but the necessary support libraries and other tools. Some complex programs use an installer which includes a JRE. The Arduino IDE is such a package. The IDE uses its own version of the JRE to avoid version conflicts with the one that may, or may not, be installed on the user's system. For smaller programs this is usually overkill.
Yes,You can create an executable i.e .exe but its irrelevant, jvms are crucial for a system anyway, we run java almost everywhere these days