flock() is generally safe as a signal since it is a system call. Its semantics makes it difficult to implement it in different ways. It is not in the POSIX list of functions supporting an asynchronous signal, because it is not in POSIX at all.
You probably don't need an explicit unlock, because flock() locks are automatically released when all file descriptors that reference a description of an open file are closed.
The calls to printf() and fprintf() should be replaced by the corresponding calls to write() . The stdio functions are not included in the list of safe functions for asynchronous signals and are often very dangerous for an asynchronous signal.
The abort() call is probably best replaced by setting the signal to the default action and resending it to itself; this way, the shells know that your program has gone out due to a signal and can interrupt command sequences when necessary.
source share