問題描述
我聽說過一些關于用 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)
- 我一般是錯的嗎假設?(好像是這樣)
- 外星人"中的司機如何操作系統中使用的語言?
- 有什么要求(來自編程語言的觀點)無論如何都需要設備驅動程序?
感謝閱讀
推薦答案
有幾種方法可以做到這一點.
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 編寫設備驅動程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!