Resource cleanup on abnormal process termination Resource cleanup on abnormal process termination multithreading multithreading

Resource cleanup on abnormal process termination


As implied by your question, this depends on the kill signal that's sent to the program. AFIK it's really only KILL (i.e. kill -kill) will terminate a process without giving the process an opportunity to shut itself down properly. Other signals like TERM OR SIGINT can be hooked by the program itself and either ignored, or used to initiate some clean shutdown process. I guess the mildest signals like SIGHUP won't do anything unless the code has explicit hooks programmed into it. So I don't know the specific answer to your question about file locks, but consider the fact that it's probably only really kill -kill that you're worried about here.


No, there is no order in releasing the resources. Only for sure, locks are released after the process is terminated.

As I understand your question, you hold two or more "locks" which relate to each other. Somehow your application depends on the exact release order of the locks. Without knowing much details about your problem, this seems to be just bad design.

If the lock of the file, depends on the lock of the shared memory, you should implement this dependency programatically.

Another solution is just to wait for e.g. 100 milliseconds, checking the second lock. Because you can assume, all locks of the terminated process will be released in a short time period. If your newly started application can acquire the first lock, it will first wait for 100 milliseconds before it tries to acquire the file lock (ore the other way around). This automatically avoids any race condition if a process was terminated just at this point.