例如,我有两个尝试打开特殊文件(/ dev / example)的进程A和B.驱动程序有一个open方法,用于初始化结构(struct c)并将其传递给filp-> private_data.如果之后进程B打开相同的特殊文件,会发生什么如果我理解正确,我们将有两个struct文件实例(两个指向同一结构文件的filp指针). open方法是否会再次初始化struct C并将其传递给filp-> private_data以及一个进程初始化后会发生什么? 最佳答案
When afterwards process B opens the same special file , what happens
If I understand correct is that we will have two instances of struct
file (two filp pointers that point to the same struct file).
这是错的.每个open(2)都与一个struct文件匹配.引自LDD3/Chapter3:
The file structure represents an open file. (It is not specific to
device drivers; every open file in the system has an associated struct
file in kernel space.) It is created by the kernel on open and is
passed to any function that operates on the file, until the last
close. After all instances of the file are closed, the kernel releases
the data structure.
对于共享同一结构文件的两个进程,则必须具有父子关系,因为创建文件描述符副本的唯一方法(指向同一结构文件的两个文件描述符)是fork(2)或dup(2)系统调用.
在您的情况下,由于每个open(2)初始化一个struct C,并且进程A和B都调用open(2),它们不会通过private_data字段共享相同的struct C,因为它们与不同的struct文件相关联.
他们分享并可能让你困惑的是struct inode
.