4

For example , I have two processes A and B that try to open a special file (/dev/example) . The driver has an open method that initializes a structure (struct c) and passes it to 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). Is the open method going to initialize struct C again and pass it to filp->private_data and what will happen to the one process A initialized ?

chrk
  • 3,739
  • 2
  • 40
  • 45
mark4rd
  • 235
  • 4
  • 11
  • What function are you using to open it? – Patrick Collins Aug 25 '14 at 13:51
  • with `open()` . Would different functions have different results? – mark4rd Aug 25 '14 at 13:56
  • I don't think so, it appears that `open` is a wrapper around a system call, I was mostly wondering what documentation to look for. – Patrick Collins Aug 25 '14 at 14:08
  • 1
    I suppose that would depend quite a bit on how the driver is actually implemented. In some cases, it would make sense to initialize a single struct and return that to every caller to `open()`. In others, it would make sense to generate a unique struct for every user. There's no way to tell which is appropriate in this case without more information on the driver (probably including code)... – twalberg Aug 25 '14 at 14:22
  • Thanks for the reply. As far as I know , no one returns any struct to any caller. Each file has a specific struct file that represents it and it is passed to any function that operates on the file . The question is general and not about a specific driver . – mark4rd Aug 25 '14 at 14:38
  • If using `O_APPEND` with `fopen()` both processes could write to the file at the same file I believe... – shkschneider Aug 25 '14 at 15:39

1 Answers1

2

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).

This is wrong. Every open(2) is matched with one struct file. Quoting from 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.

For two processes to share the same struct file, then have to have a parent-child relationship, as the only ways to create a duplicate of a file descriptor (two file descriptors pointing to the same struct file) is a fork(2) or the dup(2) system calls.

In your case, since each open(2) initializes a struct C, and both processes A and B called open(2), they don't share the same struct C through their private_data field, as they're associated with different struct file.

What they share and possibly confused you is the struct inode.

chrk
  • 3,739
  • 2
  • 40
  • 45
  • 1
    "For two processes to share the same `struct file`, they have to have a parent-child relationship" — or they used FD passing over a UNIX domain socket. See: http://stackoverflow.com/q/909064/149341 –  Aug 25 '14 at 15:54