Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fork leads to double release of device

Status
Not open for further replies.

scienzia

Programmer
Feb 21, 2002
160
IT
Hi,

I am working with a device driver.
When I close the device file I start the release procedure in the module.

The problem is that if the application that opened the device file makes a fork, I have 2 releases, since after the fork both the processes have the device file open.
When one of the 2 processes closes the device file, I have a kernel panic because the other process keeps using it.

Is there a way of preventing this behaviour?

Thanks in advance.
 
the device closes at the end of the execution, I can't control it
 
When you fork, there is the original task and the new task. Can the original task close before the new task?

Option 1 - original always closes after the new. Only original can release the device

Option 2 - new always closes after the original. Only new can release the device.

Option 3 - they close in any order. See if the other task is still running. If it isn't then release the device.
 
The original task is the only one that uses the device.
The new task closes before the original.
When the new task closes, I have the problems because it starts the release function in the module, when the original task uses the device after the new task closed.

It should be option 1, but how can I stop it from releasing the device?
 
When you open the device, save the result of getpid() in the driver state information.

When you close the device, compare the value of getpid() with that previously stored. If it's different, then you know the close is from a different process, and you can ignore it.

Is this Unix as in Linux, in that you potentially have the source code for everything. Or is it some proprietary Unix and/or driver which you have no control over.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top