大家都知道进程,可是知道linux是怎么管理其进程的吗?每一个进程都有一个进程描述符,具体是task_struct结构体存储相关的信息,在linux/sched.h文件里定义,那么我们先看看linux内核3.0版本的task_struct结构体的定义吧(删除了不必要的字段,只保留了重要的字段)。同时欢迎大家转载和交流。
struct task_struct {
//这个是进程的运行时状态,-1代表不可运行,0代表可运行,>0代表已停止。
volatile long state;
/*
flags是进程当前的状态标志,具体的如:
0x00000002表示进程正在被创建;
0x00000004表示进程正准备退出;
0x00000040 表示此进程被fork出,但是并没有执行exec;
0x00000400表示此进程由于其他进程发送相关信号而被杀死 。
*/
unsigned int flags;
//表示此进程的运行优先级
unsigned int rt_priority;
//这里出现了list_head结构体,详情请参考
struct list_head tasks;
//这里出现了mm_struct 结构体,该结构体记录了进程内存使用的相关情况,详情请参考
struct mm_struct *mm;
/* 接下来是进程的一些状态参数*/
//exit_signal:当进程退出时发给父进程的信号,如果是轻量级进程为-1
int exit_state;
int exit_code, exit_signal;
//这个是进程号
pid_t pid;
//这个是进程组号
pid_t tgid;
//real_parent是该进程的”亲生父亲“,不管其是否被“寄养”。
struct task_struct *real_parent;
//parent是该进程现在的父进程,有可能是”继父“
struct task_struct *parent;
//这里children指的是该进程孩子的链表,可以得到所有孩子的进程描述符,但是需使用list_for_each和list_entry,list_entry其实直接使用了container_of,详情请参考
struct list_head children;
//同理,sibling该进程兄弟的链表,也就是其父亲的所有孩子的链表。用法与children相似。
struct list_head sibling;
//这个是主线程的进程描述符,也许你会奇怪,为什么线程用进程描述符表示,因为linux并没有单独实现线程的相关结构体,只是用一个进程来代替线程,然后对其做一些特殊的处理。
struct task_struct *group_leader;
//这个是该进程所有线程的链表。
struct list_head thread_group;
//顾名思义,这个是该进程使用cpu时间的信息,utime是在用户态下执行的时间,stime是在内核态下执行的时间。
cputime_t utime, stime;
//下面的是启动的时间,只是时间基准不一样。
struct timespec start_time;
struct timespec real_start_time;
//comm是保存该进程名字的字符数组,长度最长为15,因为TASK_COMM_LEN为16。
char comm[TASK_COMM_LEN];
/* 文件系统信息计数*/
int link_count, total_link_count;
/*该进程在特定CPU下的状态*/
struct thread_struct thread;
/* 文件系统相关信息结构体*/
struct fs_struct *fs;
/* 打开的文件相关信息结构体*/
struct files_struct *files;
/* 信号相关信息的句柄*/
struct signal_struct *signal;
struct sighand_struct *sighand;
/*这些是松弛时间值,用来规定select()和poll()的超时时间,单位是纳秒nanoseconds */
unsigned long timer_slack_ns;
unsigned long default_timer_slack_ns;
};
1.1 struct file
struct file结构体定义在include/linux/fs.h中定义。文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码中,struct file的指针通常被命名为file或filp。如下所示:
struct file {
union {
struct list_head fu_list; 文件对象链表指针linux/include/linux/list.h
struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制
} f_u;
struct path f_path; 包含dentry和mnt两个成员,用于确定文件路径
#define f_dentry f_path.dentry f_path的成员之一,当前文件的dentry结构
#define f_vfsmnt f_path.mnt 表示当前文件所在文件系统的挂载根目录
const struct file_operations *f_op; 与该文件相关联的操作函数
atomic_t f_count; 文件的引用计数(有多少进程打开该文件)
unsigned int f_flags; 对应于open时指定的flag
mode_t f_mode; 读写模式:open的mod_t mode参数
off_t f_pos; 该文件在当前进程中的文件偏移量
struct fown_struct f_owner; 该结构的作用是通过信号进行I/O时间通知的数据。
unsigned int f_uid, f_gid; 文件所有者id,所有者组id
struct file_ra_state f_ra; 在linux/include/linux/fs.h中定义,文件预读相关
unsigned long f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
// needed for tty driver, and maybe others */
void *private_data;
#ifdef CONFIG_EPOLL
// Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif //#ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
};
1.2 struct dentry
dentry的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件,也可以是目录。 inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在运行的时候链接多个dentry,而d_count记录了这个链接的数量。
struct dentry {
atomic_t d_count; 目录项对象使用计数器,可以有未使用态,使用态和负状态
unsigned int d_flags; 目录项标志
struct inode * d_inode; 与文件名关联的索引节点
struct dentry * d_parent; 父目录的目录项对象
struct list_head d_hash; 散列表表项的指针
struct list_head d_lru; 未使用链表的指针
struct list_head d_child; 父目录中目录项对象的链表的指针
struct list_head d_subdirs; 对目录而言,表示子目录目录项对象的链表
struct list_head d_alias; 相关索引节点(别名)的链表
int d_mounted; 对于安装点而言,表示被安装文件系统根项
struct qstr d_name; 文件名
unsigned long d_time;
struct dentry_operations *d_op; 目录项方法
struct super_block * d_sb; 文件的超级块对象
vunsigned long d_vfs_flags;
void * d_fsdata; 与文件系统相关的数据
unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名
};
1.3 struct files_struct
对于每个进程,包含一个files_struct结构,用来记录文件描述符的使用情况,定义在include/linux/file.h中
struct files_struct
{
atomic_t count; 使用该表的进程数
struct fdtable *fdt;
struct fdtable fdtab;
spinlock_t file_lock ____cacheline_aligned_in_smp;
int next_fd; 数值最小的最近关闭文件的文件描述符,下一个可用的文件描述符
struct embedded_fd_set close_on_exec_init; 执行exec时需要关闭的文件描述符初值集合
struct embedded_fd_set open_fds_init; 文件描述符的屏蔽字初值集合
struct file * fd_array[NR_OPEN_DEFAULT]; 默认打开的fd队列
};
struct fdtable {
unsigned int max_fds;
struct file ** fd; 指向打开的文件描述符列表的指针,开始的时候指向fd_array,当超过max_fds时,重新分配地址fd_set *close_on_exec; 执行exec需要关闭的文件描述符位图(fork,exec即不被子进程继承的文件描述符)
fd_set *open_fds; 打开的文件描述符位图
struct rcu_head rcu;
struct fdtable *next;
1.4 struct fs_struct
struct fs_struct {
atomic_t count; 计数器
rwlock_t lock; 读写锁
int umask;
struct dentry * root, * pwd, * altroot;根目录("/"),当前目录以及替换根目录
struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;
};
1.5 struct inode
索引节点对象由inode结构体表示,定义文件在linux/fs.h中。
struct inode {
struct hlist_node i_hash; 哈希表
struct list_head i_list; 索引节点链表
struct list_head i_dentry; 目录项链表
unsigned long i_ino; 节点号
atomic_t i_count; 引用记数
umode_t i_mode; 访问权限控制
unsigned int i_nlink; 硬链接数
uid_t i_uid; 使用者id
gid_t i_gid; 使用者id组
kdev_t i_rdev; 实设备标识符
loff_t i_size; 以字节为单位的文件大小
struct timespec i_atime; 最后访问时间
struct timespec i_mtime; 最后修改(modify)时间
struct timespec i_ctime; 最后改变(change)时间
unsigned int i_blkbits; 以位为单位的块大小
unsigned long i_blksize; 以字节为单位的块大小
unsigned long i_version; 版本号
unsigned long i_blocks; 文件的块数
unsigned short i_bytes; 使用的字节数
spinlock_t i_lock; 自旋锁
struct rw_semaphore i_alloc_sem; 索引节点信号量
struct inode_operations *i_op; 索引节点操作表
struct file_operations *i_fop; 默认的索引节点操作
struct super_block *i_sb; 相关的超级块
struct file_lock *i_flock; 文件锁链表
struct address_space *i_mapping; 相关的地址映射
struct address_space i_data; 设备地址映射
struct dquot *i_dquot[MAXQUOTAS];节点的磁盘限额
struct list_head i_devices; 块设备链表
struct pipe_inode_info *i_pipe; 管道信息
struct block_device *i_bdev; 块设备驱动
unsigned long i_dnotify_mask;目录通知掩码
struct dnotify_struct *i_dnotify; 目录通知
unsigned long i_state; 状态标志
unsigned long dirtied_when;首次修改时间
unsigned int i_flags; 文件系统标志
unsigned char i_sock; 套接字
atomic_t i_writecount; 写者记数
void *i_security; 安全模块
__u32 i_generation; 索引节点版本号
union {
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该 成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
} ;
};
我们在进程中打开一个文件F,实际上就是要在内存中建立F的dentry,和inode结构,并让它们与进程结构联系来,把VFS中定义的接口给接起来。
(1)、我们先找到一个设备号devno,可以动态申请,也可以静态设定,假设静态设定为major,minor,通过宏MKDEV(major,minor)来生成devno
(2)、构建对设备的操作函数file_opreation结构体,里面包含了的设备的操作:open、read、write、release、ioctl等
(3)、构建cdev结构体,里面填充两个主要成员dev(设备号)、file_operation(对设备的操作)
(4)、把cdev添加的cdev链表中:cdev_init、cdev_add
struct cdev {
struct kobject kobj;
struct module *owner; //所属模块
const struct file_operations *ops; //文件操作结构,在写驱动时,其结构体内的大部分函数要被实现
struct list_head list;
dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号
unsigned int count;
};
应用程序中:
fd=open("/dev/hello",O_RDWR)来打开设备文件,此设备节点对应有一个设备号,这是我们识别驱动和设备的桥梁。
打开 /dev/hello时,根据设备号,在cdev链表中找到cdev这个结构体,cdev里面包含了file_operation结构体,有设备的各种操作,打开时就调用里面的.open 函数。在这里要完成几件事:
(1)inode节点 每一个文件都对应有一个inode节点,inode结构体里.i_fop由cdev的file_operation填充,i_rdev由cdev的设备号填充
(2)file结构体中的file_operation也同样由cdev中对应项填充,还有一项fd,对应于打开文件的文件描述符,fd和file一一对应,文件每打开一次,就有一个file结构it。所以file里面的.private就很重要,下面会说到。
还有一个问题,那就是多个相同的设备,会公用同一个驱动,所以要把每一个设备的私有数据封装起来,构成一个私有数据结构体。对设备的每一次读写,都通过操作设备的私有数据结构体中的资源来完成。也就是说,驱动在加载的时候,会申请多个设备私有资源结构体,每个结构体中包含了设备的所有私有资源,虽然公用一个驱动,可是通过设备号找到此设备号对应设备的私有资源,说的有点拗口。这可以通过file结构体的.private来指向。
例如封装私有数据的结构体为:
struct hello_device{
char buf[128]; //设备的私有资源,譬如buf
struct cdev cdev;//设备结构体,里面有devno和file_operation
……
};
前面应经提到inode中的i_cdev会指向cdev结构,所以可以由container宏来得到hello_device的地址。
所以,在驱动的open函数中有两个参数,inode和file
int open(structc inode *inode,struct file *file){
struct hello_device *p =container(inode->i_cdev,hello_struct,cdev)
file->private=p;
}
首先,系统调用open打开一个字符设备的时候, 通过一系列调用,最终会执行到 chrdev_open.
(最终是通过调用到def_chr_fops中的.open, 而def_chr_fops.open = chrdev_open. 这一系列的调用过程,本文暂不讨论)
int chrdev_open(struct inode * inode, struct file * filp)
chrdev_open()所做的事情可以概括如下:
1. 根据设备号(inode->i_rdev), 在字符设备驱动模型中查找对应的驱动程序, 这通过kobj_lookup() 来实现, kobj_lookup()会返回对应驱动程序cdev的kobject.
2. 设置inode->i_cdev , 指向找到的cdev.
3. 将inode添加到cdev->list的链表中.
4. 使用cdev的ops 设置file对象的f_op
5. 如果ops中定义了open方法,则调用该open方法
6. 返回.
执行完 chrdev_open()之后,file对象的f_op指向cdev的ops,因而之后对设备进行的read, write等操作,就会执行cdev的相应操作.