c++有什么优势?编写设备驱动程序?

发布于 2024-10-11 15:27:31 字数 131 浏览 7 评论 0原文

据我所知,为了编写设备驱动程序,人们通常使用 C++ 还是汇编?装配的选择对我来说是明确的。但为什么是c++呢?我想用java(例如)或者其他高级语言也是可以的。那么为什么 C++ 如此普遍呢? C++ 编程语言是否有编写驱动程序所必需的特定功能?

As I know in order to write device drivers people usually use c++ or assembly? The choice of assembly is clear for me. But why c++? I guess it is possible to do in java (for example), or in other high level language as well. So why c++ is so common? Is there a specific feature of C++ programming language that is necessary for writing drivers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕梦 2024-10-18 15:27:31

我想说的是,C 更常用作设备驱动程序语言(相对于 C++)。

造成这种情况的原因有几个:

  1. 大多数主要操作系统都是用 C 和汇编语言组合编写的,并为操作系统提供 C 语言接口。
  2. 编写设备驱动程序通常涉及与设备进行通信,这涉及寄存器等的直接内存操作。 C 和 C++ 允许您以本机方式执行此操作。

编辑 - 直接内存操作示例:

让我编造一些虚构的设备。假设它有一个寄存器,当您向其中写入某些值时,设备上的灯就会打开。该寄存器为 32 位宽。前 31 位表示灯光的亮度,最低位表示灯光的打开和关闭。

当您将设备插入计算机时,操作系统会为该寄存器分配一个特定的内存位置(假设在 32 位操作系统上为 0x00FF00FF00)。为了打开灯,设备驱动程序会执行以下操作:

int* lightRegister = 0x00FF00FF00; // You would normally ask the OS for this address, not hardcode it.
int brightnessValue = 256; // Only Even numbers, the low bit must be zero.
*lightRegister = brightnessValue | 1; //Turn it on.
*lightRegister = 0; // Turn it off.

像 Java 这样的高级语言,通常不会让您写入这样的随机内存位置。

I would say that C is much more commonly used as a language for device drivers (over C++).

There are a couple of reasons for this:

  1. Most of the major operating systems are written in a combination of C and assembler and provide C language interfaces to the operating system.
  2. Writing a device driver usually involves communicating with a device which involves direct memory manipulation of registers and the like. C and C++ allow you to do this natively.

Edit - Example of Direct Memory Manipulation:

Let me make up some fictitious device. Say it has a register, that when you write certain values onto it, makes a light on the device turn on. This register is 32 bits wide. The top 31 bits say how bright to make the light, the lowest bit turns it on and off.

When you plug the device into the computer, the operating system assigns that register a particular memory location, (let's say 0x00FF00FF00 on a 32 bit OS). In order to turn the light on, the device driver would do something like this:

int* lightRegister = 0x00FF00FF00; // You would normally ask the OS for this address, not hardcode it.
int brightnessValue = 256; // Only Even numbers, the low bit must be zero.
*lightRegister = brightnessValue | 1; //Turn it on.
*lightRegister = 0; // Turn it off.

Higher level languages like Java, don't generally let you write into some random memory location like this.

网白 2024-10-18 15:27:31

这里有关于在 Windows 中使用 C++ 进行设备驱动程序的优缺点的很好的讨论:
用于内核模式驱动程序的 C++:优点和缺点

很多 C++ 与C 讨论是“宗教”的,是个人喜好的问题。一般来说,在较低级别上工作可以让您更好地控制内存和性能等内容,但也可能需要更多时间来复制高级语言中可用的设施。做出这些权衡并为正确的问题选择正确的工具是软件开发的一部分。

没有根本原因说明您不能使用 Java 来编写设备驱动程序。这更多的是对语言、库以及驱动程序内部需要完成的工作类型(中断处理程序、I/O 等)的运行时支持的问题。性能也可能是一个问题。您需要开发大量的基础设施才能做到这一点。虽然 Java 不提供低级设施(例如指针),但可以用对本机函数的调用来替换这些设施。

There is a good discussion on the pros and cons of using C++ for device drivers in Windows here:
C++ for Kernel Mode Drivers: Pros and Cons

A lot of the C++ vs. C discussion is "religious" and a question of personal preference. In general working at a lower level gives you better control over things like memory and performance but may also require more time duplicating facilities that may be available in higher level languages. Making those trade-offs and choosing the right tool for the right problem is part of what software development is about.

There is no fundamental reason why you couldn't use Java for writing a device driver. It's more of a question of run-time support for the language, the libraries and the kind of work you need to get done inside the driver (interrupt handlers, I/O etc.). Performance may also be an issue. You would need to develop a very large amount of infrastructure to do that. While Java doesn't offer low level facilities (such as pointers) these could be replaced with calls to a native function.

波浪屿的海角声 2024-10-18 15:27:31

C++ 可以生成非常高效的汇编代码,并提供更高级别的构造,例如模板、RAII 和 OOP。对于现代程序来说,汇编太慢了,无法手工编写,并且不能提供现代程序员所期望的许多东西,比如 OOP,而 Java 则太慢了,无法编写设备驱动程序。

C++ can produce very efficient assembly code AND provide higher-level constructs such as templates, RAII and OOP. Assembly is much too slow to write by hand for modern programs and does't provide many things that modern programmers expect, like OOP, whereas Java would be MUCH too slow to write a device driver in.

完美的未来在梦里 2024-10-18 15:27:31

我不知道为什么?以及是否确实如此,但我猜想,C++ 的效率和高级抽象的结合使其成为需要性能并可以从高级抽象中受益的任务的非常好的候选者。

I don't know why? and whether it's actually true, but I would guess, C++ combination of efficiency and high level of abstraction makes it a very good candidate for tasks that require performance and can benefit from high level abstractions.

贪恋 2024-10-18 15:27:31

因为 C++ 是最后一种广泛传播的语言(C 除外),它可以直接翻译为机器代码,没有太多复杂性。

Because C++ is the last well spread language (except C) which translates directly to machine code without too many complications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文