I need to execute clean up functions in SIGINT handler, but I can't pass local data to it. Here an example:
int main(int argc, char *argv[]) {
struct database *db = db_cursor();
sockfd_t master_socket = init_server();
signal(SIGINT, sigint_handler);
while (1) {
// accepting connections
}
}
void sigint_handler(int s) {
destroy_db(db);
shutdown(master_socket, SHUT_RDWR);
close(master_socket);
exit(EXIT_SUCCESS);
}
How can I implement such behaviour? I've tried make this variables are global, but I can't invoke this functions at compile time (error: initializer element is not a compile-time constant).
sig_atomic_t
. – Coriecorillaaccept
, and when I invokeSIGINT
it doesn't stop immediately. Should I use multiplexing to address this issue? – Arlenarlenaaccept()
doesn't return, create this signal-handler usingsigaction()
instead ofsignal()
and unset theSA_RESTART
flag in the membersa_flags
of the structure passed. – Coriecorilla