I have a console server which listens clients and does what they need. But sometimes I need end server and I don't know how I can do it properly. Because I can hit ctrl+c to end program, but I have important code after loop which I need to do.
main_function(){
while(true){
listen();
}
do_something_important();
}
Can I catch some signal from console and run function to do important stuff and properly end?
end_program(){
do_something_important();
return 0;
}
Or what is the best way what I can do in my situation?