编译的程序如何与操作系统交互?

时间:2023-01-02 22:53:49

When a program is compiled it is converted to machine code which can be "understood" by the machine. How does this machine code interact with the operating system in order to do things like getting input from the keyboard ? To me, it seems that the machine code should run at a lower level than the operating system and therefore, I can't understand how the OS can act as an intermediary between the compiled application and the hardware.

编译程序时,它将转换为机器可以“理解”的机器代码。这个机器代码如何与操作系统交互以执行从键盘输入的操作?对我来说,似乎机器代码应该在比操作系统更低的级别运行,因此,我无法理解操作系统如何充当编译应用程序和硬件之间的中介。

PS : I just started C ++ programming and I am trying to understand how cin and cout work

PS:我刚刚开始进行C ++编程,我正在努力理解cin和cout是如何工作的

2 个解决方案

#1


24  

This is a very good question (better than you know), and there is a lot to learn. A LOT.

这是一个非常好的问题(比你知道的要好),还有很多东西需要学习。很多。

I'll try to keep it short. The operating system acts as a level of abstraction between software and hardware:

我会尽量保持简短。操作系统充当软件和硬件之间的抽象层次:

   Software
       .
      /|\    
       |   communicates with
      \|/
       '
Operating System
       .
      /|\    
       |   communicates with
      \|/
       '
   Hardware

The OS communicates with the hardware through programs called drivers (widely used term), and the OS communicates with software through procedures called system calls (not-so-widely used term).

OS通过称为驱动程序(广泛使用的术语)的程序与硬件通信,并且OS通过称为系统调用的程序(不太广泛使用的术语)与软件通信。

Essentially, when you make a system call, you are leaving your program and entering code of the operating system. System calls are the only way programmers are allowed to communicate with resources.

基本上,当您进行系统调用时,您将离开程序并输入操作系统的代码。系统调用是允许程序员与资源通信的唯一方式。


Now I would stop there, but you also said:

现在我会停在那里,但你也说:

To me, it seems that the machine code should run at a lower level than the operating system and therefore, I can't understand how the OS can act as an intermediary between the compiled application and the hardware.

对我来说,似乎机器代码应该在比操作系统更低的级别运行,因此,我无法理解操作系统如何充当编译应用程序和硬件之间的中介。

This is tricky, but simple once you understand some basics.

一旦你理解了一些基础知识,这很棘手,但很简单。

First, all code is just machine code running on the CPU. No code is higher or lower than other code (with the exception of some commands that can only be run in a privileged kernel mode). So the question is, how can the OS possibly be in control even though it is relinquishing control of the CPU to the user?

首先,所有代码都只是在CPU上运行的机器代码。没有代码高于或低于其他代码(除了一些只能在特权内核模式下运行的命令)。所以问题是,即使它放弃了对用户的CPU控制,操作系统如何才能控制?

When code is running on a CPU, there is a concept called an interrupt. This is a signal sent to the CPU that causes the currently running code to stop and get switched out with another piece of code, called an interrupt handler.

当代码在CPU上运行时,有一个称为中断的概念。这是一个发送到CPU的信号,它导致当前运行的代码停止并被另一段代码(称为中断处理程序)切换出来。

Examples of interrupts include the keyboard, the mouse, and most importantly, the clock.

中断的示例包括键盘,鼠标,最重要的是时钟。

The clock interrupt is raised on a regular basis causes the operating system's clock interrupt handler to run. Within this clock interrupt handler is the operating system's code that examines what code is currently running determines what code needs to run next. This can be either more operating system code or more user code.

定时引发时钟中断会导致操作系统的时钟中断处理程序运行。在此时钟中,中断处理程序是操作系统的代码,用于检查当前正在运行的代码,以确定接下来需要运行的代码。这可以是更多操作系统代码或更多用户代码。

Because the clock is always ticking, and because the operating system always gets this periodic chance to run on the CPU, it is able to orchestrate everything within the computer, even though it runs using the same set of CPU commands as any normal program.

因为时钟总是在滴答作响,并且因为操作系统总能获得在CPU上运行的周期性机会,所以它能够协调计算机内的所有内容,即使它使用与任何普通程序相同的CPU命令集运行。

#2


5  

The operating system provides system calls that programs can call to get access to lower level services.

操作系统提供程序可以调用的系统调用以访问较低级别的服务。

Note that system calls are different from the system() function that you have probably used to execute external programs.

请注意,系统调用与您可能用于执行外部程序的system()函数不同。

System calls are used to do things like access files, communicate over the network, request heap memory, etc.

系统调用用于执行诸如访问文件,通过网络进行通信,请求堆内存等操作。

#1


24  

This is a very good question (better than you know), and there is a lot to learn. A LOT.

这是一个非常好的问题(比你知道的要好),还有很多东西需要学习。很多。

I'll try to keep it short. The operating system acts as a level of abstraction between software and hardware:

我会尽量保持简短。操作系统充当软件和硬件之间的抽象层次:

   Software
       .
      /|\    
       |   communicates with
      \|/
       '
Operating System
       .
      /|\    
       |   communicates with
      \|/
       '
   Hardware

The OS communicates with the hardware through programs called drivers (widely used term), and the OS communicates with software through procedures called system calls (not-so-widely used term).

OS通过称为驱动程序(广泛使用的术语)的程序与硬件通信,并且OS通过称为系统调用的程序(不太广泛使用的术语)与软件通信。

Essentially, when you make a system call, you are leaving your program and entering code of the operating system. System calls are the only way programmers are allowed to communicate with resources.

基本上,当您进行系统调用时,您将离开程序并输入操作系统的代码。系统调用是允许程序员与资源通信的唯一方式。


Now I would stop there, but you also said:

现在我会停在那里,但你也说:

To me, it seems that the machine code should run at a lower level than the operating system and therefore, I can't understand how the OS can act as an intermediary between the compiled application and the hardware.

对我来说,似乎机器代码应该在比操作系统更低的级别运行,因此,我无法理解操作系统如何充当编译应用程序和硬件之间的中介。

This is tricky, but simple once you understand some basics.

一旦你理解了一些基础知识,这很棘手,但很简单。

First, all code is just machine code running on the CPU. No code is higher or lower than other code (with the exception of some commands that can only be run in a privileged kernel mode). So the question is, how can the OS possibly be in control even though it is relinquishing control of the CPU to the user?

首先,所有代码都只是在CPU上运行的机器代码。没有代码高于或低于其他代码(除了一些只能在特权内核模式下运行的命令)。所以问题是,即使它放弃了对用户的CPU控制,操作系统如何才能控制?

When code is running on a CPU, there is a concept called an interrupt. This is a signal sent to the CPU that causes the currently running code to stop and get switched out with another piece of code, called an interrupt handler.

当代码在CPU上运行时,有一个称为中断的概念。这是一个发送到CPU的信号,它导致当前运行的代码停止并被另一段代码(称为中断处理程序)切换出来。

Examples of interrupts include the keyboard, the mouse, and most importantly, the clock.

中断的示例包括键盘,鼠标,最重要的是时钟。

The clock interrupt is raised on a regular basis causes the operating system's clock interrupt handler to run. Within this clock interrupt handler is the operating system's code that examines what code is currently running determines what code needs to run next. This can be either more operating system code or more user code.

定时引发时钟中断会导致操作系统的时钟中断处理程序运行。在此时钟中,中断处理程序是操作系统的代码,用于检查当前正在运行的代码,以确定接下来需要运行的代码。这可以是更多操作系统代码或更多用户代码。

Because the clock is always ticking, and because the operating system always gets this periodic chance to run on the CPU, it is able to orchestrate everything within the computer, even though it runs using the same set of CPU commands as any normal program.

因为时钟总是在滴答作响,并且因为操作系统总能获得在CPU上运行的周期性机会,所以它能够协调计算机内的所有内容,即使它使用与任何普通程序相同的CPU命令集运行。

#2


5  

The operating system provides system calls that programs can call to get access to lower level services.

操作系统提供程序可以调用的系统调用以访问较低级别的服务。

Note that system calls are different from the system() function that you have probably used to execute external programs.

请注意,系统调用与您可能用于执行外部程序的system()函数不同。

System calls are used to do things like access files, communicate over the network, request heap memory, etc.

系统调用用于执行诸如访问文件,通过网络进行通信,请求堆内存等操作。