是否可以用 Java 编写设备驱动程序?

Is it possible to code a device driver in Java?(是否可以用 Java 编写设备驱动程序?)

本文介绍了是否可以用 Java 编写设备驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过一些关于用 Java 编写设备驱动程序的消息(听说是用我的耳朵"而不是从互联网上听到的)并且想知道......我一直认为设备驱动程序是在操作系统级别上运行的,因此必须用与操作系统相同的语言(因此主要是 CI 假设)

I heard something about writing device drivers in Java (heard as in "with my ears", not from the internet) and was wondering... I always thought device drivers operated on an operating system level and thus must be written in the same language as the OS (thus mostly C I suppose)

  1. 我一般是错的吗假设?(好像是这样)
  2. 外星人"中的司机如何操作系统中使用的语言?
  3. 有什么要求(来自编程语言的观点)无论如何都需要设备驱动程序?

感谢阅读

推荐答案

有几种方法可以做到这一点.

There are a couple of ways this can be done.

首先,在操作系统级别"运行的代码不需要使用与操作系统相同的语言编写.它只需要能够与操作系统代码链接在一起.几乎所有语言都可以与 C 互操作,而这正是我们所需要的.

First, code running at "OS level" does not need to be written in the same language as the OS. It merely has to be able to be linked together with OS code. Virtually all languages can interoperate with C, which is really all that's needed.

所以语言方面,技术上没有问题.Java函数可以调用C函数,C函数可以调用Java函数.如果操作系统不是用 C 编写的(比如说,为了论证它是用 C++ 编写的),那么操作系统 C++ 代码可以调用一些中间 C 代码,这些代码会转发给你的 Java,反之亦然.C 几乎是编程的一种通用语言.

So language-wise, there is technically no problem. Java functions can call C functions, and C functions can call Java functions. And if the OS isn't written in C (let's say, for the sake of argument that it's written in C++), then the OS C++ code can call into some intermediate C code, which forwards to your Java, and vice versa. C is pretty much a lingua franca of programming.

程序一旦被编译(编译为本机代码),其源语言就不再相关.无论源代码在编译之前是用哪种语言编写的,汇编程序看起来都差不多.只要你使用与操作系统相同的调用约定,就没有问题.

Once a program has been compiled (to native code), its source language is no longer relevant. Assembler looks much the same regardless of which language the source code was written in before compilation. As long as you use the same calling convention as the OS, it's no problem.

更大的问题是运行时支持.操作系统中没有很多软件服务可用.例如,通常没有 Java 虚拟机.(从技术上讲,没有理由不存在,但通常,但通常可以安全地假设它不存在).

A bigger problem is runtime support. Not a lot of software services are available in the OS. There usually is no Java virtual machine, for example. (There is no reason why there technically couldn't be, but usually, but usually, it's safe to assume that it's not present).

不幸的是,在其默认"表示中,作为 Java 字节码,Java 程序需要很多基础设施.它需要Java VM来解释和JIT字节码,它需要类库等等.

Unfortunately, in its "default" representation, as Java bytecode, a Java program requires a lot of infrastructure. It needs the Java VM to interpret and JIT the bytecode, and it needs the class library and so on.

但是有两种方法可以解决这个问题:

But there are two ways around this:

  • 在内核中支持 Java.这将是一个不寻常的步骤,但可以做到.
  • 或者将您的 Java 源代码编译为本机格式.Java 程序不必编译为 Java 字节码.您可以将其编译为 x86 汇编程序.这同样适用于您使用的任何类库.这些也可以一直编译到汇编程序.当然,部分 Java 类库需要某些不可用的操作系统功能,但可以避免使用这些类.

所以是的,这是可以做到的.但这并不简单,也不清楚你会得到什么.

So yes, it can be done. But it's not straightforward, and it's unclear what you'd gain.

当然,另一个问题可能是 Java 不允许您访问任意内存位置,这会使很多硬件通信变得非常棘手.但这也可以解决,也许可以通过调用非常简单的 C 函数来简单地将相关的内存区域作为数组返回,供 Java 处理.

Of course another problem may be that Java won't let you access arbitrary memory locations, which would make a lot of hardware communication pretty tricky. But that could be worked around too, perhaps by calling into very simple C functions which simply return the relevant memory areas as arrays for Java to work on.

这篇关于是否可以用 Java 编写设备驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是否可以用 Java 编写设备驱动程序?