Linux内核有main函数吗? [英] Does Linux kernel have main function?

查看:25
本文介绍了Linux内核有main函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Device DriverKernel 编程.根据 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);

这里我有两个问题:

  1. 为什么我们不需要设备驱动程序中的 main() 函数?
  2. 内核有 main() 函数吗?
  1. Why we do not need main() function in Device Drivers?
  2. 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.

系统程序员定义(每个操作系统都有自己的方法)设备、用作设备驱动程序的加载模块以及加载模块中入口点的名称之间的连接.

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内核有main函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆