{"id":2345,"date":"2015-12-23T10:51:53","date_gmt":"2015-12-23T10:51:53","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2345"},"modified":"2015-12-23T11:01:26","modified_gmt":"2015-12-23T11:01:26","slug":"signal-function","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/signal-handling\/signal-function","title":{"rendered":"signal() function"},"content":{"rendered":"<p>\nThe signal() function allows you to register your own functions to be called when one of the following signals occur:\n<\/p>\n<p>\nSIGABRT&nbsp;&#8211;&nbsp;Abnormal termination of the program, such as a call to abort<br \/>\nSIGFPE &#8211;&nbsp;An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow<br \/>\nSIGILL&nbsp;&#8211;&nbsp;Detection of an illegal instruction<br \/>\nSIGINT&nbsp;&#8211;&nbsp;Receipt of an interactive attention signal<br \/>\nSIGSEGV &#8211;&nbsp;An invalid access to storage<br \/>\nSIGTERM &#8211;&nbsp;A termination request sent to the program\n<\/p>\n<h4>\nGood Resources<br \/>\n<\/h4>\n<p>\n<a href=\"http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_signal_handling.htm\">http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_signal_handling.htm<\/a>\n<\/p>\n<h4>\nExample<br \/>\n<\/h4>\n<p>\nIn your Initialize function:\n<\/p>\n<pre>\r\n<code>\r\n\t\/\/----- REGISTER EXTERNAL SIGNAL FUNCTION HANDLERS -----\r\n\t\/\/Functions to be called on these special signals being triggered\r\n\tsignal(SIGTERM, onterm);\r\n\tsignal(SIGABRT, onterm);\r\n\tsignal(SIGINT, onterm);\r\n\tsignal(SIGQUIT, onterm);\r\n\tatexit(onexit);\r\n<\/code><\/pre>\n<p>\nThe functions that will be called:\n<\/p>\n<pre>\r\n<code>\r\n\/\/*****************************************\r\n\/\/*****************************************\r\n\/\/********** ON TERMINATE SIGNAL **********\r\n\/\/*****************************************\r\n\/\/*****************************************\r\n\/\/This function is registered in initialise() to be called on various error signal events\r\nstatic void onterm(int s)\r\n{\r\n\texit(s != SIGHUP  &amp;&amp; s != SIGINT &amp;&amp; s != SIGQUIT &amp;&amp; s != SIGABRT &amp;&amp; s != SIGTERM ? EXIT_SUCCESS : EXIT_FAILURE);\r\n}\r\n\r\n\/\/************************************\r\n\/\/************************************\r\n\/\/********** ON EXIT SIGNAL **********\r\n\/\/************************************\r\n\/\/************************************\r\n\/\/This function is registered in initialise() to be called on the exit event\r\nstatic void onexit(void)\r\n{\r\n\t\/\/Nicely shut down and application specific things\r\n\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The signal() function allows you to register your own functions to be called when one of the following signals occur: SIGABRT&nbsp;&#8211;&nbsp;Abnormal termination of the program, such as a call to abort SIGFPE &#8211;&nbsp;An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow SIGILL&nbsp;&#8211;&nbsp;Detection of an illegal instruction SIGINT&nbsp;&#8211;&nbsp;Receipt of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[],"class_list":["post-2345","post","type-post","status-publish","format-standard","hentry","category-signal-handling"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2345"}],"version-history":[{"count":3,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2345\/revisions"}],"predecessor-version":[{"id":2348,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2345\/revisions\/2348"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}