linux课程设计指导文档 联系客服

发布时间 : 星期一 文章linux课程设计指导文档更新完毕开始阅读b08d5cda7f1922791688e89f

注意:有消息称,在2.6.20中,syscall的原型又有所变化,不需要给出参数个数 。

新版本的测试程序,编写下列程序运行即可:

#include

#include #include #include int main(int argc,char **argv) {

int b=syscall(318,200); printf(\ //200 return 0; }

注意:syscall()中的第一个参数318是系统调用号,第二个参数200个是函数的参数。

关于此部分的内容可以参考:http://space.univs.cn/?4484/viewspace-32545

http://os.yesky.com/lin/4/2657504.shtml

19

第3章 问题3及其相关知识

3.1. 基础知识

系统调用是操作系统内核和应用程序之间的接口,而设备驱动程序是操作系统内核和机器硬件之间的接口。设备驱动程序为应用程序屏蔽了硬件的细节,这样在应用程序看来,硬件设备只是一个设备文件, 应用程序可以象操作普通文件一样对硬件设备进行操作。设备驱动程序是内核的一部分,它完成以下的功能:

(1) 对设备初始化和释放.

(2) 把数据从内核传送到硬件和从硬件读取数据.

(3) 读取应用程序传送给设备文件的数据和回送应用程序请求的数据. (4) 检测和处理设备出现的错误.

Linux支持三中不同类型的设备:字符设备(character devices)、块设备(block devices)和网络设备(network interfaces)。字符设备和块设备的主要区别是:在对字符设备发出读/写请求时,实际的硬件I/O一般就紧接着发生了,块设备则不然,它利用一块系统内存作缓冲区,当用户进程对设备请求能满足用户的要求,就返回请求的数据,如果不能,就调用请求函数来进行实际的I/O操作.块设备是主要针对磁盘等慢速设备设计的,以免耗费过多的CPU时间来等待.

用户进程是通过设备文件来与实际的硬件打交道,每个设备文件都都有其文件属性(c/b),表示是字符设备还是块设备。另外每个文件都有两个设备号,第一个是主设备号,标识驱动程序,第二个是从设备号,标识使用同一个设备驱动程序的不同的硬件设备,比如有两个软盘,就可以用从设备号来区分他们.设备文件的的主设备号必须与设备驱动程序在登记时申请的主设备号一致,否则用户进程将无法访问到驱动程序.。

设备驱动程序工作的基本原理:

用户进程利用系统调用对设备进行诸如read/write操作,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着

20

把控制权交给该函数。

最后,在用户进程调用驱动程序时,系统进入核心态,这时不再是抢先式调度.也就是说,系统必须在你的驱动程序的子函数返回后才能进行其他的工作。如果你的驱动程序陷入死循环,你只有重新启动机器了。

3.2. 添加新模块的基本步骤

1. 写设备驱动源代码:

在设备驱动程序中有一个非常重要的结构file_operations,该结构的每个域都对应着一个系统调用。用户进程利用系统调用在对设备文件进行诸如read/write操作时,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数。

struct file_operations {

int (*seek) (struct inode * ,struct file *, off_t ,int); int (*read) (struct inode * ,struct file *, char ,int); int (*write) (struct inode * ,struct file *, off_t ,int);

int (*readdir) (struct inode * ,struct file *, struct dirent * ,int); int (*select) (struct inode * ,struct file *, int ,select_table *); int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long); int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *); int (*open) (struct inode * ,struct file *); int (*release) (struct inode * ,struct file *); int (*fsync) (struct inode * ,struct file *); int (*fasync) (struct inode * ,struct file *,int); int (*check_media_change) (struct inode * ,struct file *); int (*revalidate) (dev_t dev); }

编写设备驱动程序的主要工作是编写子函数,并填充file_operations的各个域。

21

例如:

Struct file_operations my_fops={

.read=my_read, .write=my_write, .open=my_open, .release=my_release }

然后再定义函数my_read,my_write,my_open,my_release相应的函数体。例如:

static ssize_t my_open(struct inode *inode,struct file *file){ static int counter=0; if(Device_Open) return -EBUSY; Device_Open++; /*写入设备的信息*/

sprintf(msg,\ msg_ptr=msg; return 0; }

同时对于可卸载的内核模块(LKM),至少还有两个基本的模块: 例如本例中的:

static int __init my_init(void){ int result;

result=register_chrdev(0,\ if(result<0){

printk(\ return -1; }

if(my_major==0){

22