Resource cleanup when process abnormal

My question is: when the process is abnormally interrupted (via a signal, it can be SIGKILL, so we cannot intercept it), is there a guaranteed order or atomicity in which its resources are freed? In particular, I am interested in file locks and shared memory.

For example:

1) If a process holds locks on 2 files and ends abnormally, is it possible that another process trying to lock the same files sees that one file is locked and the other is unlocked? Or does the process of freeing files block atoms from the point of view of other processes?

If it is not atomic, is there at least a predefined order in which file locks will be released by the completion process (for example, in the reverse order from which they were initially locked)?

2) I wanted to use file locking to ensure the correct initialization of shared memory. Processes mapped to shared memory will contain a common lock, and a new process that wants to map to the same shared memory segment will try to check this lock to see if it needs to be initialized (if necessary, I can provide more detailed information later) .

However, the same question arises here: if the process that contains the file lock and is also displayed in the shared memory segment is abnormally interrupted, is it possible that after the shared memory is automatically turned off, other processes will still see the file lock as locked ? Or is it a decoupling of the shared memory segment and unlocking the file atom from the point of view of other processes?

+7
c multithreading unix shared-memory ipc
source share
2 answers

As implied by your question, it depends on the kill signal that is sent to the program. AFIK, this is really just KILL (i.e. kill -kill ) will terminate the process, preventing the process from closing properly. Other signals, such as TERM OR SIGINT, can be connected by the program itself and either ignored or used to initiate a clean shutdown process. I think that the softest signals, such as SIGHUP, will do nothing if the code does not have explicit hooks programmed in it. Therefore, I do not know a specific answer to your question about file locks, but I believe that perhaps this is really just the kill -kill that you are worried about.

0
source share

No, there is no order in the release of resources. Only surely the locks are released after the process ends.

As I understand your question, you are holding two or more “locks” that are connected to each other. Somehow your application depends on the exact lock lock order. Without knowing the details of your problem, it seems to be just a bad design .

If the file lock depends on the shared memory lock, you must implement this dependency programmatically.

Another solution is to just wait, for example, 100 milliseconds, checking the second lock. Since you can assume that all locks of the completed process will be released in a short amount of time. If your recently launched application can receive the first lock, it will first wait 100 milliseconds before it tries to obtain a file lock (vice versa, vice versa). This automatically avoids any race condition if the process was completed at this stage.

0
source share

All Articles