The HyperNews Linux KHG Discussion Pages

## Be careful in release() method when fork() or dup() syscalls are used!

  Forum: Device Driver Basics
  Keywords: fork dup release close
  Date: Wed, 14 Jan 1998 13:14:52 GMT
  From: Miran Miksic <mmiksic@hni.uni-paderborn.de>

I haven't found any documentation about my specific problem, so I'll just post things I've figured out to solve it. The problem was, what happens when the process which called open() on my device performs fork() or dup() calls? In that case, it is possible that the release() method would be called more times even when the device was opened only once. Normally, I want to reset device, disable interrupts, free io's or do some other nice things only when the last process issues the release() call. After exploring the kernel/fs/*.c files I've found out that there is a f_count variable in the 'struct file' structure which is incremented on every fork() or dup() call, and decremented on every close() call. So, if you want to enable more processes to access your device, you should examine this variable in release() method and act accordingly. If this variable is bigger than 1, there are more file descriptors using the same 'struct file', owned by either the same process (dup() call) or some other processes (fork() call). I hope it helps, and I'd like to ask if my interpretation of the f_count variable was correct.