程序如何执行?操作系统在哪里发挥作用? [英] How does program execute? Where does the Operating Systems come into play?

查看:127
本文介绍了程序如何执行?操作系统在哪里发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序从某种语言编译为ASM->机器代码(可直接执行).当人们说这与平台有关时,意味着形成的二进制文件只能(正确)在具有相同指令集架构(如x86,x86-64)的CPU上运行.由于ISA的不同,它可能(错误地)/可能(根本)不能在其他进程上运行.对吧?

A program is compiled from some language to ASM --> Machine Code (directly executable). When people say that this is platform dependent, the mean that the binaries formed will run (correctly) only on the CPUs with same Instruction Set Architecture like x86, x86-64. It may (incorrectly) / may not (at all) run on other processes because of the difference in ISA. Right?

现在,二进制文件的概念使我感到困惑.一切都与机器语言代码"和中央处理器".操作系统在哪里发挥作用?我的意思是说,编译后的二进制文件在加载到内存后具有直接的CPU指令. & CPU一次执行一条指令.除了在进程管理中,我什么地方都看不到操作系统的作用.链接文字.无论操作系统如何,它都应在相同ISA的CPU上运行.正确的?

Now, the concept of binaries is confusing me. Everything is about the "Machine Language Code" & "CPU". Where does the OS come into play? I mean the compiled binary has direct instructions for CPU when it is loaded into memory. & CPU executes one instruction at a time. I couldn't see the role of Operating System any where except in process management link text . It should be running on the CPU of same ISA irrespective of Operating System. right?

但事实并非如此.如果我在Windows机器上构建到x86的代码.它不能在Mac x86机器或Linux x86机器上运行.

Yet its not the case. If I build a code to x86 on windows machine. It won't run on Mac x86 machine or Linux x86 machine.

我在这里丢失了一些东西.请清除我的困惑.

I'm missing something here. Please clear my confusion.

推荐答案

对于初学者来说,现代CPU具有(至少)两种模式,一种运行于操作系统本身核心的模式(内核模式").以及运行程序的模式(用户模式").在用户模式下,CPU不能做很多事情.

For starters, a modern CPU has (at least) two modes, a mode in which it's running the core of the Operating System itself ("kernel mode") and a mode in which it's running programs ("user mode"). When in user mode, the CPU can't do a whole lot of things.

例如,通常在内核中而不是用户模式下会注意到鼠标单击.但是,操作系统会将事件分派到用户模式,然后从那里分派到正确的程序.另一种方法也需要合作:程序无法自由绘制到屏幕上,而是需要通过OS和内核模式来绘制.

For instance, a mouse click is typically noticed in the kernel, not user mode. However, the OS dispatches the event to user mode and from there to the correct program. The other way around also requires cooperation: a program can't draw to the screen freely, but needs to go through the OS and kernel mode to draw on its part.

类似地,启动程序的动作通常是合作.操作系统的外壳部分也是用户模式程序.它会获得您的鼠标单击,并确定这是旨在启动进程的鼠标单击.然后,外壳程序告诉操作系统的内核模式部分为该程序启动一个新进程.

Similarly, the act of starting a program is typically a cooperation. The shell part of the OS is a user-mode program too. It gets your mouse click, and determines that it's a mouse click intended to start a process. The shell then tells the kernel-mode part of the OS to start a new process for that program.

当内核模式需要启动新进程时,它首先分配用于簿记的内存,然后继续加载程序.这涉及从二进制文件中检索指令,还需要将程序连接到OS.这通常需要找到二进制文件的入口点(通常为int main(int argc, char** argv))以及程序要调用OS的所有点.

When the kernel mode needs to start a new process, it first allocates memory for bookkeeping, and then proceeds to load the program. This involves retrieving the instructions from the binary, but also hooking up the program to the OS. This usually requires finding the entry point (classically int main(int argc, char** argv)) of the binary, and all points where the program wants to call the OS.

不同的操作系统使用不同的方法来将程序与OS挂钩.结果,加载过程会有所不同,二进制文件的文件格式也可能会有所不同.这不是绝对的;二进制文件的ELF格式用于许多操作系统,Microsoft在其所有当前操作系统上使用其PE格式.在这两种情况下,格式都确实描述了二进制文件的精确格式,因此OS可以决定是否可以将程序连接到OS.例如,如果它是Win32二进制文件,它将采用PE格式,因此Linux不会加载该文件,Windows 2000以及Windows 7-64都不会加载.另一方面,Win64二进制文件也是PE格式,但是Windows 2000会拒绝它.

Different Operating Systems use different ways to hook up programs with the OS. As a result, the loading process differs, and the file formats for binaries can differ too. It's not absolute; the ELF format for binaries is used for a number of Operating Systems, and Microsoft uses its PE format on all its current Operating Systems. In both cases, the format does describe the precise format of the binary, so the OS can decide whether the program can be hooked up to the OS. For instance, if it's a Win32 binary, it will be in the PE format, therefore Linux won't load that, Windows 2000 will, as will Windows 7-64. A Win64 binary on the other hand is in PE format too, but Windows 2000 will reject it.

这篇关于程序如何执行?操作系统在哪里发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆