I have a C++ server program running on Linux that receives data from client computers and stores them in a MySQL database. For efficiency, the incoming data is kept in an in-memory buffer and flushed periodically to the database (or when its size reaches a threshold). The flushing period can be long (eg: 3 minutes).
If a user terminates this program (by sending SIGTERM
or SIGINT
), it should ideally flush the buffers to the database before exiting (a graceful exit).
I have learned from these questions that signal handling is the way to go.
Linux C catching kill signal for graceful termination
I want to clarify the following points;
Is writing in-memory data to a database a valid thing to do after catching a signal?
Is it better to use
signalfd()
rather thansigaction()
as I can then receive the signals via an FD in the server'sselect()
loop?Should I catch signals other than
SIGTERM
andSIGINT
in this scenario for a graceful exit?