1
votes

Background: I am writing a QA automation platform for an API which outputs formatted results to a specified directory. In addition, I have developed a GUI application for analyzing these results. A user may run the second application trying to analyze test results while our automated build system is running the first application modifying / generating new test data. To avoid thrashing, I have each application acquire file locks when making modifications, and releasing them when they are done. Upon normal program termination, if the running application has acquired a lock on the data directory it is released.

Problem: I need to be able to release the aforementioned file locks when either tool exists prematurely (user pressing CTRL-C, user stopping the application in debugger, or due to buggy API / application logic being tested). To handle this, I have implemented a signal handler using sigaction which handles intercepting fatal signals (tested and working), and have implemented a ctrl-c handler via the Win32 function SetConsoleCtrlHandler. However, I cannot seem to find a way to intercept the event of a user pressing the Stop Debugging button in Visual Studio. I assume this event generates something like SIGKILL / SIGSTOP (which cannot be handled through sigaction) but I would also hope there is some std library or Win32 functionality to intercept this event and perform some cleanup. Do you guys know of a way to handle this event or even what exactly this button does to kill a running application?

1
The button will call TerminateProcess. There is no way for the target process to intercept that. However, a dying process will automatically release all locks (and all handles and all memory). What is not working for you?avakar
As the file locks are acquired over nfs I cannot use flock. Also I cannot use fnctl as it is not available on windows. I use mkdir / rmdir as described in this post : stackoverflow.com/questions/6228915/…Sir Digby Chicken Caesar
Perhaps it would be better to use F_SETLK/F_UNLCK fcntl locks on non Windows platforms and use some Win32 locking function on windows. Do you know of any Win32 file locking functions that work over nfs?Sir Digby Chicken Caesar

1 Answers

1
votes

If you're using boost, you can use boost::interprocess::windows_shared_memory.

It is guaranteed to be released when the process ends.

Boost is just a neat wrapper around the windows API in this case. It wraps the Windows Named Shared Memory API.