c++ - What's the proper way to cleanup an infinite loop application in Linux? -


i come windows environment, i'm bit lost making transition writing things in linux.

say i've got simple c/c++ application so:

int main(int argc, char** argv) {     int h = open("something");      while (true)     {         // work         usleep(10000);     }      close(h); } 

in windows, i'd use getasynckeystate() (or other sort of keyboard checking functionality) escape key, , break out of loop when pressed. way close(h) called, , i'd cleanup need to.

the way i've been terminating applications in linux has been using ctrl+c, reading means sends sigint, , 'friendly' way cause application quit. however, in experience, that's caused sorta drop , close wherever when got signal (meaning post-loop cleanup never runs.)

some have suggested use signal() listen sigint, others disagree method. (plus, tends create bit more differences between how things done between windows , linux, , i'd stay close possible running on both platforms.)

is there 'best practice' things this?

using sigint handler normal way handle these things in posix world, , similar using setconsolectrlhandler in windows.

in sigint signal handler, set flag, , loop checks flag if should exit or not.

however, it's not strictly needed, unless have special needs in cleanup (like example sending goodbye-message other applications or similar). os make sure files closed properly, memory allocations free'd, etc. same in windows.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -