问题描述
我正在学习Device Driver
和Kernel
编程.根据Jonathan Corbet的书,我们在设备驱动程序中没有main()
功能.
I am learning Device Driver
and Kernel
programming.According to Jonathan Corbet book we do not have main()
function in device drivers.
#include <linux/init.h>
#include <linux/module.h>
static int my_init(void)
{
return 0;
}
static void my_exit(void)
{
return;
}
module_init(my_init);
module_exit(my_exit);
这里我有两个问题:
- 为什么我们在设备驱动程序中不需要
main()
功能? - 内核是否具有
main()
功能?
- Why we do not need
main()
function in Device Drivers? - Does Kernel have
main()
function?
推荐答案
从根本上讲,将例程命名为main()
并没有什么特别的.如上所述,main()
用作可执行加载模块的入口点.但是,您可以为装入模块定义不同的入口点.实际上,您可以定义多个入口点,例如,引用您喜欢的dll.
Fundamentally, there is nothing special about a routine being named main()
. As alluded to above, main()
serves as the entry point for an executable load module. However, you can define different entry points for a load module. In fact, you can define more than one entry point, for example, refer to your favorite dll.
从操作系统(OS)的角度来看,它真正需要的只是充当设备驱动程序的代码的入口点的地址.当需要设备驱动程序对设备执行I/O操作时,操作系统会将控制权传递给该入口点.
From the operating system's (OS) point of view, all it really needs is the address of the entry point of the code that will function as a device driver. The OS will pass control to that entry point when the device driver is required to perform I/O to the device.
系统程序员定义(每个OS都有自己的方法)设备之间的连接,充当设备驱动程序的加载模块以及加载模块中入口点的名称.
A system programmer defines (each OS has its own method) the connection between a device, a load module that functions as the device's driver, and the name of the entry point in the load module.
每个操作系统(显然)都有自己的内核,有些可能(也许)以main()
开头,但是我发现使用了main()
的内核而不是简单的内核(例如UNIX)使我感到惊讶.在编写内核代码时,您已经远远超出了将要编写的每个模块命名为main()
的要求.
Each OS has its own kernel (obviously) and some might/maybe start with main()
but I would be surprised to find a kernel that used main()
other than in a simple one, such as UNIX! By the time you are writing kernel code you have long moved past the requirement to name every module you write as main()
.
希望这有帮助吗?
从Unix版本6的内核中找到此代码段.如您所见,main()
只是另一个程序,正在尝试入门!
Found this code snippet from the kernel for Unix Version 6. As you can see main()
is just another program, trying to get started!
main()
{
extern schar;
register i, *p;
/*
* zero and free all of core
*/
updlock = 0;
i = *ka6 + USIZE;
UISD->r[0] = 077406;
for(;;) {
if(fuibyte(0) < 0) break;
clearsig(i);
maxmem++;
mfree(coremap, 1, i);
i++;
}
if(cputype == 70)
for(i=0; i<62; i=+2) {
UBMAP->r[i] = i<<12;
UBMAP->r[i+1] = 0;
}
// etc. etc. etc.
这篇关于Linux内核有主要功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!