clinuxlinux-kernellinux-device-driverdrivers

What happens when a lot of processes open the same special file?


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 ?


Solution

  • 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, they have to have acquired their respective file descriptor via the same open(2) system call. This would mean that they either have a parent-child relationship (parent issues open(2), then fork(2)s the child, so that the returned file descriptor will be inherited from the parent to the child), or they could be pthreads that access the same file descriptor.

    In your case, assuming that processes A and B do not have a parent-child relationship, they issue a separate open(2) system call each. Therefore, they are associated with a different struct file each. As a result, each of them is expected to allocate, initialize and store (in its filp->private_data) a distinct struct C.

    What they do always share (and possibly confused you) is the struct inode, which refers to the special file.


    EDIT: As noted in the comments (by @twalberg), this is indeed highly-dependent on the driver. In general, filp->private_data is part of Linux kernel's driver API to allow separate state per open(2). However, depending on the driver, sometimes it might indeed be sensible to associate multiple different struct files to the same stored state instead.